2018.5.20 oracle强化练习
--现在有一个商店的数据库,记录客户以及购物的情况,
商品表goods
(商品号 goodsid varchar2(8)
商品名 goodsname varchar2(20)
单价 unitprice number(5,2)
商品的类别 category varchar2(8)
供应商 provider varchar2(20))
create table goods(
goodid varchar2(8),
goodname varchar2(20),
unitprice number(5,2),
category varchar2(8),
provider varchar2(20)
);
客户表customer
customerid varchar2(8)
name varchar2(20)
address varchar2(30)
email varchar2(30)
sex varchar2(8)
create table customer(
customerid varchar2(8),
name varchar2(20),
address varchar2(30),
email varchar2(30),
sex varchar2(8)
);
购买表purchase
客户号 customerid varchar2(8)
商品号 goodsid varchar2(20)
购买数量 nums number(2)
create table purchase(
customerid varchar2(8),
goodsid varchar2(20),
nums number(2)
);
用sql语句创建三张表
1、定义每个表的主、外键
alter table customer add constraint pk_customerid primary key(customerid);
alter table goods add constraint pk_goods_goodid primary key(goodid);
alter table purchase add constraint fk_purchase_customerid foreign key(customerid) references customer(customerid) on delete cascade;
alter table purchase add constraint fk_purchase_goodsid foreign key(goodsid) references goods(goodid) on delete cascade;
2、客户的姓名不能为空
alter table customer modify (name not null);
3、单价必须大于0,购买数量必须在1到30之间
alter table goods add constraint ck_unitPrice check(unitprice>0);
alter table purchase add constraint ck_nums check(nums between 1 and 30);
4、电邮不能重复
alter table customer add constraint un_email unique (email);
5、客户的性别必须是female或者male,默认是male
alter table customer modify (sex in('female','male') default 'male');
alter table customer modify(sex varchar2(8) default 'male' );
alter table customer add constraint ck_sex check(sex='female' or sex='male' );
--chakan yueshu
select constraint_name,table_name from user_constraints where table_name =upper('goods');
select constraint_name,table_name from user_constraints where table_name =upper('customer');
select constraint_name,table_name from user_constraints where table_name =upper('purchase ');