文章詳情頁(yè)
oracle中實(shí)現(xiàn)主鍵的自動(dòng)增加
瀏覽:7日期:2023-11-19 14:12:40
實(shí)現(xiàn)方法1: 建立一個(gè)最小為1,最大為nomaxvalue的一個(gè)序列號(hào)會(huì)自動(dòng)循環(huán)的序列create sequence 序列名 increment by 1 start with 1 nomaxvalue; nocycle;當(dāng)向表中插入數(shù)據(jù)時(shí),SQL語(yǔ)句寫法如下:SQL> insert into 表名 values(序列名.nextval,列1值,列2值, ...);當(dāng)要重用該序列號(hào)時(shí),有兩種方法:a.;在同一個(gè)sql塊中重用:SQL>insert into表名(序列名.currval, 列1值,列2值...);b.;在存儲(chǔ)進(jìn)程中,將該值取到一個(gè)參數(shù)中:SQL>select序列名.nextval into 參數(shù)名 from dual;然后在重用該序列號(hào)的地方調(diào)用這個(gè)參數(shù)。實(shí)現(xiàn)方法2:(利用觸發(fā)器)SQL> create sequence a_sequence 2; start with 1 3; increment by 1;序列已創(chuàng)建。SQL> create table t (n number ,v varchar2(10));表已創(chuàng)建。SQL> create or replace trigger t_trg 2; before insert or update on t 3; for each row 4; begin 5;select a_sequence.nextval into :new.n from dual; 6; end; 7; /觸發(fā)器已創(chuàng)建SQL> insert into t values(111,'ok');已創(chuàng)建 1 行。SQL> select *; from t; N V---------- ---------- 1 ok
標(biāo)簽:
Oracle
數(shù)據(jù)庫(kù)
排行榜
