建表及约束

1、商品表goods:
create table goods (
goodsID char(8) primary key, --主键
goodsName varchar2(30),
unitprice number(10,2) check(unitprice > 0),
category varchar2(8),
provider varchar2(30)
)

2、客户表costomer :
create table customer (
customerID char(8) primary key, --主键
name varchar2(50) not null, --不能为空
address varchar2(50),
email varchar2(50) unique, --不能重复
sex char(2) default '男' check(sex in('男','女')),
cardID char(18)
)

3、购买表purchase :
create table purchase (
customerID char(8) references customer(customerID ),--指向customer表的customerID
goodsID char(8) references goods(goodsID),
nums number(10) check(nums between 1 and 30)
)


增加约束,注:增加not null约束需要使用modify,其他约束用add
1、增加商店表中的商品名不能为空
alter table goods modify goodsName not null;

2、增加身份证不能重复约束(constraint 约束关键字 aa为约束名)
alter table customer add constraint aa unique(cardID);

3、增加地址只能在广州或深圳
alter table customer add constraint bb check(address in('广州','深圳'))



删除约束请看附件





猜你喜欢

转载自jonny-java.iteye.com/blog/2235504