PL/SQL DEFAULT Constraint
Last Updated :
23 Jul, 2025
In PL/SQL the DEFAULT constraint is used to automatically assign a default value to a column when an explicit value is not provided during the insertion of a new row. This feature is particularly useful for ensuring that columns always have a meaningful value even when the user or application provides none.
In this article, we will explore the concept of DEFAULT
constraints in PL/SQL, providing detailed explanations and examples to illustrate their usage and benefits.
PL/SQL DEFAULT Constraint
The DEFAULT
constraint specifies a default value for a column in a table. When a new row is inserted into the table, and a value is not provided for a column with a DEFAULT
constraint, the database automatically assigns the predefined default value to that column.
This ensures that the column will always contain a valid value, even if the user or application does not provide one during data insertion.
Syntax:
The syntax for defining a DEFAULT constraint is
column_name data_type [DEFAULT default_value]
Where:
column_name
is the name of the column for which the default value is being set.
data_type
is the data type of the column (e.g., NUMBER
, VARCHAR2
, DATE
).
default_value
is the value to be automatically assigned if no value is provided during an insert operation.
Examples of Using the DEFAULT Constraint
In this section, we'll examine practical examples of how the DEFAULT
constraint is applied in PL/SQL. These examples will demonstrate how to automatically assign default values to columns when no explicit value is provided during data insertion.
Example 1: Default Value for Numeric Columns
Suppose we have a table named employees
where we want the bonus
column to default to a value of 1000
if no value is provided. This ensures that every employee gets a bonus, even if it's not specified during data insertion.
Table Creation:
CREATE TABLE employees (
emp_id NUMBER PRIMARY KEY,
emp_name VARCHAR2(50),
bonus NUMBER DEFAULT 1000
);
Inserting Data
INSERT INTO employees (emp_id, emp_name) VALUES (1, 'John Doe');
Output:
emp_id | emp_name | bonus |
---|
1 | John Doe | 1000 |
---|
Explanation:
In this example, we inserted a row into the employees
table without specifying a value for the bonus
column. Because of the DEFAULT
constraint, the bonus
column automatically received the value 1000
. This feature helps ensure that even if the user forgets to enter a bonus value, the database maintains a meaningful value.
Example 2: Default Value for String Columns
Now, let’s consider a products
table where the status
column should default to '
available
'
if no status is provided. This is useful for inventory systems where most products are generally available unless specified otherwise.
Table Creation:
CREATE TABLE products (
product_id NUMBER PRIMARY KEY,
product_name VARCHAR2(100),
status VARCHAR2(20) DEFAULT 'available'
);
Inserting Data
INSERT INTO products (product_id, product_name) VALUES (101, 'Laptop');
Output:
product_id | product_name | status |
---|
101 | Laptop | available |
---|
Explanation:
Here, we inserted a new product into the products
table without providing a status. The DEFAULT
constraint automatically set the status
to '
available
'
. This approach simplifies data entry by reducing the need to repeatedly specify a common value.
Examples Using the NOT Operator with DEFAULT
The NOT
operator can be used in conjunction with the DEFAULT
constraint in queries to retrieve rows where a column does not have the default value.
This can be helpful for identifying records that deviate from the expected norm.
Example 1:
Suppose we have an employees
table where the bonus
column is set with a default value of 1000
. This means that any time a new record is inserted into the table without explicitly specifying a value for the bonus
column, the value 1000
is automatically assigned to that column.
Now, let's say you want to identify all employees who have a bonus that is different from the default value of 1000
. You can use the NOT
operator (or the !=
operator, which has the same effect in this context) to filter out these records.
Query:
SELECT * FROM employees
WHERE bonus != 1000;
Output:
emp_id | emp_name | bonus |
---|
2 | John Doe | 1500 |
Explanation:
This query will return all records where the bonus
column has a value other than the default 1000
. These are the rows where the bonus has been explicitly set to a different amount, which may indicate special cases or exceptions.
Example 2:
Consider a products
table where the status
column has a default value of 'available'
. This setup ensures that when a new product is added to the table without explicitly setting the status
, it will automatically be marked as '
available
'
.
However, we might want to identify products that have a status different from 'available'
, such as products that are sold out, discontinued, or not yet released. This can be done using a query that employs the NOT
operator or the !=
operator to filter out these records.
Let's assume the following data is present in the products
table:
Product _id | product_name | status |
---|
101 | Laptop | available |
102 | Tablet | sold |
103 | Smartphone | available |
104 | Monitor | discontinued |
Query:
SELECT * FROM productsWHERE status != 'available';
Output:
product_id | product_name | status |
---|
102 | Tablet | sold |
104 | Monitor | discontinued |
Explanation:
This query retrieves products whose status is not 'available'
. It’s useful for monitoring inventory to identify products that are either sold out or not yet available.
Conclusion
The DEFAULT constraint in PL/SQL is a powerful tool for ensuring that columns have meaningful default values, which helps maintain data integrity and simplifies data entry. By using default values, you can ensure that your database columns always have valid data, even if the application does not explicitly provide it.
Similar Reads
PL/SQL Tutorial Explore this PL/SQL tutorial to effortlessly learn PL/SQL â It is perfect for beginners and experienced ones. Whether you're new to it or diving deep, this interactive guide simplifies database programming.Learn hands-on with practical examples, making your journey fun and effective. Learn PL/SQL's
8 min read
PL/SQL Fundamentals
PL/SQL Control & Loops
Decision Making in PL/SQLPL/SQL (Procedural Language/Structured Query Language) is Oracle's extension to SQL that allows for procedural programming within databases. It features various conditional statements to control the flow of execution based on specific conditions.In this article, We will learn about the various PL/SQ
5 min read
PL/SQL LoopsPL/SQL stands for Procedural Language Extension to the Structured Query Language and it is designed specifically for Oracle databases it extends Structured Query Language (SQL) capabilities by allowing the creation of stored procedures, functions, and triggers. It is a block-structured language that
5 min read
PL/SQL For LoopPL/SQL stands for Procedural Language/ Structured Query Language. It has block structure programming features. With PL/SQL, you can fetch data from the table, add data to the table, make decisions, perform repetitive tasks, and handle errors.PL/SQL supports SQL queries. To fetch records, process dat
4 min read
PL/SQL While LoopOracle PL/SQL provides various loop structures that help developers execute a block of code multiple times based on certain conditions. The main loop structures include LOOP ... END LOOP, WHILE ... END LOOP, and FOR ... END LOOP. In this article, we will explore the WHILE loop in detail, including i
5 min read
PL/SQL Queries & Clauses
PL/SQL SELECT INTO Existing TablePL/SQL is a programming language that is used alongside SQL for writing procedural code such as stored procedures, functions, triggers, and packages within the Oracle Database. It was developed by Oracle Corporation and is widely used in database programming.PL/SQL is a programming language that has
5 min read
PL/SQL INSERT StatementThe PL/SQL INSERT statement is vital for adding new records to a database table. By specifying the table's name and providing values for its columns, users can populate their database with essential information. This functionality enables efficient data entry and ensures the completeness of datasets
3 min read
PL/SQL UPDATE StatementThe UPDATE statement in the PL/SQL(Procedural Language/ Structural Query Language) is the powerful SQL (Structured Query Language) command used to modify the existing data in the database table. In this article, we will explain the PL/SQL UPDATE Statement, its syntax, and examples in detail.PL/SQL U
6 min read
PL/SQL DELETE StatementIn PL/SQL(Procedural Language/Structured Query Language), the DELETE statement is the powerful command used to remove one or more records from the database table. It is an essential part of database management and enables the users to efficiently manage and maintain the data integrity by selectively
6 min read
PL/SQL WHERE ClauseThe WHERE clause in PL/SQL is essential for filtering records based on specified conditions. It is used in SELECT, UPDATE, and DELETE statements to limit the rows affected or retrieved, allowing precise control over data manipulation and retrieval.In this article, We will learn about the WHERE Claus
3 min read
PL/SQL ORDER BY ClauseIn PL/SQL, the ORDER BY clause is a vital tool that allows for the sorting of query results by one or more columns, either in ascending or descending order. In this article, We will learn about ORDER BY clause in PL/SQL, its syntax, functionality, and practical usage through examples.Understanding O
7 min read
PL/SQL GROUP BY ClauseThe GROUP BY clause in PL/SQL is a powerful tool used to organize data into aggregated groups based on one or more columns. It is essential for performing summary operations on large datasets, enabling efficient data analysis by grouping rows that share common values.In this article, We will learn a
7 min read
PL/SQL Operators
PLSQL : || OperatorThe string in PL/SQL is actually a sequence of characters with an optional size specification. The characters could be numeric, letters, blank, special characters or a combination of all. The || Operator in PLSQL is used to concatenate 2 or more strings together. The result of concatenating two char
2 min read
PL/SQL AND OperatorThe PL/SQL AND operator is used to combine multiple conditions in a WHERE clause of an SQL query. It allows you to refine your query by ensuring that all specified conditions are met. AND queries which help in filtering data more precisely and can be crucial for retrieving accurate results from a da
7 min read
PL/SQL LIKE OperatorThe PL/SQL LIKE operator is a powerful tool used in SQL queries to search for patterns in character data. It allows you to match strings based on specific patterns defined by wildcards. This operator is commonly used in SELECT, UPDATE, and DELETE statements to filter records based on partial or comp
6 min read
PL/SQL NOT OperatorPL/SQL, an extension of SQL in Oracle, offers various operators that allow us to perform logical operations on data. One such operator is the NOT operator, which is used to negate a condition, meaning it will return true if the condition is false and vice versa.The NOT operator is commonly used in c
6 min read
PL/SQL IS NULL OperatorThe IS NULL operator is a fundamental tool in PL/SQL used to determine the presence of NULL values in database columns. Understanding how to effectively use the IS NULL operator is crucial for database management, as it allows developers and analysts to identify and handle records with missing or un
4 min read
PL/SQL CASE StatementPL/SQL stands for Procedural Language Extension to the Structured Query Language and it is designed specifically for Oracle databases it extends Structured Query Language (SQL) capabilities by allowing the creation of stored procedures, functions, and triggers. The PL/SQL CASE statement is a powerfu
4 min read
PL/SQL Program Units
PL/SQL Data Structures & Error Handling
Index in PL/SQLPL/SQL, Oracle's extension to SQL, combines SQL with procedural programming features like loops, conditionals, and exception handling. It enables developers to create stored procedures, functions, triggers, and other database applications. As a block-structured language, PL/SQL allows seamless integ
5 min read
Exception Handling in PL/SQLAn exception is an error which disrupts the normal flow of program instructions. PL/SQL provides us the exception block which raises the exception thus helping the programmer to find out the fault and resolve it. There are two types of exceptions defined in PL/SQL User defined exception. System defi
7 min read
PL/SQL RecordsPL/SQL stands for Procedural Language/Structured Query Language. It is an extension of the Structured Query Language (SQL). A core feature of PL/SQL is its ability to work with complex data types, including PL/SQL records. PL/SQL records enable developers to group related data elements, creating a s
10 min read
Cursors in PL/SQLA Cursor in PL/SQL is a pointer to a context area that stores the result set of a query. PL/SQL CursorsThe cursor is used to retrieve data one row at a time from the results set, unlike other SQL commands that operate on all rows at once. Cursors update table records in a singleton or row-by-row man
3 min read