Mysql Alter Table Primary Key
Mysql Alter Table Primary Key is used to remove the primary key from the table and add primary key to the existing other column of table.
Understand with Example
The Tutorial illustrate an example from 'Mysql Alter Table Primary Key'. To understand this example, we create a table employees with required field name and data type respectively. The createtable employee create a table employee with Empid as primary key.
Query to create table:
CREATE TABLE `employees` ( ->Empid int(10), ->Empname varchar(60), ->date date ->PRIMARY KEY(Empid) ->); |
Query to insert data into Table named employees:
The Query insert into add the records or rows to the table 'employee'.
mysql>insert into employees values(01,'Girish','2008-12-22'); Query OK, 1 row affected (0.02 sec) mysql>insert into employee1 values(02,'Komal','2008-12-23'); Query OK, 1 row affected (0.02 sec) |
Query to view data of Table named employees:
To view the detail from table 'employee' we use select query that return the detail of table 'employees'.
mysql> select * from employee; |
Output:
+-------+---------+------------+ | Empid | Empname | date | +-------+---------+------------+ | 1 | Girish | 2008-12-22 | | 2 | Komal | 2008-12-23 | +-------+---------+------------+ 2 rows in set (0.00 sec) |
Query to view datatype of Table named employees:
The describe employees return the Fieldname, data type, Null, Key, Default.
mysql> describe employees; |
Output:
+---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | Empid | int(10) | NO | PRI | 0 | | | Empname | varchar(60) | YES | | NULL | | | date | date | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) |
Query for altering the Primary key of the table employees:
The Query alter is used to change the table ' Employees' and drop primary remove the 'Empid' as primary key. The table is no longer to contain a primary key.
Step1:-Drop the primary key
mysql> alter table Employees -> drop primary key; Query OK, 2 rows affected (0.11 sec) Records: 2 Duplicates: 0 Warnings: 0 |
Query to view changes occurs in the datatype of Table named employees:
mysql> describe employees; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | Empid | int(10) | NO | | 0 | | | Empname | varchar(60) | YES | | NULL | | | date | date | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) |
Step1:-Define the primary key
Query for defining the Primary key of the table employees:
The Query alter redefine the table Employees and add keyword insert the 'Empname' as Primary Key in the table 'employees.
mysql> alter table employees -> add primary key (Empname); Query OK, 2 rows affected (0.14 sec) Records: 2 Duplicates: 0 Warnings: 0 |
Query to view datatype of Table named employees:
mysql> describe employees; +---------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +---------+-------------+------+-----+---------+-------+ | Empid | int(10) | NO | | 0 | | | Empname | varchar(60) | NO | PRI | | | | date | date | YES | | NULL | | +---------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) |