1.給表自增序列
1--創建測試表 create table student_test ( student_id number(8) not null primary key, --主鍵列 first_name varchar2(50), -- 名字 last_name varchar2(50) -- 姓 ); 2--創建序列 create sequence seq_student_test minvalue 1 --最小值 nomaxvalue --最大值 start with 1 --起始值 increment by 1 --增長基數 nocycle --不循環,一直增加 nocache ; 3 --刪除觸發器 DROP TRIGGER tri_student_ins 4 --創建序列后插入 insert into student_test(student_id,first_name,last_name) values(seq_student_test.nextval,'',''); 5--創建觸發器 create trigger tri_student_ins before insert on student_test for each row when (new.student_id is null) begin select seq_student_test.nextval into:new.student_id from dual; end; 6--序列插入 insert into student_test(first_name,last_name) values('',''); 7--循環插入代碼 declare i number := 1; v_first_name varchar2(50); v_last_name varchar2(50); begin for i in 1 .. 200 loop v_first_name := '' || to_char(i); v_last_name := '' || to_char(i); insert into student(first_name,last_name) values(v_first_name,v_last_name); commit; end loop; end;
2.創建觸發器,相關測試數據
CREATE TABLE SEOrder( FInterId number(8) not null primary key , FBillNo VARCHAR(50) NOT NULL, FBillerID INT DEFAULT(0), FDepart INT NOT NULL ) --alter table SEOrder add constraint SEOrder_PK primary key (FInterId); ALTER TABLE SEOrder ADD FDate VARCHAR(50) ALTER TABLE SEOrder DROP COLUMN KKK; ALTER TABLE SEOrder MODIFY (FDate DATE ); alter table SEOrder drop column KKK; drop sequence SEOrder_Id_sequence; drop trigger SEOrder_Id_autoincrement; --DROP sequence SEOrder_FInterId_sequence create sequence seq_student_test minvalue 1 --最小值 nomaxvalue --最大值 start with 1 --起始值 increment by 1 --增長基數 nocycle --不循環,一直增加 nocache ; DROP sequence SEOrder_FInterId_sequence create sequence SEOrder_FInterId_sequence minvalue 1 --最小值 nomaxvalue --最大值 start with 1 --起始值 increment by 1 --增長基數 nocycle --不循環,一直增加 nocache ; create trigger SEOrder_FInterId_autoincrement before insert ON SEOrder for each row when (new.FInterId is null) begin select SEOrder_FInterId_sequence.nextval into:new.FInterId from dual; end; DROP trigger SEOrder_FInterId_autoincrement grant select, insert, update, delete, references, alter, index on SEOrder to PUBLIC; ALTER TRIGGER SEOrder_FInterId_autoincrement COMPILE ; DROP TABLE SEOrder; -- Create table DROP table SEORDER create table SEORDER ( finterid NUMBER(8) not null, fbillno VARCHAR2(50) not null, fbillerid NUMBER(5) not null, fdepart NUMBER(5) not null, fdate DATE ) tablespace DBS1 pctfree 10 initrans 1 maxtrans 255 storage ( initial 64K next 1M minextents 1 maxextents unlimited ); -- Create/Recreate primary, unique and foreign key constraints alter table SEORDER add primary key (FINTERID) ALTER TRIGGER SEOrder_FInterId_autoincrement DISABLE; CREATE TABLE SEOrder ( FInterId INT , FBillNo VARCHAR(50) NOT NULL, FBillerID INT DEFAULT(0), FDate DATE, FDepart INT NOT NULL ) INSERT INTO SEOrder VALUES(SEOrder_FInterId_sequence.nextval,'XM201501' , '1' , '2015-1-22' , '1' ) INSERT INTO SEOrder VALUES(1,'XM201502' , '1' , '2015-1-22' , '1' ) INSERT INTO SEOrder (FBillNo , FBillerID ,FDate , FDepart) VALUES('XM201503' ,'2' ,to_date('2015-1-22','yyyy-mm-dd') , '2' ) alter table SEOrder add FDate DATE; SELECT * FROM SEOrder INSERT INTO SEOrder(FInterId,FBillNo , FBillerID,FDate , FDepart) VALUES(SEOrder_FInterId_sequence.nextval,'1' , '1' ,sysdate, '2' )