MySQL Default Constraint

Profile picture for user arilio666

The DEFAULT constraint sets a default specified value to the table. Sometimes, this is used to set the default value to missing data or non-entered values.

Add default constraint in MySQL

CREATE TABLE office (
    emp_ID int ,
    emp_fName varchar(255) NOT NULL,
    emp_lName varchar(255) NOT NULL,
    emp_leaves varchar(255) NOT NULL DEFAULT '2',
    Age int
    CHECK(Age <= 50)
    PRIMARY KEY (emp_ID)
    FOREIGN KEY (emp_lName) References office_two(emp_lName)
);

INSERT INTO office (emp_ID,emp_fName,emp_lName,Age)
VALUES(22,'Jake','Lockley',20);

adding default constraint in mysql

As we can see here, we created a table along with the default constraint of emp_leaves which must be by default should be 2 for everyone, and it fills in the table record of 2.

Tags