(1)增加商品名不能为空
alter table goods modify goodsname not null;
(2)增加身份证不能重复
alter table customer add constraint uni_id unique (cardId)
(3)增加客户住址只能为‘海淀’,‘朝阳’
alter table customer add constraint ch_address check(address) in ('海淀','朝阳');
(4)alter table ** add constraint 约束名 约束种类【check/unique/primary key/foreign key 】字段
alter table ** modify 字段名 not null;
(5)alter table ** drop constraint 约束名称
再删除主键约束的时候,使用alter table 表名 drop primary key可能会出现错误,如果两张表存在主从关系,再删除主表
的主键约束时,必须带上cascade选项
alter table ** drop primary key cascade;
(6)触及定义:在定义表之后,直接在列后定义约束
表级定义在列写完后,再分别说明约束
create table user8(id number,
name varchar2(40),
constraint pk_id primary key id);
constraint 约束名primary key(not null只能在列级上定义)
constraint 约束名foreign key references 鼠标名(字段)
例:
维护数据库完整性,记录客户及其购物情况
- goods(goods_id,goods_name,unitprice,category,provider)
- customer(customer_id,name,address,email,sex,card_id)
- purchase(customer_id,goods_idnums)
每个表的主外键,客户的姓名不能为空,单价必须大于0,购买数量在1--30,点又不能重复,性别男/女 默认为男
- create table goods(
- goods_id number primary key,
- goods_name varchar2(36),
- unitprice number check(unitprice>0),
- category varchar2(64),
- procedure varchar2(64));
- create table customer(
- customer_id number primary key,
- name varchar2(32) not null,
- address varchar2(64),
- email varchar2(64) unique,
- sex char(2) check(sex in('男','女'))default '男',
- cardid varchar2(20))
- create table purchase(
- constanerid number references customer(customerid),
- goos_id number references goods(goods_id),
- nums number check(nums>=1 and nums<=30));
如果坚表示忘记必要的约束,可以在建表后使用 alter table 命令为表增加约束,
增加 not null约束,使用modify选项
增加其他4中用add选项
浙公网安备 33010602011771号