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 ');

转载于:https://www.cnblogs.com/qichunlin/p/9062281.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值