
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Creating a Table in MySQL to set current date as default
Following is the syntax for creating a table and adding DEFAULT constraint to set default value −
CREATE TABLE yourTableName ( yourColumnName1 dataType not null , yourColumnName2 dataType default anyValue, . . . N );;
Let us create a table wherein we have set “employee_joining_date” with default constraint for current date as default −
mysql> create table demo43 −> ( −> employee_id int not null auto_increment primary key, −> employee_name varchar(40) not null, −> employee_status varchar(60) default "NOT JOINED", −> employee_joining_date date default(CURRENT_DATE) −> ); Query OK, 0 rows affected (0.66 sec)
Insert some records into the table with the help of insert command −
mysql> insert into demo43(employee_name,employee_status,employee_joining_date) values('John','JOINED','2020-05-10'); Query OK, 1 row affected (0.12 sec) mysql> insert into demo43(employee_name,employee_status,employee_joining_date) values('David','JOINED','2020-10-12'); Query OK, 1 row affected (0.21 sec) mysql> insert into demo43(employee_name) values('Bob'); Query OK, 1 row affected (0.15 sec)
Display records from the table using select statement −
mysql> select *from demo43;
This will produce the following output −
+-------------+---------------+-----------------+-----------------------+ | employee_id | employee_name | employee_status | employee_joining_date | +-------------+---------------+-----------------+-----------------------+ | 1 | John | JOINED | 2020−05−10 | | 2 | David | JOINED | 2020−10−12 | | 3 | Bob | NOT JOINED | 2020−10−31 | +-------------+---------------+-----------------+-----------------------+ 3 rows in set (0.00 sec)
- Related Articles
- Set DEFAULT values for columns while creating a table in MySQL
- How to set default date time as system date time in MySQL?
- Present date as default value in a table
- Creating Unique Key in MySQL table referring to date?
- Set options while creating a MySQL table. Display the same options as well
- Set current date and time to timestamp in MySQL
- Set AUTO_INCREMENT in a table while creating it in MySQL?
- How to add current date to an existing MySQL table?
- Set a MySQL field with the current date (UNIX_TIMESTAMP(now))
- Adding a new column with the current time as a default value in MySQL?
- Find the difference between current date and the date records from a MySQL table
- How to set NOW() as default value for datetime datatype in MySQL?
- How to set a comma separated list as a table in MySQL?
- How can we insert current date automatically in a column of MySQL table?
- Creating a table with MySQL - Hibernate

Advertisements