DEV Community

Kailas Warade
Kailas Warade

Posted on

SQL DEFAULT Constraint: How Can SQL Automatically Assign Values When You Don't Provide One?

 Imagine you are working on an employee database.

Your company wants every new employee to have a status. Whenever someone adds a new employee without specifying a status, the database should automatically assign 'ACTIVE'.

Can SQL handle this without requiring users to enter the status every time?

Yes. This is where the DEFAULT constraint becomes useful.

Let's understand it with a simple Oracle SQL example.

  1. Create an Employee Table

First, create an employee table with three columns:

  • "employee_id" – Employee's unique ID
  • "employee_name" – Employee's name
  • "status" – Employee's current status

We will use a PRIMARY KEY constraint, a NOT NULL constraint and a DEFAULT constraint.

CREATE TABLE employees (
employee_id NUMBER PRIMARY KEY,
employee_name VARCHAR2(50) NOT NULL,
status VARCHAR2(20) DEFAULT 'ACTIVE'
);

Here, the "status" column has a DEFAULT value of "'ACTIVE'".

This means Oracle can automatically assign ACTIVE when an INSERT statement does not provide a value for this column.

  1. Insert Employee Records

Let's insert three employees.

INSERT INTO employees
VALUES (101, 'Rahul', 'ACTIVE');

INSERT INTO employees
VALUES (102, 'Priya', 'INACTIVE');

INSERT INTO employees
VALUES (103, 'Amit', 'ACTIVE');

Now, let's display the records.

SELECT * FROM employees;

Expected Output:

EMPLOYEE_ID| EMPLOYEE_NAME| STATUS
101| Rahul| ACTIVE
102| Priya| INACTIVE
103| Amit| ACTIVE

  1. What Happens When You Don't Provide a Status?

Now comes the interesting part.

Suppose a new employee named Neha joins the company.

You insert her record without specifying the status.

INSERT INTO employees
(employee_id, employee_name)
VALUES (104, 'Neha');

Notice that the INSERT statement does not include the status column.

What will Oracle do?

It will automatically use the DEFAULT value defined when the table was created.

Let's check the result.

SELECT * FROM employees;

Expected Output:

EMPLOYEE_ID| EMPLOYEE_NAME| STATUS
101| Rahul| ACTIVE
102| Priya| INACTIVE
103| Amit| ACTIVE
104| Neha| ACTIVE

Neha's status is automatically set to ACTIVE.

We didn't provide the value, but Oracle used the DEFAULT constraint.

  1. What If You Provide a Different Value?

A DEFAULT value does not force every employee to have the same status.

You can still provide your own value.

For example:

INSERT INTO employees
(employee_id, employee_name, status)
VALUES (105, 'Kiran', 'INACTIVE');

Here, Oracle uses INACTIVE instead of ACTIVE because we explicitly provided a value.

  1. Important: What If You Insert NULL?

There is one more thing worth understanding.

What happens if you explicitly insert NULL?

INSERT INTO employees
(employee_id, employee_name, status)
VALUES (106, 'Amit', NULL);

The DEFAULT constraint does not automatically replace an explicitly supplied NULL.

The status column will contain NULL because no NOT NULL constraint has been defined on it.

This is an important difference between omitting a column and explicitly inserting NULL.

  1. SQL Challenge: Can You Predict the Result?

Consider the following statement:

INSERT INTO employees
(employee_id, employee_name)
VALUES (107, 'Rohit');

The status column has a DEFAULT value of 'ACTIVE'.

What will happen?

A. The record will be inserted with status ACTIVE.

B. The INSERT will fail because status is missing.

C. Oracle will automatically assign NULL to the status column.

D. Oracle will insert the record with status INACTIVE.

Correct Answer: A.

The record will be inserted with status ACTIVE because the status column was omitted from the INSERT statement.

  1. Important Points About the DEFAULT Constraint

Here are a few things to remember:

  • DEFAULT assigns a predefined value when no value is provided for a column.
  • It is useful for employee status, order status, registration dates and other commonly used values.
  • An explicitly provided value takes priority over the DEFAULT value.
  • A DEFAULT constraint does not prevent NULL values from being inserted explicitly.
  • A table can have multiple columns with DEFAULT constraints.

For example, an employee table can use ACTIVE as the default status, while an order table can use PENDING as the default order status.

This helps maintain consistent values and reduces unnecessary manual data entry.

  1. When Should You Use DEFAULT Constraints?

DEFAULT constraints are useful when a column frequently needs the same starting value.

Some practical examples include:

Column| Example DEFAULT value
Employee status| ACTIVE
Order status| PENDING
Payment status| UNPAID
Is_active| 1
Created date| CURRENT_DATE

Choose default values according to your application's business requirements.

A small SQL concept, but an important one when designing reliable database tables and maintaining consistent data.

Learn More About SQL Constraints

Want to understand how PRIMARY KEY, FOREIGN KEY, NOT NULL, UNIQUE and other SQL constraints work?

Explore this practical SQL Constraints guide:

"SQL Constraints – Complete Guide" (https://www.sankalandtech.com/Tutorials/sql-plsql-faq-interview/sql-constraints-primary-foreign-key-faq.html)

Final Question

Have you used DEFAULT constraints in your database tables?

Can you think of another real-world situation where automatically assigning a default value would be useful?

Share your example in the comments.


SQL #OracleSQL #SQLConstraints #DefaultConstraint #LearnSQL #DatabaseDesign #SQLTutorial #SQLInterviewQuestions

Top comments (0)