Tutorialspoint

1 Answer
karthikeya Boyini

Use DEFAULT keyword at the time of table creation and it will insert default value whenever you do not provide the value for that column.

Let us first create a table. Here, for ClientAge, we have set the default 24:Let us first create a table. Here, for ClientAge, we have set the default 24 −

mysql> create table DemoTable
   -> (
   -> ClientId int AUTO_INCREMENT PRIMARY KEY,
   -> ClientName varchar(100),
   -> ClientAge int DEFAULT 24
   -> );
Query OK, 0 rows affected (0.49 sec)

Insert some records in the table using insert command. For values which are not specified, the default 24 gets inserted in its own −

mysql> insert into DemoTable(ClientName,ClientAge) values('Bob',29);
Query OK, 1 row affected (0.17 sec)

mysql> insert into DemoTable(ClientName) values('David');
Query OK, 1 row affected (0.14 sec)

mysql> insert into DemoTable(ClientName) values('Carol');
Query OK, 1 row affected (0.15 sec)

mysql> insert into DemoTable(ClientName,ClientAge) values('Robert',31);
Query OK, 1 row affected (0.18 sec)

Display all records from the table using select statement −

mysql> select *from DemoTable;

This will produce the following output −

+----------+------------+-----------+
| ClientId | ClientName | ClientAge |
+----------+------------+-----------+
|        1 | Bob        |        29 |
|        2 | David      |        24 |
|        3 | Carol      |        24 |
|        4 | Robert     |        31 |
+----------+------------+-----------+
4 rows in set (0.00 sec)

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.