SQL Default Constraint


The attribute or column in a which has Default constraint Specifies that when a user explicitly does not insert value for that column a default value which is defined for that gets inserted .


 Lets see the declaration of DEFAULT constraint during creation of table .


               

                      Create table Vehicle(

                        vehicle_id            int Primary key,

                        vehicle_name  varchar(30),

                        mileage int  int NOT NULL DEFAULT 50

                                            ) ;
 

In the above defined vehicle table the column Mileage is declared with constraint two  NOT NULL and Default .

  Q) The reason of using NOT NULL along with Default  constraint ?

  A) It implicitly saying that a default value defined is not null and which has got some value .


Inserting values:-

                           

                               Insert into Vehicle values(1,"Honda Activa" ) ;


                       



 As you can see in the above insertion operation for vehicle table , the mileage column is not inserted

 so a default value which is defined during its declaration gets inserted .


Vehicle

vehicle_id vehicle_name mileage
1 Honda Activa 50









No comments:

Post a Comment