Home Quartz SQL Alter Table Primary Key

Related Tutorials


 
 

Share on Google+Share on Google+

SQL Alter Table Primary Key

Advertisement
sql alter table primary key

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   |     |         |       |
+-----------+-------------+------+-----+---------+-------+
Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 18, 2011

Related Tutorials

Discuss: SQL Alter Table Primary Key  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
purvi
August 26, 2011
how set foregn key

plz tell me hw creat the foreign key step by step plz tell me in detal
DMCA.com