-
null、not null ■ 字段默认属性为 null 。 ■ 设定为空,或非空,表明该列数据是否可为空值(mull) 。
-
default ■ 用于设定列默认值(不给值或给空值 null 并 not null,就会自动使用该值)。 ■ 使用形式: default 默认值。
-
primary key ■ 用于设定主键。 ■ 主键就是一个表中数据的“关键值”,通过该关键值就可以找到该特定的数据行。 ■ 一个表的主键值不能重复(相等) ,比如文章表中的文章编号id, 比如用户表中的用户名。 ■ 主键字段必须有值(不能为空)。 ■ 一个表只能有一个主键(但一个主键可以是1个字段或2个以上的字段联合构成)
mysql> create table attrTest (id int primary key, user_name varchar(10), sex enum('男','女') default '男' not null, salary float null);
mysql> desc attrTest;
+-----------+-------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| user_name | varchar(10) | YES | | NULL | |
| sex | enum('男','女') | NO | | 男 | |
| salary | float | YES | | NULL | |
+-----------+-------------------+------+-----+---------+-------+
- auto_increment ■ 用于设定一个整数字段的值是“自增长的”,通常用于一个表中的数据行的编号(比如文章编号)。 ■ 默认情况下自增长值从1开始。 ■ 一个表只能设定一个字段为自增长特性。
mysql> create table test (id int primary key auto_increment, name varchar(10));
mysql> desc test;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(10) | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
mysql> insert into test (name) values ('dzm');
mysql> insert into test (name) values ('dzm');
mysql> select * from test;
+----+------+
| id | name |
+----+------+
| 1 | dzm |
| 2 | dzm |
+----+------+
- unique key
■ 用于设定“唯一键”的特性。
■ 唯一键表示一个表中的某字段的信是“唯一的”,“不重复的”。
■ 唯一键有点类似
primary key, 但其值可以为空(null)。 ■ 一个表可以有多个唯一键。
mysql> create table test (id int auto_increment primary key, name varchar(10) unique key);
mysql> create table test (id int auto_increment primary key, name varchar(10), unique key(id, name));
- comment ■ 用于设定字段的说明性内容,类似注释,但又不是注释(属于有效的代码)。 ■ 使用形式: comment '文字内容'。
mysql> create table test (
id int auto_increment primary key comment '数据库编号',
id_card varchar(20) not null unique key comment '身份证号码',
name varchar(20) not null comment '姓名',
age tinyint unsigned default 0 comment '年龄'
);
mysql> desc test;
+---------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| id_card | varchar(20) | NO | UNI | NULL | |
| name | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | YES | | 0 | |
+---------+---------------------+------+-----+---------+----------------+