MySQL DDL語句

來源:互聯網
上載者:User

標籤:mysql ddl

這裡介紹MySQL比較常用的DDL語句。包括如下:

  • create table

  • alter table

  • drop table

  • truncate table

 

 1. create table

    create table為建表語句,看如下幾個樣本:

  • 在列名後面聲明主鍵 

    create table t1(id int primary key, name varchar(20));   

     

  • 在表層級聲明主鍵

    create table t1(id int, name varchar(20),  primary key(id));

     

  • 多欄位主鍵(只能在表層級聲明)

    create table t1(id int, name varchar(20),  primary key(id, name));


  • 建表時指定索引

    create table t1(id int, name varchar(20),primary key(id),index idx_t1_name(name));


  • 建表時指定外鍵

    create table t3(id int, name varchar(20),primary key(id),constraint cst_t1_fk foreign key(id) references t2(id));

     

  • 在表的結尾聲明表的屬性如儲存引擎和字元集等等

    create table t1(id int, name varchar(20),  primary key(id, name)) engine=innodb default charset=utf8;

     

  • 使用if not exists關鍵字。如果這個表不存在則建立這個表,如果已經存在則報一個warning

    create table if not exists t1(id int, name varchar(20), primary key(id));

     

  • 表建立完成後可以用desc t1;或show create table t1\G查看錶的結構,一般情況下desc出來的結果已經夠用,但是如果要獲得建表的語句最好用show create table t1\G



 2. alter table

    alter table可以修改表的結構和屬性,以下alter table後面的語句可以使用逗號分隔在同一條語     句中執行。看如下幾個樣本:

  • 增加欄位

    alter table t1 add (column) age int;=======>預設增加欄位到表的最後,column關鍵字可以省略

    alter table t1 add (column) age first;=======>增加欄位到表的開頭

    alter table t1 add (column) age after name;=======>增加欄位到name欄位之後

     

  • 修改欄位

    alter table t1 modify age tinyint; ========>修改欄位資料類型

    alter table t1 modify age tinyint first; ========>修改欄位資料類型並將欄位移動到表的開頭

    alter table t1 modify age tinyint after id;========>修改欄位資料類型並將欄位移動到欄位id之後

    alter table t1 change age student_age tinyint; ========>修改欄位名稱和資料類型

     

  • 刪除欄位

    alter table t1 drop (column) age;


  • 添加索引

    alter table t1 add index idx_t1_name(name);=======>添加普通索引,index關鍵字可以換成key,idx_t1_name為索引的名稱

    alter table t1 add unique index idx_t1_name(name);=======>添加唯一索引

     

  • 刪除索引

    alter table t1 drop index idx_t1_name;


  • 刪除主鍵

    alter table add drop primary key;

     

  • 添加主鍵

    alter table add primary key(id);

     

  • 添加外鍵

    alter table t1 add constraint cst_t1_fk foreign key(id) references t2(id);


  • 刪除外鍵

    alter table t1 drop foreign key cst_t1_fk;

     

  • 重新命名表 (下面三種語句都可以)

    rename table t1 to t2;

    alter table t1 rename t2;

    alter table t1 rename to t2;


  • 修改儲存引擎或者字元集

    alter table t1 engine=myisam;

    alter table t1 default charset=latin1;



 3. drop table

    意思很簡單,把表刪除。

    drop table t1; =========>直接刪除表,如果沒有這個表會報錯

    drop table if exists t1; ========>如果沒有這個表不會報錯,只會報一個warning



 4. truncate table

    truncate table為截斷表的命令,相當於把這個表刪除並立即重建。

    truncate table t1;




本文出自 “trikker” 部落格,請務必保留此出處http://trikker.blog.51cto.com/7478890/1561622

MySQL DDL語句

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.