mysql alter table敕令修正表構造實例。本站提示廣大學習愛好者:(mysql alter table敕令修正表構造實例)文章只能為提供參考,不一定能成為您想要的結果。以下是mysql alter table敕令修正表構造實例正文
mysql實例之應用alter table敕令修正表構造
mysql alter table語句可以修正表的根本構造,例如添加字段、刪除字段、添加主鍵、添加索引、修正字段數據類型、對表重定名等等操作,本文章經由過程兩個簡略的實例向年夜家引見mysql alter table的應用辦法
實例一:應用ALTER TABLE敕令向表中添加字段、修正字段類型和設置主鍵。
起首創立一個表,SQL語句以下:
mysql> CREATE TABLE myTable( -> ID SMALLINT -> );
應用desc敕令檢查表構造:
mysql> desc myTable; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | ID | smallint(6) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+
應用ALTER TABLE敕令向表中添加字段、修正字段類型和設置主鍵,SQL語句以下:
mysql> ALTER TABLE myTable -> ADD COLUMN Quantity SMALLINT UNSIGNED NOT NULL, -> MODIFY ID SMALLINT UNSIGNED NOT NULL, -> ADD PRIMARY KEY (ID);
再次應用desc敕令檢查表構造,我們發明下面的修正敕令失效了:
desc myTable; +----------+----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+----------------------+------+-----+---------+-------+ | ID | smallint(5) unsigned | NO | PRI | | | | Quantity | smallint(5) unsigned | NO | | | | +----------+----------------------+------+-----+---------+-------+
實例二:在下面的實例基本上,我們刪除myTable表中的Quantity字段和PRIMARY KEY主鍵
mysql> ALTER TABLE myTable -> DROP COLUMN Quantity, -> DROP PRIMARY KEY;
檢查表構造:
mysql> desc myTable; +-------+----------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+----------------------+------+-----+---------+-------+ | ID | smallint(5) unsigned | NO | | | | +-------+----------------------+------+-----+---------+-------+
感激浏覽此文,願望能贊助到年夜家,感謝年夜家對本站的支撐!