The INSERT IGNORE statement in MySQL is used to insert data into a table while ignoring errors caused by duplicate key constraints. This ensures that the insertion operation continues without interruption even if some records violate uniqueness constraints.
While Oracle’s PL/SQL does not directly support INSERT IGNORE, similar functionality can be implemented using MySQL. In this article, we will learn about PL/SQL INSERT IGNORE with various examples
What is PL/SQL INSERT IGNORE?
In MySQL, INSERT IGNORE allows you to insert data into a table, ignoring any errors that occur due to duplicate key constraints. This is useful for scenarios where you want to avoid inserting duplicate records but don't want to halt execution if duplicates are encountered
Syntax
INSERT IGNORE INTO table_name (column1, column2)
VALUES (value1, value2);
Key Terms:
- table_name: This is the table into which data will be inserted.
- column1, column2: The columns into which data will be inserted.
- value1, value2: The values to be inserted.
- The
INSERT IGNORE
statement will by pass errors caused by duplicate key constraints, allowing the insertion process to continue without interruption.
PL/SQL INSERT IGNORE Example
In this example, we create two tables Books
and Authors
. The Books
table stores information about books, including their BookID
, BookTitle
, and AuthorID
. The Authors
table holds the AuthorID
and AuthorName
. After creating the tables, we insert records into each.
Books Table:
We start by creating the Books
table. This table has three columns: BookID
, which uniquely identifies each book; BookTitle
, which stores the name of the book; and AuthorID
, which links the book to its author in the Authors
table.
Query:
CREATE TABLE Books (
BookID INT PRIMARY KEY,
BookTitle VARCHAR(100),
AuthorID INT
);
INSERT INTO Books (BookID, BookTitle, AuthorID) VALUES (1, 'The Great Gatsby', 101);
INSERT INTO Books (BookID, BookTitle, AuthorID) VALUES (2, 'To Kill a Mockingbird', 102);
INSERT INTO Books (BookID, BookTitle, AuthorID) VALUES (3, '1984', 103);
Output:
Book TableExplanation:
The Books
table now contains three rows:
- "The Great Gatsby" linked to
AuthorID
101
- "To Kill a Mockingbird" linked to
AuthorID
102
- "1984" linked to
AuthorID
103
Authors Table:
Next, we insert three records into the Books
table. Each record represents a different book, along with the corresponding AuthorID
that links to an author in the Authors
table.
Query:
CREATE TABLE Authors (
AuthorID INT PRIMARY KEY,
AuthorName VARCHAR(100)
);
INSERT INTO Authors (AuthorID, AuthorName) VALUES (101, 'F. Scott Fitzgerald');
INSERT INTO Authors (AuthorID, AuthorName) VALUES (102, 'Harper Lee');
INSERT INTO Authors (AuthorID, AuthorName) VALUES (103, 'George Orwell');
Output:
Author Table
Explanation:
The Authors
table now contains three rows:
AuthorID
101 linked to "F. Scott Fitzgerald"
AuthorID
102 linked to "Harper Lee"
AuthorID
103 linked to "George Orwell"
Example 1: Insert a New Book Ignoring Duplicates
This example demonstrates how to insert a new record into the Books
table while ignoring any potential duplicate BookID
values. If the BookID
already exists in the table, the insertion is ignored, and no error is generated.
Query:
INSERT IGNORE INTO Books (BookID, BookTitle, AuthorID)
VALUES (4, 'Brave New World', 104);
Output:
Insert a New Book Ignoring DuplicatesExplanation:
- If BookID 4 does not exist, it is added to the Books table.
- If BookID 4 already exists, no new entry is made, and no error is raised.
Example 2: Insert Multiple Books with Error Ignoring
This statement attempts to insert multiple books. If any BookID already exists (e.g., ID 1), those records will be ignored without causing an error.
Query:
INSERT IGNORE INTO Books (BookID, BookTitle, AuthorID)
VALUES (5, 'Catch-22', 105),
(1, 'The Catcher in the Rye', 106);
Output:
Insert Multiple Books with Error IgnoringExplanation:
- Book with BookID 5 is added if it doesn't already exist.
- Book with BookID 1 is ignored if it already exists, without causing an error.
Example 3: Insert Author Only if Not Exists
This example shows how to insert a new author into the Authors
table, but only if the AuthorID
does not already exist. If an author with AuthorID
104 already exists, the insertion will be ignored, and no error will be thrown.
Query:
INSERT IGNORE INTO Authors (AuthorID, AuthorName)
VALUES (104, 'Aldous Huxley');
Output:
Insert Author Only if Not ExistsExplanation:
- If
AuthorID
104 does not already exist in the table, a new row is added with the author name "Aldous Huxley."
- If
AuthorID
104 is already present, the insertion is ignored, and no changes are made to the table.
Example 4: Insert Books with Different Conditions
This example demonstrates how to insert multiple books into the Books
table using the INSERT IGNORE
statement. If any of the BookID
values already exist, those specific records will be ignored, preventing duplication and avoiding any errors.
Query:
INSERT IGNORE INTO Books (BookID, BookTitle, AuthorID)
VALUES (6, 'The Road', 107),
(7, 'Neuromancer', 108);
Output:
Insert Books with Different ConditionsExplanation:
- If
BookID
6 or 7 does not exist in the Books
table, the corresponding book records ("The Road" and "Neuromancer") are added successfully.
- If
BookID
6 or 7 already exists, those specific insertions are ignored, and the table remains unchanged for those IDs.
Conclusion
The INSERT IGNORE statement in MySQL is a practical tool for inserting data while avoiding interruptions caused by duplicate key constraints. Although PL/SQL does not directly support INSERT IGNORE, similar behavior can be implemented in MySQL, ensuring that data insertion processes can handle duplicates gracefully. The examples provided demonstrate how to use INSERT IGNORE effectively to maintain data integrity while avoiding errors.
Similar Reads
MySQL INSERT IGNORE
In MySQL, managing data insertion errors is crucial for maintaining data integrity and ensuring smooth database operations. The INSERT IGNORE statement provides a practical solution by allowing records to be inserted without interruption even if some rows would cause errors like duplicate key violat
5 min read
PL/SQL Insert Dates
When managing databases, handling date and time data accurately is important. Date fields are frequently used for storing important information such as user birth dates, product release dates, and event timelines. Oracle PL/SQL provides robust support for date operations, ensuring accurate data stor
4 min read
PL/SQL INSERT INTO
PL/SQL (Procedural Language/Structured Query Language) is Oracle's procedural extension to SQL. It allows us to write complex queries and scripts that include procedural logic, control structures, and error handling. The INSERT INTO statement in PL/SQL is essential for adding new rows of data to tab
6 min read
PL/SQL Insert DateTimes
In PL/SQL, handling date and time values is a common requirement, whether we are working with databases that store transaction times, schedule events, or track user activities. Understanding how to insert these values correctly is crucial for accurate data management and retrieval. In this article,
4 min read
PL/SQL NOT Operator
PL/SQL, an extension of SQL in Oracle, offers various operators that allow us to perform logical operations on data. One such operator is the NOT operator, which is used to negate a condition, meaning it will return true if the condition is false and vice versa.The NOT operator is commonly used in c
6 min read
PL/SQL Injection
PL/SQL injection is a security issue where attackers use harmful code to exploit weak spots in Oracle's PL/SQL applications. If user input isnât properly checked, attackers can access or change sensitive data and even take control of the system.In this article, we will explain what PL/SQL injection
6 min read
PL/SQL Left Join
In the world of database management, efficiently retrieving and combining data from multiple tables is crucial. Among these the LEFT JOIN is particularly important because it includes all records from one table even when there are no corresponding records in another table. In this article, we will P
6 min read
PL/SQL Outer Join
In SQL, joins are used to retrieve data from two or more tables based on a related column. Joins can be categorized into different types: INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL OUTER JOIN, etc. In this article, we will learn about OUTER JOIN in PL/SQL, including its types, syntax, use cases, and ex
5 min read
PL/SQL Comments
Oracle PL/SQL is an advanced procedural language designed to enhance SQL functionalities in Oracle Database environments. Comments can be used to make code more human-readable. It is a kind of note that programmers usually add while writing the code. In this article, we will learn about Comments in
4 min read
PL/SQL INSERT INTO SELECT
In PL/SQL, the INSERT INTO SELECT statement is used to insert data into a table by selecting data from one or more tables. This is a powerful feature for populating tables with data from existing tables or views, making it useful for data migration, reporting, and backup processes.In this guide, we
5 min read