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 SQLite?
SQLite is a lightweight and self-contained relational database management system in short RDBMS. Its has a server-less architecture which makes it a better option for small desktop and mobile applications. It also requires very low configuration which eventually helps the developer to integrate it i
4 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 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 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 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 Multiple Columns in MySQL?
To update multiple columns in MySQL we can use the SET clause in the UPDATE statement. SET clause allows users to update values of multiple columns at a time. In this article, we will learn how to update multiple columns in MySQL using UPDATE and SET commands. We will cover the syntax and examples,
3 min read
How to Alter a Column from Null to Not Null in SQL Server
In SQL Server, columns are defined with specific constraints, one of which is the nullability of the column whether or not it can hold NULL values. As a database evolves, this setting may need to be changed particularly to ensure that certain data fields are always populated. Altering a column from
4 min read