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 Interview Questions Are you preparing for a SQL interview? SQL is a standard database language used for accessing and manipulating data in databases. It stands for Structured Query Language and was developed by IBM in the 1970's, SQL allows us to create, read, update, and delete data with simple yet effective commands.
15+ min read
SQL Tutorial SQL is a Structured query language used to access and manipulate data in databases. SQL stands for Structured Query Language. We can create, update, delete, and retrieve data in databases like MySQL, Oracle, PostgreSQL, etc. Overall, SQL is a query language that communicates with databases.In this S
11 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
SQL Joins (Inner, Left, Right and Full Join) SQL joins are fundamental tools for combining data from multiple tables in relational databases. Joins allow efficient data retrieval, which is essential for generating meaningful observations and solving complex business queries. Understanding SQL join types, such as INNER JOIN, LEFT JOIN, RIGHT JO
6 min read
Normal Forms in DBMS In the world of database management, Normal Forms are important for ensuring that data is structured logically, reducing redundancy, and maintaining data integrity. When working with databases, especially relational databases, it is critical to follow normalization techniques that help to eliminate
7 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
ACID Properties in DBMS In the world of DBMS, transactions are fundamental operations that allow us to modify and retrieve data. However, to ensure the integrity of a database, it is important that these transactions are executed in a way that maintains consistency, correctness, and reliability. This is where the ACID prop
8 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read