oracle见表

1.默认是用当前时间

create table test
(id int,
time date default sysdate);

2.自动生成guid

 Oracle8i引入了SYS_GUID这个概念,它同Oracle管理员所使用的传统的序列(sequence)相比具有诸多优势。一个序列生成器只是简单地创建从给定的起点开始的一系列整数值,而且它被用在选择陈述式的时候自动地递增该系列。 

-根据数据库设计说明书创建表
--GOODS
create table goods1
(
gid number(11) primary key,
gname varchar2(20) not null unique,
gprice number(18,1) not null,
gnum number(11) not null
);


--实现GOODS1表中主键自动生成(需要使用序列和触发器)
--为GOODS1表创建序列:生成唯一一系列的数
create sequence goods_seq
start with 1     --从1开始
increment by 1  --每次增1
minvalue 1      --最小值
maxvalue 999999999999999   --最大值
nocycle           --不循环取数
cache 10;       --缓存


--为GOODS1表创建触发器,用于自动从序列取值给GOODS1表中GID赋值
create trigger goods_trigger
before insert on goods1         --触发条件:在向GOODS1表中插入数据之前自动触发此触发器
for each row                    --行级触发器:插入的每一行数据都会触发
begin
   select goods_seq.nextval into :NEW.GID from dual;
end;



--向GOODS1表中插入测试数据
INSERT INTO goods1(gname,gprice,gnum) VALUES('computer',3000,100);
INSERT INTO goods1(gname,gprice,gnum) VALUES('computer2',3500,100);
INSERT INTO goods1(gname,gprice,gnum) VALUES('iphone6',4000,200);
INSERT INTO goods1(gname,gprice,gnum) VALUES('iphone6s',5000,300);
commit;

 

posted @ 2017-11-16 17:03  zmoony  阅读(179)  评论(0编辑  收藏  举报