--給某一張表添加一個列 ALTER TABLE `users` ADD `username` TEXT NOT NULL; --例如 alter table app_user add starLevel INT(11) NULL default 6; --建表時 給某列添加默認值 create table tablename (columnname datatype default defaultvalue); --已建表修改 alter table tablename alter column columnname set default defaultvalue; --給user表的username添加唯一約束 Alter table user add unique(username); --更改app_activity表中digest的字段,允許為空 ALTER TABLE app_activity MODIFY digest VARCHAR(255) null; --刪除某一字段 ALTER TABLE mytable DROP 字段 名; --修改列的類型 alter table 表名稱 change 字段名稱 字段名稱 字段類型 [是否允許非空]; --更改表名 rename table 舊表名 to 新表名; //添加utf8編碼庫,刪除一個數據庫 CREATE DATABASE `test` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci; DROP database test; //刪除一個索引 alter table 表名 drop index 索引列的名字; //查看表的字段信息: desc 表名; //查看表的所有信息: show create table 表名; //添加主鍵約束: alter table 表名 add constraint 主鍵 (形如:PK_表名) primary key 表名(主鍵字段); //添加外鍵約束: alter table 從表 add constraint 外鍵(形如:FK_從表_主表) foreign ey 從表(外鍵字段) references 主表(主鍵字段); //刪除主鍵約束: alter table 表名 drop primary key; //刪除外鍵約束: alter table 表名 drop foreign key 外鍵(區分大小寫); //刪除唯一約束(username該列上有一個唯一約束,app_user為表名) drop index username on app_user;
