We can provide default value for a column. If we don specify its value then it will automatically assign a default value. We can specify default value on table creation. Syntax for adding default value is
| column_name datatype DEFAULT value; |
for example
| create table product( id int(11) NOT NULL AUTO_INCREMENT, name varchar(50) NOT NULL, quantity int(10) DEFAULT 100, price decimal(6,2) DEFAULT 10.22, status varchar(50) DEFAULT 'AVAILABLE', add_date timestamp DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY(id)); |
this will produce following output
| mysql> desc product; +----------+--------------+------+-----+-------------------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+--------------+------+-----+-------------------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(50) | NO | | NULL | | | quantity | int(10) | YES | | 100 | | | price | decimal(6,2) | YES | | 10.22 | | | status | varchar(50) | YES | | AVAILABLE | | | add_date | timestamp | NO | | CURRENT_TIMESTAMP | | +----------+--------------+------+-----+-------------------+----------------+ 6 rows in set (0.06 sec) |
while inserting any rowin table if we dont provide … Read more