MYSQL笔记:约束条件
主键约束
不能为空,值必须是不同的(唯一性)
一个表只能修饰一个主键
PRIMARY KEY
自增约束
AUTO_INCREMENT
唯一键约束
可以为空
unique
非空约束
not null
默认值约束
default
外键约束
foreign key
外键,存储函数,存储过程,触发器,在后端用的不多,限制逻辑代码逻辑是由mysql本身控制的。
例子
CREATE TABLE user(
id INT unsigned PRIMARY KEY AUTO_INCREMENT COMMENT ' 用户id',
nickname varchar(50) UNIQUE NOT NULL COMMENT'用户名称',
age TINYINT UNSIGNED NOT NULL DEFAULT 18,
sex ENUM('male','famale'));
mysql> show table;
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 '' at line 1
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user |
+----------------+
1 row in set (0.00 sec)
mysql> desc user;
+----------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-----------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| nickname | varchar(50) | NO | UNI | NULL | |
| age | tinyint(3) unsigned | NO | | 18 | |
| sex | enum('male','famale') | YES | | NULL | |
+----------+-----------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)