SQL Alter Table

Ads
 

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.

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

Ads

Ads