SQL语句——约束

约束

  • 一、约束
    • 1.什么是约束
    • 2.约束的作用
  • 二、常见的约束
    • 1.非空约束(not null)
      • (1)非空约束测试
    • 2.唯一性约束(unique)
      • (1)一个字段有唯一性约束
      • (2)两个字段联合起来具有唯一性
      • (3)not null 和 unique 联合
    • 3.主键约束(primary key)PK
      • (1)单一主键
      • (2)复合主键
      • (3)自然主键和业务主键
      • (4)auto_increment
    • 4.外键约束(foreign key)FK
    • 5.检查约束(check) mysql不支持,oracle支持

一、约束

1.什么是约束

在创建表的时候,我们可以给表中的字段加上一些约束,来保证这个表中的数据的完整性、有效性

2.约束的作用

约束的作用是为了保证表中数据有效

二、常见的约束

1.非空约束(not null)

非空约束not null约束的字段不能为NULL,:在字段后边加上 not null
插曲xxxx.sql 这种sql脚本文件,可以在mysql中批量执行,使用 source 绝对路径 执行。

(1)非空约束测试

    //创建一个表
    create table t_vip(
       id int,
       name varchar(255) not null
    );
    //正常插入是好的
    insert into t_vip(id,name) values(1,'jack');
    
    //如果不给name插入值,则会报错
    insert into t_vip(id) values(2);
    //错误如下:
    ERROR 1364 (HY000): Field 'name' doesn't have a default value

not null 只有列级约束,没有表级约束

2.唯一性约束(unique)

约束的字段不能重复,但是可以为NULL

(1)一个字段有唯一性约束

列级约束:约束直接添加到列后边

     //创建一个表
     create table t_vip(
       id int,
       name varchar(255) unique,
       email varchar(255)
     );
     //插入数据
      insert into t_vip(id,name,email) values(1,'jack','[email protected]');
     
     //再次插入相同name会报错
      insert into t_vip(id,name,email) values(1,'jack','[email protected]');
     //如下:
     ERROR 1062 (23000): Duplicate entry 'jack' for key 'name'    

(2)两个字段联合起来具有唯一性

表级约束:约束没有添加到列后边,给多个字段联合使用

     create table t_vip(
       id int,
       name varchar(255),
       email varchar(255)unique(name,email)
     );

(3)not null 和 unique 联合

在 mysql 中,如果一个字段同时被 not null 和 unique 约束的话,该字段自动变成主键字段

3.主键约束(primary key)PK

(1)主键约束
(2)主键字段:主键约束的字段
(3)主键值:主键字段里的值,建议使用 int bigint char 等类型,不建议使用 varchar 来做主键,主键值一般是定长
(4)主键:唯一且非空

(1)单一主键

列级约束

     create table t_vip(
       id int primary key,
       name varchar(255,
       email varchar(255)       
     );

表级约束

     create table t_vip(
       id int,
       name varchar(255),
       email varchar(255),
       primary key (id)
     );

(2)复合主键

复合主键是几个字段联合起来做一个主键,而不是每个都是主键
实际开发中不建议使用复合主键

     create table t_vip(
       id int,
       name varchar(255),
       email varchar(255),
       primary key (id,name)
     );

(3)自然主键和业务主键

自然主键:主键值是一个自然数,和业务没有关系
业务主键:主键值和业务紧密关联,例如拿银行卡号做主键值
实际开发中,自然主键用的多

(4)auto_increment

auto_increment 表示自增,从1开始,每次加1

   create table t_person(
     id int primary key auto_increment,
     name varchar(255)
   );
   insert into t_person(name) values('lisi');
   insert into t_person(name) values('zhangsan');
   insert into t_person(name) values('wangmazi');
   select * from t_person;

结果如下:
SQL语句——约束_第1张图片

4.外键约束(foreign key)FK

     create table t_class(
       classno int primary key,
       classname varchar(255)
     );
    
     create table t_student(
       no int primary key auto_increment,
       name varchar(255),
       cno int,
       foreign key(cno) references t_class(classno)
     );
    
     insert into t_class(classno,classname) values(100,'firstclass');
     insert into t_class(classno,classname) values(101,'secondclass');
     
     insert into t_student(name,cno) values('jack',100);
     insert into t_student(name,cno) values('tom',100);
     insert into t_student(name,cno) values('mark',101);
    

5.检查约束(check) mysql不支持,oracle支持

你可能感兴趣的:(sql,mysql,数据库)