标签:重复 mys ted aci mil cin create text ade
约束条件 |
说明 |
PRIMARYKEY |
主键约束,用于唯一标识对应的记录 |
FOREIGNKEY |
外键约束 |
NOTNULL |
非空约束 |
UNIQUE |
唯一性约束 |
DEFAULT |
默认值约束,用于设置字段的默认值 |
单字段主键
单字段主键是指由一个字段构成的主键
字段名 数据类型 PRIMARY KEY,
mysql> CREATE TABLE tb_1
-> (
-> id INT PRIMARY KEY,
-> name CHAR(20),
-> grade FLOAT
-> );
Query OK, 0 rows affected
查看是否成功
mysql> DESC tb_1;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| name | char(20) | YES | | NULL | |
| grade | float | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set
多字段主键
多字段主键是指多个字段组合而成的主键
PRIMARY KEY (字段1,字段2)
mysql> CREATE TABLE tb_2
-> (
-> id INT (20),
-> stu_id INT (20),
-> name CHAR,
-> PRIMARY KEY (id,stu_id)
-> );
Query OK, 0 rows affected
查看是否成功
mysql> DESC tb_2;
+--------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| id | int(20) | NO | PRI | 0 | |
| stu_id | int(20) | NO | PRI | 0 | |
| name | char(1) | YES | | NULL | |
+--------+---------+------+-----+---------+-------+
3 rows in set
注意:每个数据表中最多只能有一个主键约束,定义为PRIMARY KEY的字段不能有重复值且不能为NULL值
非空约束
非空约束指的是字段的值不能为NULL
字段名 数据类型 NOT NULL,
mysql> CREATE TABLE tb_3
-> (id INT (20) PRIMARY KEY,
-> name CHAR(20) NOT NULL,
-> grade FLOAT
-> );
Query OK, 0 rows affected
查看是否成功
mysql> DESC tb_3;
+-------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| id | int(20) | NO | PRI | NULL | |
| name | char(20) | NO | | NULL | |
| grade | float | YES | | NULL | |
+-------+----------+------+-----+---------+-------+
3 rows in set
注意:在同一个数据表中可以定义多个非空字段
唯一约束
唯一约束用于保证数据表中字段的唯一性,即表中字段的值不能重复出现
字段名 数据类型 UNIQUE,
mysql> CREATE TABLE tb_4
-> (
-> id INT PRIMARY KEY,
-> stu_id INT UNIQUE,
-> name CHAR(20) NOT NULL
-> );
Query OK, 0 rows affected
查看是否成功
mysql> DESC tb_4;
+--------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| stu_id | int(11) | YES | UNI | NULL | |
| name | char(20) | NO | | NULL | |
+--------+----------+------+-----+---------+-------+
3 rows in set
默认约束
默认约束用于给数据表中的字段指定默认值,即当在表中插入一条新纪录时,如果没有给这个字段赋值,那么,数据库系统会自动为这个字段插入默认值。
字段名 数据类型 DEFAULT 默认值
mysql> CREATE TABLE tb_5
-> ( id INT PRIMARY KEY AUTO_INCREMENT,
-> stu_id INT UNIQUE,
-> grade FLOAT DEFAULT 0
-> );
Query OK, 0 rows affected
查看是否成功
mysql> DESC tb_5;
+--------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| stu_id | int(11) | YES | UNI | NULL | |
| grade | float | YES | | 0 | |
+--------+---------+------+-----+---------+----------------+
3 rows in set
表的字段值自动增加
AUTO_INCREMENT可以是任何整数类型,默认情况下,该字段的值是从1开始自增的
字段名 数据类型 PRIMARY KEY AUTO_INCREMENT,
mysql> CREATE TABLE tb_6
-> ( id INT PRIMARY KEY AUTO_INCREMENT,
-> stu_id INT(20) UNIQUE,
-> grade FLOAT
-> );
Query OK, 0 rows affected
查看是否成功
mysql> DESC tb_6;
+--------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| stu_id | int(20) | YES | UNI | NULL | |
| grade | float | YES | | NULL | |
+--------+---------+------+-----+---------+----------------+
3 rows in set
标签:重复 mys ted aci mil cin create text ade
原文地址:https://www.cnblogs.com/sxmzs/p/11748134.html