Home Sql Mysql-alter SQL Alter Table
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

SQL Alter Table

Advertisement
SQL Alter Table is used to modify the existing table definition. The modification in table relates to add columns, rename column and add primary key to the column in a table.

SQL Alter Table

     

SQL Alter Table is used to modify the existing table definition. The modification in table relates to add columns, rename column and add primary key to the column in a table.

Understand with Example

The Tutorial illustrate an example from 'SQL Alter Table'. To understand and grasp the example we create a table 'Stu' with fieldnames and datatypes respectively.

 

 

 

Create Table Stu:

Create Table Stu(Id int,Name Varchar(15));

Describe Stu:

The Describe Stu is used to describe the Fieldname, Type, Null etc of the table Stu.

+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Id    | int(11)     | YES  |     |         |       |
| Name  | varchar(15) | YES  |     |         |       |
+-------+-------------+------+-----+---------+-------+

Insert data into Stu:

To add the records into the 'Stu', we use insert into that add the records in table Stu. 

Insert Into Stu values(1,'Ajay');
Insert Into Stu values(2,'Bhanu');
Insert Into Stu values(3,'Komal');
Insert Into Stu values(4,'Rakesh');
Insert Into Stu values(5,'Santosh');

Stu Table

+------+---------+
| Id   | Name    |
+------+---------+
| 1    | Ajay    |
| 2    | Bhanu   |
| 3    | Komal   |
| 4    | Rakesh  |
| 5    | Santosh |
+------+---------+

Add Primary Key:

The  Query Alter given below is used to modify the existing table 'Stu' and add id as primary key in the table Stu. 

Alter Table Stu add primary key (id) ;

Describe Stu

+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Id    | int(11)     | NO   | PRI | 0       |       |
| Name  | varchar(15) | YES  |     |         |       |
+-------+-------------+------
Advertisements

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: October 6, 2010

Ask Questions?    Discuss: SQL Alter Table  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com