MySQL DEFAULT Constraint

The DEFAULT constraint is used to add default value to any column if the value is not supplied during the insert or update statement.

   create table employee(empno int PRIMARY KEY, 
   name varchar(50) NOT NULL,
   age numeric, 
   role varchar(50) NOT NULL, 
   location varchar(50) NOT NULL, 
   salary decimal NOT NULL,
   created_on date DEFAULT CURRENT_DATE()
   );

In the above query, the created_on column is of type date
and it will receive the current date whenever a new column is inserted.

It is possible to add DEFAULT constraint to a column in an existing table using ALTER TABLE.

alter table employee
alter location SET DEFAULT 'India';

To drop a DEFAULT constraint we use

ALTER TABLE employee
ALTER location DROP DEFAULT;

Most Read