Oracle中表字段的自動加1

2021-04-13 02:06:02 字數 687 閱讀 2891

實現表字段的自動加1有兩種思路:都會用到序列sequence。

首先,準備乙個table。 create table t_test ( id number,info varchar2(10));

其次,建立乙個sequence。create sequence seq_test_id increment by 1start with 1 maxvalue 999999999;

方法一:在使用sql語句進行插入的時候,使用sequence的值。

insert into t_test (id,info) values ( seq_test_id.nextval, 'test information');

方法二:使用觸發器,在使用者使用insert語句的時候出發事件使得插入的值中目標鍵值(id)為seq_test_id.nextval。

建立觸發器的**如下:

create or replace trigger tri_bef_ins_t_test

before insert on t_test

referencing old as old new as new for each row

begin 

select   seq_test_id.nextval   into   :new.id   from  dual; 

end; 

/在程式中直接插入資料即可。

hibernate使用資料庫中表字段的預設值

hibernate允許我們在對映檔案裡控制insert和update語句的內容.比如在對映檔案中1 元素 insert屬性 設定為false,在insert語句中不包含這個字段,表示永遠不會被插入,預設true 2 元素 update屬性 設定為false,在update語句中不包含這個字段,表示永...

ORACLE獲取表字段 注釋的方法

獲取表字段 select from user tab columns where table name 使用者表 order by column name 獲取表注釋 select from user tab comments where table name 使用者表 order by table...

oracle之修改表字段的sql

用慣了pl sql開發,那些基礎的sql語句都忘記不少了。比如這個修改表字段 alter table patrol lines powercut modify powercut cause varchar2 400 task planning varchar2 400 powercut desc v...