1. 程式人生 > >mysql alter修改資料庫表結構用法

mysql alter修改資料庫表結構用法

1.alter操作表字段

(1)增加欄位

  alter table 表名 add 欄位名 欄位型別;

  alter table student add name varchar(10);

(2)修改欄位

   alter table 表名 change 舊欄位名 新欄位名 欄位型別;

   alter table 表名 modify 欄位名 欄位型別;//修改欄位型別

   alter table student change name name varchar(20)not null default 'liming';//修改欄位型別 default後邊是

    欄位預設的值

   alter table student change name name1 varchar(20)not null default 'liming';//修改欄位名

(3)刪除欄位

   alter table 表名 drop 欄位名;

   alter table student drop name;

2.alter 索引操作

 (1)增加索引

    alter table 表名 add index 索引名 (欄位名1,欄位名2.....);

    alter table student add index stu_name(name);

  (2)刪除索引

     alter table 表名 drop index 索引名;

     alter table student drop index stu_name;

  (3)檢視某個表的索引

     show index from 表名;

   (4)增加唯一限制條件的索引

     alter table 表名 add unique 索引名(欄位名);

 3.主鍵操作

    增加主鍵:

   alter table 表名 add primary key(欄位名);

   刪除主鍵:

   alter table 表名 drop primary key;(主鍵不是自動增長情況下)

    alter table 表名 modify 欄位 欄位型別, drop primary key;(主鍵是自動增長情況下)

  alter table 123 modify id int,drop primary key;