1. 程式人生 > 實用技巧 >Mysql表定義與完整性約束控制

Mysql表定義與完整性約束控制

顯示錶的名稱

show tables;

顯示錶的結構

desc 表名;

show create table 表名;//展示詳細語句以及引擎和字符集

修改資料表

alter table <舊錶名> rename to <新表名>;

修改欄位資料型別

alter table <表名> modify <欄位名> <資料型別>;

修改欄位名

alter table <表名> change <舊欄位名> <新欄位名> <新資料型別>;

新增資料表字段

alter table <表名> add <新欄位名> <資料型別>[約束條件][FIRST|AFTER已經存在的欄位名];

往表的第一列新增一個欄位:

alter table stu add testid int(10) first;

往表中已有欄位後新增一個新的欄位:

alter table stu add markid int(10) after mark;

刪除欄位

alter table <表名> drop <欄位名>;

修改欄位排序

alter table <表名> modify <欄位1> <資料型別> first|after <欄位2>;

更改表的儲存引擎

alter table stu engine=MyISAM;

刪除表的外來鍵約束

alter table <表名> drop foreign key <外來鍵約束名>;

複製資料表

create table <表1> like <表2>;//將表2複製到表1(全部複製)

create table t1 as select sn,sname,sage from t2;//將t2的欄位選擇複製到t1

刪除資料表

drop table <表名>;