Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Sql SQL Alter Column Default Value

Related Tutorials


 
 

Share on Google+Share on Google+

SQL Alter Column Default Value

Advertisement
Alter Column Default Value in SQL Server is used with Create Table Statement.

SQL Alter Column Default Value

     

Alter Column Default Value in  SQL Server is used with Create Table Statement. The default value will be set as per specified column. The use of default value is specified when insert is performed, but value in the column is not set.

Understand with Example 

In this Tutorial, the query create a table 'Stu_Table' using a create statement. The insert into in SQL Query add the records to table. The Describe table show you the Table attribute like field, data type etc.

Create Table Stu_Table

create table Stu_Table( Stu_Id integer(2), Stu_Name varchar(15), 
Stu_Class  varchar(10))

Insert data into Stu_Table

insert into Stu_Table (Stu_Id, Stu_Name) values(1,'Komal');
insert into Stu_Table (Stu_Id, Stu_Name) values(2,'Ajay');
insert into Stu_Table (Stu_Id, Stu_Name) values(3,'Rakesh');
insert into Stu_Table (Stu_Id, Stu_Name) values(4,'Bhanu');
insert into Stu_Table (Stu_Id, Stu_Name) values(5,'Santosh');

Stu_Table

+--------+----------+-----------+
| Stu_Id | Stu_Name | Stu_Class |
+--------+----------+-----------+
| 1      | Komal    | 10        |
| 2      | Ajay     | 10        |
| 3      | Rakesh   | 10        |
| 4      | Bhanu    | 10        |
| 5      | Santosh  | 10        | 
+--------+----------+-----------+

Describe Stu_Table

+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| Stu_Id    | varchar(10) | YES  |     |         |       |
| Stu_Name  | varchar(10) | YES  |     | NULL    |       |
| Stu_Class | varchar(5)  | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+

Alter column Column Default Query

The Alter Table Keywords modify table'Stu_Table' and MODIFY keyword modify the data type of field Stu_Id (varchar(10)) to Stu_Id(int(3)) and set the default value for this field is set '10'.Whenever you leave a blank value in Stu_Id, This field would take default value of '10'.

ALTER TABLE Stu_Table MODIFY Stu_Id int(3) Default '10'

Describe Stu_Table

+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| Stu_Id    | int(3)       | No   |     |  10       |       |
| Stu_Name  | varchar(10)  | YES  |     | NULL    |       |
| Stu_Class | varchar(5)   | YES  |     | NULL     |       |
+-----------+--------------+------+-----+---------+-------+
Advertisement

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: December 20, 2008

Related Tutorials

Discuss: SQL Alter Column Default Value  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
elyn
October 20, 2011
Cannot alter table column default value

hye! i faced some problem regarding to set default value for password column in table student. i already tried many ways including this one : ALTER TABLE Student MODIFY Password varchar(50) Default '123456' But, it gave me incorrect syntax near 'MODIFY' and '50'. So can you help me fix this syntax? Thanks in advance. :)
DMCA.com