|
|
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已! |
|
恭喜您是本站第 1729343
位訪客!
登入 | 註冊
|
|
|
|
發表者 |
討論內容 |
冷日 (冷日) |
發表時間:2019/7/29 13:46 |
- Webmaster

- 註冊日: 2008/2/19
- 來自:
- 發表數: 15771
|
- [轉貼]Oracle 如何做到 SQL Server 的 Identity 欄位型態
Oracle 如何做到 SQL Server 的 Identity 欄位型態在 DB table 規劃時,內部使用的 primary key 欄位常見用法分成兩派:使用 int 或是 GUID,在 SQL Server 上兩種做法都有對應的欄位型態,而在 Oracle 上就得要自行處理了,以 int 做法為例,在 SQL Server 中實作的欄位型態為 Identity ,是一種自動累加固定值的識別碼,馬上來看看該如何在 Oracle 上實作 Identity 囉 實作做法說明 - 依實際需求建立 table 及欄位,並指定 primary key 欄位為 number 型態
- 建立 oracle sequence (序列) 用來取號
- 建立 trigger 在 insert data 時從 sequence 取號塞值
1. 建立 table請依實際需求調整 table 欄位
需要 primary key 為 number(10, 0) 且 not null 我這邊使用 Id 為欄位名稱
指定 constraint 語法範例
create table UserProfile ( Id number(10, 0) not null, Name VARCHAR2(50) not null, BirthDay date not null, Addr VARCHAR2(250) not null, constraint PK_UserProfile primary key (Id) )
2. 建立 Oracle Sequence3. 建立 Trigger實際效果 輸入時指定 100  儲存 1  心得一直以來都是使用 SQL Server 從來沒想過原來其他 db 沒有 identity 欄位型態,本來也沒想到可以這麼做,是使用 code first 產生 pl-sql 時發現原來 oracle 的 identity 可以透過上述方式建立,特別筆記一下 參考資訊 原文出處:Oracle 如何做到 SQL Server 的 Identity 欄位型態 - Yowko's Notes
|
|
|
討論串
|