SQL DDl_约束条件

SQL约束

约束:
是在表上强制执行的一些数据校验规则,被插入/修改/删除的数据必须符合在相关字段上设置的这些约束条件

  1. 非空约束
    非空 限定某个字段不能为空
    create table 表名(
    字段 字段类型 not null
    );
    create table testNull(
    id number(3) not null,
    name varchar2(10)
    );
    在这里插入图片描述
    2.默认值的约束
    如果某个字段没有值 可以设置一个默认值
    create table teseDefault(
    id number(10) not null,
    name varchar2(10) default ‘琦玉’,
    age number(2) default 18
    );
    在这里插入图片描述

  2. 唯一约束
    唯一约束: 表示某个字段值在表中是唯一标识
    create table testUnique(
    id number(5) unique not null,
    name varchar(10) default ‘老王你’
    );
    注意:
    1.如果2个数据不为空 唯一约束不相等 (如果都为空数据是 可以插入)
    2. 一个字段可以加入多个约束
    在这里插入图片描述
    4.主键约束 unique + not null
    表示是 设定的字段 是唯一约束不能为空
    主键是唯一标识符 (加快查询效率 自动创建索引)
    create table testPrimary(
    id number(4) primary key,
    name varchar2(10) default ‘老宋’
    );

    联合主键:
    create table testKey(
    id number(3),
    name varchar2(20)
    );
    – 创建表后给testKey 添加唯一约束
    alter table 表名 add constraint 主键标识符 primary key(字段名);
    alter table testKey add constraint pk_key primary key (id);
    – 多个字段联合起来组成主键
    alter table testKey add constraint pk_key primary key (id,name);

注意:
1.主键是唯一标识 一张表 只能有一个主键
2. 多个字段联合为一个联合主键
3. 一张表可有多个 unique + not null 但是只能有一个主键


5.外键约束:
外键是从表中重复出现,在主表中作为主键存在 外键关联2张表
主表: 主键的表 一方
从表:外键的表 多方

 多方关联一方 多方设置外键 
  班级  主表   一方      员工 多方  
  学生  从表  多方       部门 一方
   
 java中的对象模型:
 
 class Student{
     private int sid;
     private String name;
     private int ClassId;
       
 }   
 
 class Calss(
   private int Classid;
   private String className;
 );

数据库中的模型:
   方法一:
      -- 主表

create table t_class(
cid number(4) primary key,
cname varchar2(10)
);
– 从表
create table t_stu(
sid number(3),
sname varchar2(20),
classId number(4),
constraint fk_stu_class foreign key (classId) references t_class(cid)
);

– constraint fk_stu_class foreign key (classId) 把 字段 classId声明为外键 references t_class(cid)关联其他表的字段

方法二:
– 主表
create table t_class(
cid number(4) primary key,
cname varchar2(10)
);
– 从表
create table t_stu(
sid number(3),
sname varchar2(20),
classId number(4)
);
– 创建表完成后添加外键
alter table t_stu add constraint fk_stu_class foreign key(classId) references t_class(cid);

外键的特点:
1. 外键可以在从表中重复出现 ,他在主表中是主键
2. 一张表可以有多个外键
3. 外键可以为null
4. 外键的作用是关联多种表,可以建立表之间的关系 (一对一 一对多 多对多)
5. 删除主表时候 如果 从表有使用主表的数据 是不能删除的, 从表的数据可以直接删除
6. 2表关联了 先删除 从表 在删除主表
7. 关键是多表查询单 必要条件

表的关系:
  一对一     老公--- 老婆
  一对多     部门--- 员工
  多对多     学生----老师 

**6.check约束:**
   某个字段可以在一定的设置范围存储
   
   create table t_stu(
       sid number(5) primary key,
       sname varchar2(20),
   --  sage number(2) check(sage>20)     
    -- sage number(2) check(sage = 18 or sage=20)   
       sage number(2) check(sage in (18,19,20))
   );
  
check 约束可以给字段加运算符 和一些关键字: 
   > < = 
   and or 
   in / not in 
   is null / is not null 
建表以后如何添加check约束 
alter table t_stu add constraint ck_stu_age check(sage in (18,19,20));
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值