------------恢復內容開始------------
創建資料庫
create database 資料庫名
切換資料庫
use 資料庫名
建表:
create table 表名 (
欄位名1,型別,約束
欄位名2,型別,約束
...
)
約束:
1.主鍵約束
1)直接在建表時欄位型別后加 primary key
2)在表最后加 constraint 約束名 primary key(欄位名)
3)表外修改 alter table 表名 add constraint 約束名 primary key(欄位名)
2.檢查約束
1)直接在建表型別后加 check(約束條件)
2)在表最后加 constraint 約束名 check(約束條件)
3)表外修改 alter table 表名 add constraint 約束名 check(約束條件)
注:mysql不支持檢查約束,但是寫上檢查約束不會報錯
3.非空約束
1)直接在創建表的型別后加 not null
2) 在表最后加入 constraint 約束名 check(欄位名 is not null)
3)在表外修改 alter table 表名 modify 欄位名 欄位型別 not null
4.唯一約束
1)直接在創建表的型別后加 unique
2) 在表的最后加入 constraint 約束名 unqiue(欄位名)
3) 在表外修改 alter table 表名 add constraint 約束名 unique(欄位名)
5.外鍵約束
1)直接在創建表的型別后加 references 父表名(父表主鍵名)
2)在表的最后加入 constraint 約束名 foreign key(欄位名) references 父表名(父表主鍵名)
3)在表外修改 alter table 表名 add constraint 約束名 foreign key(欄位名) references 父表名(父表主鍵名)on delete set null on updata cascade
6.默認約束
1)直接在創建表的型別后加 default 默認值
2)在表外修改 alter table 表名 add constraint 約束名
洗掉約束
alter table 表名 drop constraint 約束名
表的修改
1)添加欄位
alter table 表名 add 欄位名 欄位型別 注:在表中已經有值時,不能加非空約束
2)洗掉欄位
alter table 表名 drop 欄位名
3)修改欄位型別
alter table 表名 modify 欄位名 新欄位型別
4)修改欄位名
alter table 表名 change 欄位名 新欄位名 欄位型別
5)修改表名
alter table 表名 rename as 新表名
6)洗掉表
drop table 表名
查看當前資料庫中所有表
show tables
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/92741.html
標籤:MySQL
上一篇:MySQL學習——備份和還原
下一篇:JDBC學習筆記
