Oracle ——数据完整性

Oracle ——数据完整性

为什么使用约束? 易于维护,并且效果最好

约束:约束是确保数据库满足特定商业的规则

约束有5种:[not null] 非空,[unique] 唯一,[primary key] 主键,[foreign key] 外键,[check] 检查

not null : 插入数据时,必须提供数据

unique : 该列数据值不能出现重复值,但可以为空NULL

primary key : 该列不但不能有重复的数据,也不能出现null,一张表中只能有一个主键,但是可以有多个 unique 唯一约束

foreign key : 定义外键约束后,主表和从表根据某个字段建立外键约束,从表的该字段的值必须符合主表某个字段范围。

check : 检查添加的数据是否满足所设的条件。

  1. 每个表的主外键
  2. 客户的姓名不能为空
  3. 单价必须大于0,购买数量必须在1-30之间
  4. 电邮不能重复
  5. 客户的性别必须是男或女,默认是男

建表语句:


--创建商品表
create table Goods(
  GoodsId number(10) primary key ,---主键约束
  GoodName varchar2(100) not null ,---当前列不能为空
  Untilprice number(5,2) check(Untilprice>0),---单价必须大于零
  Categroy varchar2(50) ,
  provider varchar2(100)
)

如何验证是否建表成功?添加一条数据来验证。验证信息如下:

在GOODS 具体表的Constraints选项中由错误代码(J10623.SYS_C009670)找到具体错误原因


---创建客户表
create table Customer(
   CustomerId char(8) primary key,
   Name varchar2(100) not null,
   Address varchar2(200),
   Email varchar2(100) unique,
   Gender char(2) default('男') check(Gender in('男','女')),--如果不设为非空,也可设置默认值给当前值。
   cardId char(18) 
)


---创建销售表
create table Purchases(
   CustomerId cahr(8) references Customer(CustomerId),--关联外键
   GoodsId number(10) references Goods(GoodsId),--关联外键
   Num number(10) check(Num between 1 and 30) 
)


---另外的添加约束的方法
create table newAddMethod(
  File_one char(1) not null,
  File_two char(2) not null,

  ---添加约束,将File_one设置为主键
  constraint pk_name primary key (File_one)
)

使用DDL语句添加修改约束

如果创建表的时候没有添加约束时,使用 alter table + [约束],如果需要添加 not null 需要使用modify 关键字,其他四种约束添加是使用add



---添加商品表中 GoodsId 不能为空
alter table Goods modify GoodsId not null;

---为客户表中的身份证不能重复的约束
alter table Customer add constraint UQ_CardId unique(cardId);

---添加客户地址时,只能在 广州、惠州、佛山、江门
alter table Customer add constraint CK_Address check(Address in('广州','惠州','佛山','江门'))


删除对应约束

aler table +[表名]+drop+constraint+约束名称


---第一种情况:无关联关系,即无外键
---删除 商品表 商品名称 非空的约束约束相应代码去表结构找
alter table Goods drop constraint SYS_C009685;

---第二种情况:有关联关系,有外键
---删除 商品表 GoodsId 主键约束,关联的外键 SYS_C009694 级联删除
alter table Goods drop constraint SYS_C009687 cascade

---级联删除之后,补回主键 外键
alter table Goods add constraint pk_GoodId primary key(GoodsId)

---补回外键
alter table Purchases add constraint FK_p_g foreign key(GoodsId) references Goods(GoodsId);

---主键自动增长 使用(序列 sequences)

CREATE SEQUENCE [name] INCREMENT BY [Recursive value] START WITH [start value] CACHE [buffer] ;

---级联删除[ ON DELETE CASCADE ] 删除,[ON DELETE SET NULL] 置空
---添加部门表
create table bumen(
 bumenid number(10) not null,
 bumenname varchar(10) not null,
 
 constraint ok_id primary key (bumenid)
);
---添加员工表
create table yuangong(
  yuangongid number(10) not null,
  yuangongname varchar2(10) not null,
  yuangongbumen number(10) not null
  
)

---添加外键关联
alter table yuangong add constraint  F_K_BUMEN foreign key (yuangongbumen) references bumen(bumenid) on delete cascade
---添加数据
insert into bumen (bumenid,bumenname) values (1,'销售部');
insert into bumen (bumenid,bumenname) values (2,'开发部' );

insert into yuangong(yuangongid,yuangongname,yuangongbumen)values(1,'a',2);
insert into yuangong(yuangongid,yuangongname,yuangongbumen)values(2,'b',1);
insert into yuangong(yuangongid,yuangongname,yuangongbumen)values(3,'c',1);
insert into yuangong(yuangongid,yuangongname,yuangongbumen)values(4,'d',2);

---删除销售部门
---结果显示员工表的所有在销售部门的员工的数据都被清除了,如果是[ON DELETE SET NULL] 置空模式的话,则全部被置空
delete from bumen where bumenid=1;

  1. 添加新序列

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值