SQL DEFAULT Constraint

The DEFAULT constraint in SQL is used to fill up the mentioned column with default and fixed values. All the new records will have the same value if no other value is mentioned.

  • Syntax using DEFAULT Constraint – 

CREATE TABLE table_name
(column1 datatype,column2 datatype,.....,
columnN datatype DEFAULT 'value' );

Example – Create an Employee table with columns as (ID, Name, Country) and add constraint on Country as “INDIA”.

CREATE TABLE EMPLOYEES
( ID int NOT NULL,
Emp_Name varchar (50) NOT NULL,
Country varchar(255) DEFAULT 'India');

  • Here the EMPLOYEES table has been created with the DEFAULT constraint. Now let’s insert some values into the table.


INSERT INTO
EMPLOYEES VALUES(1, 'Simon', DEFAULT);
INSERT INTO EMPLOYEES VALUES(2, 'Maria', DEFAULT);

  • Here we haven’t mentioned any Country name, but have a constraint instead of any Country’s name.

Implementing the SELECT Statement to view the EMPLOYEES table –

ID Emp_name Country
1 Simon India
2 Maria India

Other SQL topics to check out:

Follow us on TwitterFacebookLinkedin, and Tableau Public to stay updated with our latest blog and what’s new in Tableau.

Automate data analysis pipeline and create report ready dashboards

If you are looking forward to getting your data pipeline built and setting up the dashboard for business intelligence, book a call now from here.