當前位置:
首頁 > 知識 > oracle插入時如何自動生成主鍵

oracle插入時如何自動生成主鍵

oracle中自動生成主鍵方式例子如下:

1)先創建表:

create table student(

sno int not null,

sname
varchar(20),

sex char(4),

constraint PK_SNO
primary key(sno) );

2)創建序列:

create sequence seq_student --序列名

start with
1 --初始化為1

increment
by
1 --每次增加1

maxvalue
99999 --最大值

nocache --無緩存

nocycle --無循環,一直累加

3)這個時候插入數據:insert into student(sno,sname,sex)
values(seq_student.nextval,"shang","男"),如果還想插入數據時主鍵自動加入則需要創建觸發器:

create or replace trigger tri_student

before insert on student for each
row

declare

begin

if :new.sno is null or :new.sno=0
then

select seq_student.nextval into :new.sno from
sys.dual; --seq_student為上面建的序列

end
if;

end tri_studnet;(此中的if語句可以去掉)

4)測試語句:insert into student(sname,sex) values("test,"男");

如果報「觸發器無效且未通過重新驗證」可能是觸發器建的有問題

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 笑月天狼 的精彩文章:

Java垃圾回收
Mybatis框架簡單介紹第一篇

TAG:笑月天狼 |

您可能感興趣

rapid-generator自動生成代碼步驟
Mybatis+velocity自動生成代碼
Python自動生成表情包
還在做重複的勞動,不如用Python自動生成Excel以郵件發送
centos7重啟後sshd服務不能自動拉起
Alberto Rodriguez:我們離自動靈巧機器人有多遠?
Perceptive Automata加入Renovo的Aware生態系統 為自動駕駛車輛預測人類行為
自定義Android註解Part2:代碼自動生成
macOS 效率進階,學習如何用 AppleScript 實現自動化
webSocket如何解決自動關閉的意思
Docker下使用selenium+testng實現web自動化
WPS Excel 目錄怎麼自動生成
TensorFlow官方力推、GitHub爆款項目:用Attention模型自動生成圖像字幕
自動駕駛的棘手難題:how safe is safe enough
Linux上監控Tomcat down掉後自動重啟Tomcat
Ford整合自動駕駛業務與資源,成立Ford Autonomous Vehicles
Starsky Robotics能憑攝像頭玩轉自動駕駛嗎?
用Python Featuretools庫實現自動化特徵工程
用 Python Featuretools 庫實現自動化特徵工程
Swiss Advanced Vision上市用於白內障手術的實時自動對焦主動式人工晶體概念