博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Oracle创建表,id为自增序列
阅读量:7038 次
发布时间:2019-06-28

本文共 1170 字,大约阅读时间需要 3 分钟。

hot3.png

1.创建表

create table my_table (

 id number(10) constraint pk_id primary key,

 name varchar2(20) not null,
 phone_Number varchar2(20) constraint unique_phone_number unique,
 email_Address varchar2(200) constraint email_not_null not null,
 home_Address varchar2(200) constraint home_addr_not_null not null
)
#查看约束 
select * from user_constraints;

 

2.创建序列

create sequence my_table_seq start with 1 increment by 1;

#查看序列
select * from user_sequences;

#删除序列

 DROP SEQUENCE my_table_seq;

3.创建触发器

create or replace trigger bi_my_table 

before insert on my_table
for each row
when(new.id is null)
begin
  select my_table_seq.nextval into:NEW.ID from dual;
end;

或者

CREATE OR REPLACE TRIGGER  "bi_my_table"   before insert on "my_table"                for each row begin    if :new."ID" is null then    select "my_table_seq".nextval into :new."ID" from sys.dual;  end if;end;
ALTER TRIGGER  "bi_my_table" ENABLE

#查看触发器

select * from user_triggers;

#删除触发器

DROP TRIGGER "bi_my_table" ;

#测试

insert into my_table(name,phone_number,email_address,home_address) values('zcl','13800138000','youremail@gmail.com','guangzhou')

commit;
select * from t_user;

转载于:https://my.oschina.net/youfen/blog/1924489

你可能感兴趣的文章
网站出问题了
查看>>
Linux 工具,一本好书 大牛的博客
查看>>
CentOS 7 关闭图形界面
查看>>
GDAL创建图像提示Driver xxx does not support XXX creation option的原因
查看>>
UVA213 UVALive5152 Message Decoding
查看>>
HDU1370 Biorhythms【中国剩余定理】
查看>>
谈一谈“九阴真经”
查看>>
Netty入门教程:Netty拆包粘包技术讲解
查看>>
关于修改bug的思考
查看>>
国内阿里云Maven镜像(速度飞起)
查看>>
数组的一些操作
查看>>
Microsoft CRM 2013 设置默认组织 default organization
查看>>
【理论基础】ContentProvider的简要概述
查看>>
加快某云下载速度。。。
查看>>
【LeetCode】169 - Majority Element
查看>>
爱上MVC3系列~改变Areas的FindView顺序
查看>>
Where is the warnings view in Android Studio?
查看>>
pycharm中的flask项目如何开启debug模式
查看>>
SpringMVC 利用@ResponseBody注解返回Json时,出现406 not acceptable 错误的解决方法。
查看>>
成为Java GC专家(5)—Java性能调优原则
查看>>