pl/sql 编程(二)

1.创建表以及各种约束

drop table bookinfo;
create table bookinfo(
bookid int primary key,                                      --主键约束
bookname varchar2(100) not null,                    --非空约束
publish varchar2(100),
price decimal,
remark varchar2(4000) default '备注信息',       --默认值
constraint un_bookname  unique (bookname), --唯一性约束
constraint ck_price check ( price <=100)         --检查约束
);

drop table salerecord;
create table salerecord (
sid int,
bookid int,
consumerid int,
num  int,
constraint primary_keys primary key(sid,bookid,consumerid),                 --联合主键
constraint foreign_key foreign key(bookid) references bookinfo(bookid) --外键约束
);

 2.修改表

alter table bookinfo modify publish varchar2(200) not null;                            --修改列 类型 是否非空

alter table bookinfo drop column remark;                                                     --删除列

alter table bookinfo add  remark varchar2(200) default '备注信息';               --增加列

alter table bookinfo add constraint ck_publish check(publish in ('a','b','c')) ; --增加约束

alter table bookinfo drop constraint ck_publish ;                                           --删除约束

--修改表中的约束即先删除再增加

rename  bookinfo to newbookinfo;                                                                 --表的重命名

3.删除表

drop table bookinfo                                                                                      --删除表

你可能感兴趣的:(pl/sql)