Home Sql Mysql-table Mysql Add Column Default Value



Mysql Add Column Default Value
Posted on: January 20, 2009 at 12:00 AM
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    |
+------+---------+-------+

Related Tags for Mysql Add Column Default Value:
sqlmysqlcidetablesedcolumndefaultvalueidtabifforierecordaddrecordstocieputuseulpefaultinnomsidsideddadspecumnwhensspcolmykisrdsvassrdthabablaluefaultlumolo


More Tutorials from this section

Ask Questions?    Discuss: Mysql Add Column Default Value  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

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.