SQL > Constraint > DEFAULT Constraint

The DEFAULT constraint provides a default value to a column when the INSERT INTO statement does not provide a specific value. For example, if we create a table as below:

The SQL DEFAULT constraint automatically fills in a pre-set value for a column whenever an INSERT omits that column, preventing unintended NULLs and reducing the need for explicit values in every query.

CREATE TABLE Student
(Student_ID integer Unique,
Last_Name varchar (30),
First_Name varchar (30),
Score Integer DEFAULT 80);

and execute the following SQL statement,

INSERT INTO Student (Student_ID, Last_Name, First_Name) VALUES (10, 'Johnson', 'Rick');

The table will look like the following:

 Student_ID  Last_Name  First_Name  Score 
 10  Johnson  Rick  80 

Even though we didn't specify a value for the "Score" column in the INSERT INTO statement, it does get assigned the default value of 80 since we had already set 80 as the default value for this column.

Frequently Asked Questions

Q: What is the SQL DEFAULT constraint?
A: The DEFAULT constraint automatically assigns a predefined value to a column when no value is provided in an INSERT statement, preventing NULL from being stored unintentionally.

Q: Can you use a function as a DEFAULT value?
A: Yes. Many databases allow expressions and functions as default values. For example, DEFAULT GETDATE() in SQL Server sets the default to the current timestamp.

Q: Can you change a DEFAULT constraint after a table is created?
A: Yes. You can use ALTER TABLE to modify or drop a DEFAULT constraint on an existing column without needing to recreate the table.

Q: Does DEFAULT override an explicit NULL insert?
A: No. If you explicitly insert NULL into a column, the DEFAULT is not used. The DEFAULT only applies when the column is omitted from the INSERT statement entirely.

Next: SQL UNIQUE Constraint

This page was last updated on March 19, 2026.




Copyright © 2026   1keydata.com   All Rights Reserved     Privacy Policy     About   Contact