PL/SQL DEFAULT Constraint
Last Updated :
30 Aug, 2024
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
SQL | DEFAULT Constraint In SQL, maintaining data integrity and ensuring consistency across tables is important for effective database management. One way to achieve this is by using constraints. Among the many types of constraints, the DEFAULT constraint plays an important role in automating data insertion and ensuring tha
3 min read
MySQL DEFAULT Constraint The MySQL DEFAULT constraint returns the default value for a table column. The DEFAULT value of a column is a value used in the case, when there is no value specified by the user. To use this function there should be a DEFAULT value assigned to the column. Otherwise, it will generate an error. Synta
3 min read
SQL DROP CONSTRAINT In SQL, constraints are used to ensure data integrity and define rules for the data in our database tables. These rules include ensuring uniqueness, maintaining referential integrity, and validating data with conditions. By applying constraints such as primary key, foreign key, unique, and check con
4 min read
SQL NOT NULL Constraint In SQL, constraints are used to enforce rules on data, ensuring the accuracy, consistency, and integrity of the data stored in a database. One of the most commonly used constraints is the NOT NULL constraint, which ensures that a column cannot have NULL values. This is important for maintaining data
3 min read
PL/SQL Constants In Oracle's PL/SQL (Procedural Language/SQL), constants play an important role in maintaining the integrity and predictability of the values used throughout the program. Declaring the constants enhances the program readability and reduces the errors, it also provides the performance benefits in cert
5 min read
PL/SQL CHECK Constraint In Oracle's PL/SQL, the CHECK constraint is an essential feature that enforces data integrity by ensuring that the values stored in a table's columns meet specific conditions. By applying logical rules to the data, this constraint helps maintain the validity and consistency of the database, preventi
4 min read
SQL PRIMARY KEY Constraint The PRIMARY KEY constraint in SQL is one of the most important constraints used to ensure data integrity in a database table. A primary key uniquely identifies each record in a table, preventing duplicate or NULL values in the specified column(s). Understanding how to properly implement and use the
5 min read
SQLite NOT NULL Constraint SQLite is a very lightweight and embedded Relational Database Management System (RDBMS). It requires very minimal configuration and it is self-contained. It is serverless, therefore it is a perfect fit for mobile applications, simple desktop applications, and embedded systems. While it may not be a
4 min read
SQL | Constraints SQL constraints are essential elements in relational database design that ensure the integrity, accuracy, and reliability of the data stored in a database. By enforcing specific rules on table columns, SQL constraints help maintain data consistency, preventing invalid data entries and optimizing que
5 min read
SQL FOREIGN KEY Constraint A FOREIGN KEY constraint is a fundamental concept in relational databases, ensuring data integrity by enforcing relationships between tables. By linking a child table to a parent table, the foreign key establishes referential integrity. This constraint ensures that the values in the foreign key colu
5 min read