MySQL基础-3 数据完整性

数据完整性

作用:保证用户输入的数据保存到数据库中是正确的
实质:创建表的时候给表中的字段添加约束

1.实体完整性

实体:表中的一行或者一条记录代表一个实体
实体完整性的作用:标识每一行数据不重复
约束类型:

  • 主键约束【primary key】
  • 唯一约束【unique】
  • 自动增长列【auto_increment】

1.1主键约束

特点:数据唯一,且不能为null
主关键字可以是表中的一个字段或者多个字段,它的值用来唯一标识表中的某一条记
场景:在多个表的关联关系中
演示:
创建一个学生表id作为主键
直接在你想设为主键的字段后面加上primary key

create table student(id int primary key,name varchar(20));

也可以在表的最后加上

create table student(id int,name varchar(20),primary key(id));

要是建表的时候忘了添加主键,也可以通过下面这种方式添加(stu_id只是起的一个名字)

alter table student add constraint stu_id primary key(id);

1.2唯一约束

作用:在非主键列中不能输入重复的值
演示:
创建一个student表,让名字唯一

create table student(id int primary key,name varchar(30) unique);

primary key和unique之间的区别

  • 二者都强调的是唯一性
  • .在同一个表中,一般只出现一个primary key,可以出现多个unique
  • primary key不允许为null,但是unique是允许的

1.3自动增长列

给主键添加添加自动增长性,列只能是整数类型
场景:一般添加给主键
演示:

create table student(id int primary key auto_increment,name varchar(30));

2.域完整性

作用:限制单元格数据的正确性,域代表当前单元格

约束类型:

  • 数据类型
  • 非空约束【not null】
  • 默认值约束【default】

2.1数据类型

数字类型:int float double
日期类型:date datetime
字符串类型:varchar(20)

2.2非空约束

演示:

create table student(id int primary key auto_increment,name varchar(30) not null);

插入数据的时候name不能为空,否者会报错

2.3默认值约束

演示:

create table student(id int primary key auto_increment,name varchar(30) not null,age int default 18);

3.外键约束

添加外键约束:foreign key
注意:添加外键必须先有主键,主键和外键的类型必须保持一致
举例:学生表,成绩表
作用:将两个甚至多个表产生联系
演示:
第一种方式:直接在建表的时候添加外键约束(stu_score_id是给约束起的名字,可以自定义)

mysql> create table student(stuid int primary key,stuname varchar(30));
Query OK, 0 rows affected (0.01 sec)

mysql> create table score(stuid int,
    -> subject varchar(20),
    -> score int,
    -> constraint stu_score_id foreign key(stuid) references student(stuid));
Query OK, 0 rows affected (0.01 sec)

第二种就是在创建完表之后,再添加外键

mysql> create table score(stuid int,
    -> subject varchar(20),
    -> score int);
Query OK, 0 rows affected (0.01 sec)
  
mysql> alter table score add constraint stu_socre_id foreign key(stuid) references student(stuid);
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

外键必须和主键保持一致,主键有的外键才能有,还有就是主键和外键的类型必须保持一致

mysql> insert into student values(2101,'张三'),(2102,'李四'),(2103,'王五'),(2104,'赵六');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> insert into score values(2101,'语文',88),(2102,'语文',70),(2101,'数学',90),(2103,'英语',60),(2104,'数学',10),(2104,'英语',60);
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select * from student;
+-------+---------+
| stuid | stuname |
+-------+---------+
|  2101 | 张三    |
|  2102 | 李四    |
|  2103 | 王五    |
|  2104 | 赵六    |
+-------+---------+
4 rows in set (0.00 sec)

mysql> select * from score;
+-------+---------+-------+
| stuid | subject | score |
+-------+---------+-------+
|  2101 | 语文    |    88 |
|  2102 | 语文    |    70 |
|  2101 | 数学    |    90 |
|  2103 | 英语    |    60 |
|  2104 | 数学    |    10 |
|  2104 | 英语    |    60 |
+-------+---------+-------+
6 rows in set (0.02 sec)

mysql> insert into score(2105,'语文',65);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2105,'语文',65)' at line 1

主键和外键的名字可以不一样,这里只是便于观察

猜你喜欢

转载自blog.csdn.net/hmh4640219/article/details/113979685