Home Sql Mysql-table Mysql Add Column Default Value
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Mysql Add Column Default Value

Advertisement
Mysql Add Column Default Value is used to add the default value to the column. The records take a default value when there is no value for a records put inside the specified column of table.

Mysql Add Column Default Value

     

Mysql Add Column Default Value is used to add the default value to the column. The records take a default value when there is no value for a records put  inside the specified column of table.

Understand with Example

The Tutorial illustrate an example from 'Mysql Add Column Default Value'. To understand and grasp this example we create a table 'stu' and 'lib' with required fieldnames and datatypes respectively.

 

 

 

Create Table Stu

Create Table Stu(Id int, Name varchar(10), Class  int);

Describe Stu

The Describe Stu is used to describe the fieldname,Type,Null ,etc in the table.

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

Query for  Add Column Default Value

The Query Alter is used to modify the table 'stu' and add a default value of 10 for those records which don't have value specified in a column.

ALTER TABLE Stu MODIFY class int Default '10';

Describe Stu

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

Insert data into Stu

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

Stu Table

+------+---------+-------+
| Id   | Name    | class |
+------+---------+-------+
| 1    | Komal   | 10    |
| 2    | Ajay    | 10    |
| 3    | Rakesh  | 10    |
| 4    | Bhanu   | 10    |
| 5    | Santosh | 10    |
+------+---------+-------+
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: January 20, 2009

Ask Questions?    Discuss: Mysql Add Column Default Value  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
mutai
November 12, 2011
COMPUTER NETWORKS

GOOD WORK OF YOU
DMCA.com