数据库约束条件

2021-06-07  本文已影响0人  GuoDJ

约束条件

主键约束

表中行的唯一性标识,可以由一个列或者多个列共同组成,但组成主键约束的所有列中元素不能为空。在同一表中,主键是唯一的。

(两个点,一个注意)

关键字:primary key

单字段主键约束

create table 表名(
    字段1  数据类型(长度) primary key,
    字段2  数据类型(长度)
    ....
)
create database bd1;
use db1;

create table pri(
  id int(10)primary key,
  name varchar(30),  
  age int(6)
);
提示:一旦给id添加了主键约束时,要必须为id 添加数据;
     通常主键约束设置给ID字段

案列验证:
insert into pri values(1,"tom",23);

错误演示:
1.添加 id 重复的数据
insert into pri values(1,"jack",25); //id被主键约束修饰,不能够重复添加
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'

错误演示:
2.添加一条没有id值得数据
insert into pri(name,age ) values("rose",17); //id被主键约束修饰,所有不能为null值
ERROR  :Field 'id' doesn't have a default value

错误演示:
3.创建一个多次出现主键约束的表格
create table pri2(
   id int(10)primary key,
   name varchar(12)primary key,
   age int(5)
);  //主键约束在表格中只能出现一次

组合字段主键约束

是为多个字段组合而成的主键,必须多条字段的值同时都不相同

create table 表名(
    字段1  数据类型(长度), 
    字段2  数据类型(长度),
    字段3  数据类型(长度),
    字段4  数据类型(长度),
    ....
    primary key(字段1,字段2...)
);
建立表格:
create table pri2(
  id int(10),
  name varchar(20),
  chines double(9,2),
  math double(9,2),
  primary key(id,name)
);
添加数据:
insert into pri2 values(1,"tom",65,89);

1.添加 id 重复 ,name不重复的数据
insert into pri2 values(1,"jack",78,45);  // 无错误,正常添加
select * from prie2; //查询验证

2.添加 name 重复,id 不重复的数据
insert into pri2 values(2,"jack",12,23); // 无错误,正常添加
select * from prie2; //查询验证
结果说明:在组合主键约束中,如果让其中的某一个字段发生重复现象的话,是可以通过的

3.添加  id 和 name 同时重复的数据
insert into pri2 values(2,"jack",85,96);//错误,id-name是组合主键约束
ERROR 1062 (23000): Duplicate entry '2-jack' for key 'PRIMARY'

4.添加  id 和 name 同时不重复的数据
insert into pri2 values(3,"roes",85,96);  //执行通过
select * from prie2; //查询验证

如果为某两个或多个字段设置了组合字段主键约束的话,必须它们共同组合起来不能重复,但是单个字段重复的话是可以存在或通过的。

  1. 关键字:primary key

  2. 保证字段数据的唯一性,不能重复;

  3. 同时保证字段的值不能为空值

  4. 组合主键约束约束必须多个字段同时不重复

  5. 一个表中主键约束只能出现一次

唯一性约束

create  table 表名(
  字段1  数据类型(长度), 
  字段2  数据类型(长度) unique,
  字段3  数据类型(长度) unique,
  ...
);

案例分析:

create table unique_test(
  id int(10) primary key,
  name varchar(20) unique,
  chines double(9,2),
  math double(9,2)
);

添加数据:
insert into unique_test values(1,"tom",65,89);

1.添加一条 name 重复的数据
insert into unique_test values(2,"tom",85,74);
ERROR 1062 (23000): Duplicate entry 'tom' for key 'name'
//name设置了唯一性约束,所以name值不能重复添加

2.添加一条 name 不重复的数据
insert into unique_test values(2,"jack",85,74); //语法正确,执行通过

3.添加一条name为null的值
insert into unique_test(id,chines,math) values(3,89,56);//语法正确,执行通过
说明唯一性约束字段的值可以为null

4.添加一条name=null 重复的数据
insert into unique_test(id,chines,math) values(4,12,45);//语法正确,执行通过
说明了null值是可以重复的

非空约束

字段名 数据类型(长度) not null

create  table 表名(
  字段1  数据类型(长度),
  字段2  数据类型(长度) not null,
  字段3  数据类型(长度) not null,
  ...
)
create table no_test(  
  id int(10) primary key,     //设置主键约束
  name varchar(20) unique,   //设置唯一性约束
  sex char(5) not null,    //设置非空约束
  address varchar(30)
);
desc no_test; //查看表的结构
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(10)     | NO   | PRI | NULL    |       |
| name    | varchar(20) | YES  | UNI | NULL    |       |
| sex     | char(5)     | NO   |     | NULL    |       |
| address | varchar(30) | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+

添加一条基础数据:
insert into no_test values(1,"tom","男","美国");

1.添加一条性别为空的数据
insert into no_test (id,name,address) values(2,"jack","德国");
//执行不通过,因为sex字段设置了非空约束
ERROR 1364 (HY000): Field 'sex' doesn't have a default value'

2.添加一条性别不为空的数据
insert into no_test (id,name,sex,address) values(3,"roes","女","英国");
//执行通过

默认值约束

create  table 表名(
  字段1  数据类型(长度),
  字段2  数据类型(长度) default ***,
  字段3  数据类型(长度) default ***,
  ...
)
create table def_test(
  id int(10) primary key,     
  name varchar(20) unique ,   
  sex char(5) not null,    
  address varchar(30) default "郑州"   
);
desc def_test;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(10)     | NO   | PRI | NULL    |       |
| name    | varchar(20) | YES  | UNI | NULL    |       |
| sex     | char(5)     | NO   |     | NULL    |       |
| address | varchar(30) | YES  |     | 郑州    |       |
+---------+-------------+------+-----+---------+-------+

1.验证添加一条没有地址的数据,看看默认是什么
insert into def_test (id,name,sex) values(1,"tom","男"); 
//执行通过,address字段会显示默认值:郑州
select * from  def_test;


2.验证没有设置非空约束,并且没有设置默认值得情况: 添加一条没有名字的数据,看看默认值是什么
insert into def_test(id,sex) values (2,"男");
select * from  def_test;

同时设置多个约束条件

书写格式:

create  table 表名(
  字段1  数据类型(长度) primary key ,
  字段2  数据类型(长度) not null default ***,
  字段3  数据类型(长度) unique  default ***,
  字段4  数据类型(长度) unique  not null,
  字段5  数据类型(长度) unique  not null  default ****,
  ...
)

案例分析:

create table test(
  id int(10)primary key,
  name varchar(20)unique not null,
  age int(5) not null default 20,
  address varchar(52) unique default "中国"
);
//可以为某个字段同时设置唯一约束,非空约束 默认值约束

添加数据:
insert into test (id,name,age,address )values(1,"tom",12,"洛阳");

1.验证name字段为null的情况
insert into test (id,age,address) values (2,20,"luoyang");//执行错误,因为默认值是null
Field 'name' doesn t have a default value
2.验证age字段的默认值是否为20
insert into test (id,name,address) values (2,"roes","shangqiu");

3.验证非空约束,是不是必须要给修饰的字段添加数据
insert into test (id,name,address )values(2,"jack","郑州");
// 在为非空约束的字段设置了默认值的情况下,是可以不为其添加数据的,此时显示的是默认值

4.验证address字段是否可以为重复
insert into test values(3,"jason",23,"zhengzhou");
// Duplicate entry 'zhengzhou' for key 'address'

5.验证address 的默认值是否为中国
insert into test (id,name,age) values(3,"jsaon",30);
//查看结果
select * from test;
+----+-------+-----+-----------+
| id | name  | age | address   |
+----+-------+-----+-----------+
|  1 | jack  |  18 | zhengzhou |
|  2 | roes  |  20 | shangqiu  |
|  3 | jsaon |  30 | china     |
+----+-------+-----+-----------+

6.再次验证address的默认值中国是否可以重复
insert into test (id,name,age) values(4,"mark",30); //结果为不能重复
// Duplicate entry 'china' for key 'address'
这里告诉我们,虽然字段可以设置多个约束条件,但是也要合理使用。
比如:
address字段既设置了唯一性约束,又设置了默认值约束,这样的情况就是属于不合理的使用约束条件。

字段值的自动增加

  1. 自动生成的数据一定的唯一,并且是非空的
  2. 为了保证数据的唯一性和独立性,在使用此功能时,建议同时使用主键约束 primary key

increment:增加

auto:自动的

字段名 整数数据类型(长度) primary key auto_increment 

案例分析:

create table auto_test(
  id int(20) primary key auto_increment,
  name varchar(20)
);
desc auto_test;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(20)     | NO   | PRI | NULL    | auto_increment |
| name  | varchar(20) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+


1.添加数据
验证 : auto_increment是否是从1 开始的
insert into auto_test (name)  values("tom");

2.添加数据
验证 : auto_increment是否完成自增,每次增加1
insert into auto_test (name)  values("jack");
上一篇 下一篇

猜你喜欢

热点阅读