Oracle建立表,id爲自增序列

1.建立表測試

create table my_table (rem

 id number(10) constraint pk_id primary key,
 name varchar2(20) not null,
 phone_Number varchar2(20) constraint unique_phone_number unique,
 email_Address varchar2(200) constraint email_not_null not null,
 home_Address varchar2(200) constraint home_addr_not_null not null
)
#查看約束 
select * from user_constraints;it

 

2.建立序列table

create sequence my_table_seq start with 1 increment by 1;
#查看序列
select * from user_sequences;email

#刪除序列select

 DROP SEQUENCE my_table_seq;im

3.建立觸發器next

create or replace trigger bi_my_table 
before insert on my_table
for each row
when(new.id is null)
begin
  select my_table_seq.nextval into:NEW.ID from dual;
end;tab

或者mail

CREATE OR REPLACE TRIGGER  "bi_my_table" 
  before insert on "my_table"              
  for each row 
begin  
  if :new."ID" is null then
    select "my_table_seq".nextval into :new."ID" from sys.dual;
  end if;
end;
ALTER TRIGGER  "bi_my_table" ENABLE

#查看觸發器
select * from user_triggers;

#刪除觸發器

DROP TRIGGER "bi_my_table" ;

#測試

insert into my_table(name,phone_number,email_address,home_address) values('zcl','13800138000','youremail@gmail.com','guangzhou') commit; select * from t_user;

相關文章
相關標籤/搜索