程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> sql alert table 修改表結構的使用方法

sql alert table 修改表結構的使用方法

編輯:MySQL綜合教程

sql alert table 修改表結構的使用方法

ALTER TABLE 語句用於在已有的表中添加、修改或刪除列

ADD [COLUMN] column name (column definitions) [FIRST or AFTER column_name]
ADD INDEX [index_name] (column_list)
ADD PRIMARY KEY (column_list)
ADD UNIQUE [index_name] (column_list)
ALTER [COLUMN] column_name {SET DEFAULT default_value or DROP DEFAULT}
CHANGE [COLUMN] old_col_name create_definition
DROP [COLUMN] col_name
DROP PRIMARY KEY
DROP INDEX index_name
MODIFY [COLUMN] create_definition
RENAME [AS] new_tbl_name

The IGNORE keyword causes rows with duplicate values in unique keys to be deleted;
otherwise, nothing happens.

ALTER TABLE employee ADD COLUMN Account_Number INT
ALTER TABLE employee ADD INDEX (ID)
ALTER TABLE employee ADD PRIMARY KEY (ID)
ALTER TABLE employee ADD UNIQUE (ID)
ALTER TABLE employee CHANGE ID salary INT
ALTER TABLE employee DROP Customer_ID
ALTER TABLE employee DROP PRIMARY KEY

ALTER TABLE employee DROP INDEX Customer_ID
ALTER TABLE employee MODIFY First_Name varchar(100)
ALTER TABLE employee RENAME Customer

給表增加一個主鍵

mysql教程>
mysql>
mysql> CREATE TABLE myTable(
    ->    ID SMALLINT
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql>
mysql> desc myTable;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| ID    | smallint(6) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql>
mysql> ALTER TABLE myTable
    ->    ADD COLUMN Quantity SMALLINT UNSIGNED NOT NULL,
    ->    MODIFY ID SMALLINT UNSIGNED NOT NULL,
    ->    ADD PRIMARY KEY (ID);
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql>
mysql> desc myTable;
+----------+----------------------+------+-----+---------+-------+
| Field    | Type                 | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+-------+
| ID       | smallint(5) unsigned | NO   | PRI |         |       |
| Quantity | smallint(5) unsigned | NO   |     |         |       |
+----------+----------------------+------+-----+---------+-------+
2 rows in set (0.02 sec)

mysql>
mysql> drop table myTable;
Query OK, 0 rows affected (0.00 sec)

mysql>

修改表列名

mysql>
mysql> ALTER TABLE myTable
    ->    DROP COLUMN Quantity,
    ->    DROP PRIMARY KEY;
Query OK, 0 rows affected (0.05 sec)
Records: 0  Duplicates: 0  Warnings: 0

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved