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 | | +-----------+--------------+------+-----+---------+-------+ |
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: SQL Alter Column Default Value
Post your Comment