How to Set a Column Value to Null in PL/SQL?
Last Updated :
28 Oct, 2024
In PL/SQL, setting a column value to NULL is a common requirement when working with databases. Understanding how to set column values to NULL is essential for database developers and administrators. In this article, we will look into the concept of setting a column value to NULL in PL/SQL, covering the syntax, examples, and output explanations.
Key Concepts in PL/SQL and Database Management
- PL/SQL: A procedural language extension for SQL by Oracle.
- NULL: Represents a missing or unknown value in a database.
- Database Management: A crucial skill for developers and administrators.
Setting Column Value to NULL in Oracle PL/SQL
To set a column value to NULL in Oracle PL/SQL, the UPDATE statement is used. The syntax for updating a column to NULL is as follows:
UPDATE table_name
SET column_name = NULL
WHERE condition;
Here,
- UPDATE: SQL keyword used to modify or change existing records in a table.
- table_name: The name of the table that you want to update.
- SET: SQL keyword indicating the column you want to modify.
- column_name: The specific column within the table that you want to update.
- NULL: Set the value of the specified column to NULL.
- WHERE: SQL keyword used to filter the rows that will be updated.
- condition: The criteria that the rows must meet to be updated. If omitted, all rows will be updated.
Examples of Updating Column Value to NULL
Example 1: Setting a Column to NULL in the "Employee" Table
We can create the 'Employee' table using the following SQL code, which defines the table structure with columns such as 'Emp_ID,' 'Emp_Name,' 'Salary', and 'department_Id,' specifying appropriate data types for each column to store information about Employee' details.
CREATE TABLE Employee (
Emp_ID INT PRIMARY KEY,
Emp_Name VARCHAR(255),
Salary DECIMAL(10, 2),
department_Id INT
);
INSERT INTO Employee (Emp_ID, Emp_Name, Salary, department_Id)
VALUES (101, 'John Doe', 50000.00, 1),
(102, 'Jane Smith', 60000.00, 2),
(103, 'Robert Johnson', 55000.00, 1),
(104, 'Emily Davis', 48000.00, 2);
Employee TableAnd we have to set the salary to NULL for an employee with Emp_id 101, the following query can be used:
UPDATE Employee
SET Salary = NULL
WHERE Emp_Id = 101;
Output:
Output for Example1We can observe that the salary for the employee with 'Emp_Id' 101 is set to NULL. This operation is useful when you want to mark certain data as unavailable or remove outdated salary information.
Example 2: Setting a Column to NULL in the "Student" Table
We can create the 'Students' table using the following SQL code, which defines the table structure with columns such as 'Students,' 'Subject,' and 'Marks,' specifying appropriate data types for each column to store information about students' marks in different subjects.
CREATE TABLE Student (
Student_ID INT PRIMARY KEY,
Name VARCHAR(50),
Date_of_Birth DATE,
Major VARCHAR(100),
GPA DECIMAL(3, 2)
);
INSERT INTO Student (Student_ID, Name, Date_of_Birth, Major, GPA)
VALUES
(1, 'John Doe', '2000-05-15', 'Computer Science', 3.75),
(2, 'Jane Smith', '2001-02-28', 'Mathematics', 3.90),
(3, 'Michael Johnson', '1999-11-10', 'Physics', 3.60),
(4, 'Emily Williams', '2000-08-05', 'Biology', 3.80);
Student TableAnd we have to set the GPA to NULL for an Student with Student_ID 4, the following query can be used:
UPDATE Student
SET GPA = NULL
WHERE Student_ID = 4;
Output:
Output for Example2We can observe that the GPA for the Student with 'Student_ID' 4 is set to NULL. This approach is helpful when GPA information is temporarily unavailable or requires re-evaluation.
Conclusion
In conclusion, setting a column value to NULL in PL/SQL, using the UPDATE statement, is essential for maintaining data accuracy and integrity. "Data Accuracy" ensures the correctness of records, while "Data Integrity" preserves data reliability over time. Mastering this concept is vital for efficient database management and consistent, error-free data handling in PL/SQL projects.
Similar Reads
How to Set a Column Value to Null in SQL?
You can set a column value to NULL using the SQL UPDATE statement. Through the UPDATE statement, existing records in a table can be changed. The fundamental syntax to set a column value to NULL is as follows. Syntax: UPDATE table_name set column_name=NULL WHERE Conditions; table_name: The name of th
2 min read
How to Set a Column Value to NULL in SQL Server
In the world of database management, SQL Server is a leading and extensively utilized system. A fundamental task within SQL Server is manipulating data within tables, and setting a column value to NULL is a common operation. Whether it's for maintaining data integrity, performing updates, or meeting
4 min read
How to Set a Column Value to NULL in MariaDB
In MariaDB, the NULL represents an unknown value in a column. Changing a column value to NULL is the most common operation performed in MariaDB that allows us to remove existing data in a specific field. It is applicable in different ways including data correction, record inclusions and values setti
4 min read
How to Rename a Column in View in PL/SQL?
Renaming a column in a view can be a challenging task in PL/SQL, as the language does not provide a direct command for this operation. However, there are several effective methods to achieve this goal without compromising data integrity or the structure of the view. In This article, we will explore
4 min read
How to Add Column in View in PL/SQL?
In Oracle PL/SQL, adding a column to a view is not done through the ALTER VIEW command since it does not directly support adding columns. Instead, views are modified by recreating them using the CREATE OR REPLACE VIEW statement, which allows the addition of new columns while retaining the existing s
3 min read
How to Rename a Column in PL/SQL?
Renaming a column in PL/SQL is a fundamental operation in Oracle Database management. It enhances clarity, maintains consistency, or accommodates evolving data requirements. Database administrators can ensure the data integrity and process of streamlining data manipulation by altering the column nam
4 min read
How to Filter Rows Without Null in a Column in SQL?
Here we will see, how to filter rows without null in a column of an MS SQL Server's database table with the help of a SQL query using IS NOT NULL operator. For the purpose of demonstration, we will be creating a demo_orders table in a database called âgeeksâ. Creating the Database: Use the below SQL
2 min read
How to Update a Column in a Table in SQL Server
In the database management area, updating columns in a table is a normal query and it is important software that ensures the accuracy and integrity of data. Whether we are making spelling corrections, editing or altering existing information, or being attentive to changing requirements, carrying out
4 min read
How to Set a NOT NULL Column into NULL in PostgreSQL?
PostgreSQL is an open-source relational database management system in short RDBMS. It is commonly known for its reliability and vast feature set. We can clearly state that it is one of the most powerful RDBMS available. We often create some columns with NOT NULL constraints but later on, there will
4 min read
How to Declare a Variable in PL/SQL?
Declaring variables in PL/SQL is a fundamental step towards building powerful and efficient database applications. Variables act as placeholders for data which enable us to manipulate and store information within our PL/SQL programs. Here, we will explore various methods of declaring variables in PL
5 min read