1.NULL / NOT NULL
NULL(表示默認的),數據庫默認字段基本都是字段爲空。
NOT NULL(表示不爲空)
--NULL和任意數據進行運算操作,結果爲NULL
2.default(默認值)
比如創建女生班級學生表時所有的學生性別都是女,就可以在創建表時給性別列附上默認值'女';在插入數據時,不用給性別賦值,就使用默認值。
mysql> create table student(id int,name varchar(32),sex varchar(32) default '女');
Query OK, 0 rows affected (0.49 sec)
mysql> insert into student(id,name)values(1,'huahua'),(2,'haha');
Query OK, 2 rows affected (0.19 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from student;
+------+--------+------+
| id | name | sex |
+------+--------+------+
| 1 | huahua | 女 |
| 2 | haha | 女 |
+------+--------+------+
2 rows in set (0.00 sec)
mysql> insert into student values(3,'hh','男');
Query OK, 1 row affected (0.14 sec)
mysql> select * from student;
+------+--------+------+
| id | name | sex |
+------+--------+------+
| 1 | huahua | 女 |
| 2 | haha | 女 |
| 3 | hh | 男 |
+------+--------+------+
3 rows in set (0.00 sec)
3.列描述(comment註釋:對程序沒有影響)
mysql> create table student(
id int comment '學號',
name varchar(32) comment '姓名',
sex varchar(32) comment '性別' default '女');
Query OK, 0 rows affected (0.49 sec)
4.zerofill(如果寬度小於設定的寬度,自動填充0)
mysql> create table zerofilltest(a int(5));
Query OK, 0 rows affected (0.80 sec)
mysql> insert into zerofilltest values(1);
Query OK, 1 row affected (0.17 sec)
mysql> select * from zerofilltest;
+------+
| a |
+------+
| 1 |
+------+
1 row in set (0.00 sec)
5.主鍵(primary key)
主鍵用來唯一的約束該字段裏面的數據,不能重複,不能爲空,一張表中最多隻能有一個主鍵,主鍵所在的列往往都是整數類型。
添加主鍵的方式
I.直接在列名後加primary key。
mysql> create table pritest(a int primary key,b int);
Query OK, 0 rows affected (0.63 sec)
mysql> desc pritest;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| a | int(11) | NO | PRI | NULL | |
| b | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.11 sec)
II.在表創建成功後使用alter table 表名 add primary key(字段名)添加主鍵
mysql> create table pritest1(a int,b int);
Query OK, 0 rows affected (0.65 sec)
mysql> desc pritest1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| a | int(11) | YES | | NULL | |
| b | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
--添加主鍵
mysql> alter table pritest1 add primary key(a);
Query OK, 0 rows affected (1.12 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc pritest1;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| a | int(11) | NO | PRI | NULL | |
| b | int(11) | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.02 sec)
III.使用複合主鍵,在創建表時用primary key(字段1名,字段2名)
mysql> create table pritest2(a int,b int,primary key(a,b));
Query OK, 0 rows affected (0.71 sec)
mysql> desc pritest2;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| a | int(11) | NO | PRI | NULL | |
| b | int(11) | NO | PRI | NULL | |
+-------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
6.自增長(auto_increment)
當對應的字段,不給值,會自動的被系統觸發,系統會從當前字段中已經有的最大值+1操作, 得到一個新的不同的值
mysql> create table incrementtest(id int primary key auto_increment,age int);
Query OK, 0 rows affected (0.57 sec)
mysql> insert into incrementtest (age)values(15),(16);
Query OK, 2 rows affected (0.13 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from incrementtest;
+----+------+
| id | age |
+----+------+
| 1 | 15 |
| 2 | 16 |
+----+------+
2 rows in set (0.00 sec)
自增長的特點:
任何一個字段要做自增長,前提是本身是一個索引(key一欄有值)
自增長字段必須是整數
一張表最多隻能有一個自增長
7.唯一鍵(unique key)
唯一鍵可以解決表中有多個字段需要唯一性約束的問題。唯一鍵允許爲空,也可多個爲空,空字段不進行唯一性比較。
8.外鍵(foreign key )
外鍵用於定義主表和從表之間的關係:外鍵約束主要定義在從表上,主表則必須是有主鍵約束或unique約束。當 定義外鍵後,要求外鍵列數據必須在主表的主鍵列存在或爲null。
外鍵語法:foreign key (字段名) references 主表(列)