資料完整性
作用:保證用戶輸入的資料保存到資料庫中是正確的
實質:創建表的時候給表中的欄位添加約束
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
主鍵和外鍵的名字可以不一樣,這里只是便于觀察
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/263336.html
標籤:其他
上一篇:sql防注入
下一篇:MongoDB基本命令及使用
