前提准備:
① workbench for mysql輔助開發mysql數據庫的工具;
② 熟練sql命令;
開始操作:
1. 創建數據庫表student:此時已經有數據庫表的主鍵!
1 create table student( 2 stu_id integer primary key, 3 stu_name varchar(20) not null, 4 stu_age int(3) not null, 5 stu_grades int(3) not null 6 )engine=InnoDB,charset=utf8;
2.對數據庫插入數據:
insert into student values(1,'Drew',20,78),(2,'Allen',22,90),(3,'Bob',21,87);
3. 顯示數據:

4. 將數據庫的中的stu_name 設置約束為唯一的:
alter table student add constraint unique(stu_name);
之后的數據庫結構表為:

5. 見證奇跡的時刻到了:注意此時的主鍵約束是stu_id!我們來嘗試刪除主鍵:
alter table student drop primary key;
6. 在顯示一下數據表student的結構:
desc student;
結果為:

你發現了什么嗎?
mysql數據庫自動的匹配非空唯一的值作為主鍵約束!——從另一個角度來講,這樣可以幫助你更好的理解主鍵約束的含義。
好了!到此結束!感謝耐心閱讀!
