SQL Alter Table Primary Key
Alter a Table Primary Key in SQL modifies the existing table and adds a primary key.
Create Table Stu_Table
SQL statement to create table:
create table Stu_Table(Stu_Id varchar(2), Stu_Name varchar(15), Stu_Class varchar(10))
Insert data into Stu_Table
SQL statement to insert data into table:
insert into Stu_Table values(1,'Komal',10); insert into Stu_Table values(2,'Ajay',10); insert into Stu_Table values(3,'Rakesh',10); insert into Stu_Table values(4,'Bhanu',10); insert into Stu_Table values(5,'Santosh',10); insert into Stu_Table values(6,'Tanuj',10); |
Stu_Table
Records in the table:
Stu_Id | Stu_Name | Stu_Class |
1 | Komal | 10 |
2 | Ajay | 10 |
3 | Rakesh | 10 |
4 | Bhanu | 10 |
5 | Santosh | 10 |
6 | Tanuj | 10 |
Describe Stu_Table
+-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | Stu_Id | varchar(2) | NO | | | | | Stu_Name | varchar(15) | YES | | | | | stu_class | varchar(10) | NO | | | | +-----------+-------------+------+-----+---------+-------+ |
Alter Table Primary Key Syntax
The table_name is the name of the table on which primary key is added.
The column_name is the name of the column on which primary key is created.
ALTER TABLE table_name ADD PRIMARY KEY (column_name) |
Alter Table Primary Key Query
The given Query show you an example to alter a table name 'Stu_Table' on which primary key is added. The 'Stu_Id' is the name of the column on which primary key is created.
ALTER TABLE Stu_Table ADD PRIMARY KEY (Stu_Id) |
Describe Stu_Table
+-----------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+-------------+------+-----+---------+-------+ | Stu_Id | varchar(2) | NO | PRI | | | | Stu_Name | varchar(15) | YES | | | | | stu_class | varchar(10) | NO | | | | +-----------+-------------+------+-----+---------+-------+ |