|版權聲明:本文爲博主原創文章,未經博主允許不得轉載。轉載請附上原鏈接,博客地址:https://blog.csdn.net/sgsgy5
1.11.2 創建組合鍵
1.11.3 查看主鍵
1.11.3 刪除主鍵
1.11.4 選擇主鍵的原則
1、 最少性:儘量選擇一個字段做主鍵
2、 穩定性:儘量選擇更新少的列做主鍵
3、 儘量選擇數字型的列做主鍵
1.11.5 主鍵思考題
1、在主鍵列輸入的數值,允許爲空嗎? 不可以
2、 一個表可以有多個主鍵嗎? 不可以
3、 在一個學校數據庫中,如果一個學校內允許重名的學員,但是一個班級內不允許學員重名,可以組合班級和姓名兩個字段一起來作爲主鍵嗎? 可以
4、 標識列(自動增長列)允許爲字符數據類型嗎? 不可以
5、 表中沒有合適的列作爲主鍵怎麼辦? 添加自動增加列
6、 如果標識列A的初始值爲1,增長量爲1,則輸入三行數據以後,再刪除兩行,下次再輸入數據行的時候,標識值從多少開始? 從4開始
1.12 列屬性——唯一鍵
特點:
1、不能重複,可以爲空
2、一個表可以有多個唯一鍵
作用:
1、 保證數據不能重複。保證數據完整性
2、 加快數據訪問
1.12.1 添加唯一鍵
方法一:創建表的時候添加唯一鍵
mysql> create table t22(
-> id int primary key,
-> name varchar(20) unique, #通過unique添加唯一鍵
-> addr varchar(100) unique
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> insert into t22 values (1,'tom','上海');
Query OK, 1 row affected (0.05 sec)
mysql> insert into t22 values (2,'tom','北京'); # name重複了,報錯
ERROR 1062 (23000): Duplicate entry 'tom' for key 'name'
mysql> insert into t22 values (2,'berry','上海'); # addr重複了
ERROR 1062 (23000): Duplicate entry '上海' for key 'addr'
還有一種方法
mysql> create table t26(
-> id int,
-> name varchar(20),
-> addr varchar(20),
-> primary key(id),
-> unique (name), # 添加唯一鍵
-> unique (addr)
-> );
Query OK, 0 rows affected (0.06 sec)
方法二:修改表的時候添加唯一鍵
mysql> create table t23(
-> id int primary key,
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.02 sec)
mysql> alter table t23 add unique (name); # 添加一個唯一鍵
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
一次添加多個唯一鍵
mysql> create table t24(
-> id int primary key,
-> name varchar(20),
-> addr varchar(20)
-> );
Query OK, 0 rows affected (0.06 sec)
mysql> alter table t24 add unique(name),add unique(addr);
Query OK, 0 rows affected (0.09 sec)
Records: 0 Duplicates: 0 Warnings: 0
添加組合唯一鍵
mysql> create table t25(
-> id int primary key,
-> name varchar(20),
-> addr varchar(20)
-> );
Query OK, 0 rows affected (0.09 sec)
mysql> alter table t25 add unique(name,addr);
Query OK, 0 rows affected (0.01 sec)
Records: 0 Duplicates: 0 Warnings: 0
1.12.2查看唯一鍵
mysql> show create table t26\G
*************************** 1. row ***************************
Table: t26
Create Table: CREATE TABLE `t26` (
`id` int(11) NOT NULL DEFAULT '0',
`name` varchar(20) DEFAULT NULL,
`addr` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`), # 唯一鍵
UNIQUE KEY `addr` (`addr`) # 唯一鍵
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
mysql> show create table t25\G
*************************** 1. row ***************************
Table: t25
Create Table: CREATE TABLE `t25` (
`id` int(11) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`addr` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`,`addr`) # 組合唯一鍵
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
添加唯一鍵,給唯一鍵取名
mysql> create table t27(
-> name varchar(20)
-> );
Query OK, 0 rows affected (0.03 sec)
mysql> alter table t27 add unique UQ_name(name);
Query OK, 0 rows affected (0.00 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show create table t27\G
*************************** 1. row ***************************
Table: t27
Create Table: CREATE TABLE `t27` (
`name` varchar(20) DEFAULT NULL,
UNIQUE KEY `UQ_name` (`name`) # 唯一鍵的名字是UQ_name
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
1.12.3 刪除唯一鍵
通過唯一鍵的名字來刪除唯一鍵
語法:alter table 表名 drop index 唯一鍵名稱
問題:主鍵和唯一鍵的區別?
1、主鍵不能重複,不能爲空,唯一鍵不能重複,可以爲空
2、主鍵只有一個,唯一鍵可以有多個。
1.13列屬性——備註(comment)
爲了程序員之間的相互交流
1.14 SQL註釋
單行註釋:–或#
多行註釋:/* */