MySQL動態添刪改欄欄位命令

來源:互聯網
上載者:User

MySQL如何動態添刪改欄欄位呢,SQL如下:

動態增加欄欄位:

ALERT TABLE table1 add transactor varchar(10) not Null;

動態刪除欄欄位:

ALERT TABLE TableName drop column field_id;

動態修改欄欄位:

ALERT TABLE table_name change old_field_name  new_field_name field_type;

動態修改表結構

ALERT TABLE table_name MODIFY field_name field_type

建立表格後添加: alter table tablename add id int auto_increment primary key

設定自增欄位初始值:alter table tablename  auto_increment =x ;

設定主鍵:alter table tablename add primary key(field_name);

建立複合主鍵:create table tablename (
                                  studentno int,
                                  courseid int,
                                  score int,
                                  primary key (studentno,courseid) );


設定複合主鍵:alter table tablename add primary key (列1,列2,列3);

重新命名表: alter table table_old_name rename table_new_name;

改變欄位的類型:alter table tableName modify field_name field_type;

重新命名欄位:alter table tableName change old_field_name new_field_name new_field_type;

刪除欄位:alter table tableName drop column field_name;

增加一個新欄位:alter table tableName add new_field_name field_type;  

新增一個欄位,預設值為0,非空:alter table tableName add new_field_name field_type not null default '0';

新增一個欄位,預設值為0,非空,自增,主鍵:alter table tabelname add new_field_name field_type default 0 not null   auto_increment ,add primary key (new_field_name); 

修改欄位類型:

alter table tablename change filed_name filed_name new_type;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.