Create a new table in MySQL with specific options with DEFAULT?


For this, use DEFAULT keyword after the column data type.

Let us create a table −

mysql> create table demo33
−> (
−> id int not null auto_increment primary key,
−> name varchar(20) not null,
−> start_date date default(current_date),
−> end_date date default NULL,
−> category enum('Good','Medium','Low') default 'Low'
−> );
Query OK, 0 rows affected (2.32 sec)

Insert some records into the table with the help of insert command −

mysql> insert into demo33(name) values('John');
Query OK, 1 row affected (0.15 sec)

mysql> insert into demo33(name,end_date,category) values('David','2020−12−21','Medium');
Query OK, 1 row affected (0.09 sec)

mysql> insert into demo33(name,start_date,category) values('David','2020−04−01','Good');
Query OK, 1 row affected (0.14 sec)

mysql> insert into demo33(name,start_date,end_date,category) values('David','2020−03−10','2020−08−22','Good');
Query OK, 1 row affected (0.17 sec)

Display records from the table using select statement −

mysql> select *from demo33;

This will produce the following output −

+----+-------+------------+------------+----------+
| id | name  | start_date | end_date   | category |
+----+-------+------------+------------+----------+
| 1  | John  | 2020−10−28 | NULL       | Low      |
| 2  | David | 2020−10−28 | 2020−12−21 | Medium   |
| 3  | David | 2020−04−01 | NULL       | Good     |
| 4  | David | 2020−03−10 | 2020−08−22 | Good     |
+----+-------+------------+------------+----------+
4 rows in set (0.00 sec)

Updated on: 19-Nov-2020

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements