Drop primary key from a table using the alter table command : Alter Table « Table « MySQL Tutorial






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

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.08 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.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

mysql>
mysql> desc myTable;
+-------+----------------------+------+-----+---------+-------+
| Field | Type                 | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+-------+
| ID    | smallint(5) unsigned | NO   |     |         |       |
+-------+----------------------+------+-----+---------+-------+
1 row in set (0.01 sec)

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

mysql>








4.3.Alter Table
4.3.1.ALTER [IGNORE] TABLE table_name specification [,specification]
4.3.2.Add primary key to a table by using the alter table command
4.3.3.Drop table column with alter table command
4.3.4.Alter table to add the foreign key
4.3.5.Drop primary key from a table using the alter table command
4.3.6.MODIFYing a TABLE