The 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, facilitating various database operations and analyses.
PL/SQL INSERT Statement
In the world of database management systems, the INSERT PL/SQL statement allows adding new data into database tables. It allows programmers to insert data in an organized way which ultimately improves the form of interaction with database systems
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Here,
- table_name: Name of the table where data will be inserted.
- (column1, column2, ...): Names of the columns where data will be inserted.
- VALUES (value1, value2, ...): Values to be inserted into the respective columns listed in the (column1, column2, ...) clause.
Example of PL/SQL INSERT Statement
Consider a table named employees with columns employee_id, first_name, last_name, and department:
CREATE TABLE employees (
employee_id INT,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50)
);
INSERT INTO employees (employee_id, first_name, last_name, department)
VALUES (101, 'John', 'Doe', 'Sales');
Output:
+-------------+------------+-----------+------------+
| employee_id | first_name | last_name | department |
+-------------+------------+-----------+------------+
| 101 | John | Doe | Sales |
+-------------+------------+-----------+------------+
Explanation: The output displays a single row representing an employee with an ID of 101, first name 'John', last name 'Doe', and department 'Sales'. Each column's value aligns with the respective column headers: employee_id, first_name, last_name, and department.
Example of Using SELECT Statement
The PL/SQL INSERT statement can also act based on the SELECT query, which is used to select data from another table. undefined
For example, assume we have another table of name new_employees with the same columns as the employees table. We would like to load data in the new_employees table to the employees table.
-- Inserting multiple rows into the 'employees' table
-- Every line represents employee_id, first_name, last_name, and department as its constituent components.
INSERT INTO employees (employee_id, first_name,last_name, department)
VALUES
(102, 'Alice', 'Smith', 'Marketing')-- Row 1
(103, 'Bob', 'Johnson', 'Finance') -- Row 2
(104, 'Emily', 'Brown' , 'HR'); -- Row 3
-- Creating a new table named 'Newemployees' with the same structure as the 'employees' table
CREATE TABLE Newemployees (
employee_id INT,
first_name VARCHAR(50),
last_name VARCHAR(50),
department VARCHAR(50)
);
-Select All rows in the 'employees' table and put data into the 'Newemployee' table.
-- The condition '1 != 2' is always true and it won't filter any rows in the data set.
INSERT INTO Newemployees
SELECT * FROM employees
WHERE 1 != 2;
Output:
+-------------+------------+-----------+------------+
| employee_id | first_name | last_name | department |
+-------------+------------+-----------+------------+
| 102 | Alice | Smith | Marketing |
| 103 | Bob | Johnson | Finance |
| 104 | Emily | Brown | HR |
+-------------+------------+-----------+------------+
Explanation: The output displays a new table named Newemployees, identical to the employees table, with employee data copied over. Newemployees now contain three rows, each representing an employee's ID, first name, last name, and department, derived from the employees table. This action ensures Newemployees mirror the original employee records without any filtering or alterations.
Conclusion
An insert statement within PL/SQL is a flexible, as well as efficient, way of putting new data into the table of the database. Developers are provided with the INSERT statement with which the data entry can automatically be inserted into the database, the data can be retrieved from another table or multiple rows can be inserted at the same time; in this way, data management processes are enhanced, the systems are made more functional and the development process is much more organized.
Similar Reads
SQL INSERT INTO Statement
The SQL INSERT INTO statement is one of the most commonly used commands for adding new data into a table in a database. Whether you're working with customer data, products, or user details, mastering this command is crucial for efficient database management. Letâs break down how this command works,
6 min read
PL/SQL CASE Statement
PL/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
MySQL INSERT INTO Statement
In MySQL, the INSERT INTO statement is essential for adding new data rows to a table in a database. This is important for setting up initial data in tables and for adding new records as needed when working with the database. Understanding how to use the INSERT INTO statement is key for managing and
6 min read
PL/SQL DELETE Statement
In 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
7 min read
PL/SQL CONTINUE Statement
PL/SQL is a block-structured language that enables developers to combine the power of SQL with procedural statements. All the statements of a block are passed to the Oracle engine all at once which increases processing speed and decreases the traffic. A key component of loop control in PL/SQL is the
4 min read
PL/SQL GOTO Statement
PL/SQL also known as Procedural Language/Structured Query Language, PL/SQL is a powerful programming language used in Oracle databases to do interaction with data. One of its features is the GOTO statement, which helps control how a program flows by allowing it to jump to specific statements within
5 min read
SQL INSERT INTO SELECT Statement
In SQL, the INSERT INTO statement is used to add or insert records into the specified table. We use this statement to insert data directly into a table by specifying column names in a specific order. The SELECT statement is used to retrieve data from the table, and it can be used in conjunction with
6 min read
SQL SELECT INTO Statement
The SELECT INTO statement in SQL is a powerful and efficient command that allow users to create a new table and populate it with data from an existing table or query result in a single step. This feature is especially useful for creating backups, extracting specific subsets of data, or preparing new
5 min read
SQL CASE Statement
The CASE statement in SQL is a versatile conditional expression that enables us to incorporate conditional logic directly within our queries. It allows you to return specific results based on certain conditions, enabling dynamic query outputs. Whether you need to create new columns, modify existing
4 min read
Insert Statement in MS SQL Server
The SQL Server INSERT statement is a fundamental command used to add new rows of data to a table. Whether we are inserting specific values, utilizing default values or copying data from another table. In this guide, weâll explore various ways to use the Insert statement in MS SQL Server with the hel
4 min read