SQL | INSERT IGNORE Statement
Last Updated :
19 Jul, 2023
We know that a primary key of a table cannot be duplicated. For instance, the roll number of a student in the student table must always be distinct. Similarly, the EmployeeID is expected to be unique in an employee table. When we try to insert a tuple into a table where the primary key is repeated, it results in an error. However, with the INSERT IGNORE statement, we can prevent such errors from popping up, especially when inserting entries in bulk and such errors can interrupt the flow of insertion. Instead, only a warning is generated. Cases where INSERT IGNORE avoids error
- Upon insertion of a duplicate key where the column must contain a PRIMARY KEY or UNIQUE constraint
- Upon insertion of NULL value where the column has a NOT NULL constraint.
- Upon insertion of a row to a partitioned table where the inserted values go against the partition format.
Example: Say we have a relation, Employee. Employee Table:
EmployeeID | Name | City |
---|
15001 | Aakash | Delhi |
---|
15003 | Sahil | Bangalore |
---|
15010 | John | Hyderabad |
---|
15008 | Shelley | Delhi |
---|
15002 | Ananya | Mumbai |
---|
15004 | Sia | Pune |
---|
As we can notice, the entries are not sorted on the basis of their primary key, i.e. EmployeeID. Sample Query:
INSERT OR IGNORE INTO Employee (EmployeeID, Name, City)
VALUES (15002, 'Ram', 'Mumbai');
Output: No entry inserted. Sample Query:
Inserting multiple records
When inserting multiple records at once, any that cannot be inserting will not be, but any that can will be:
INSERT OR IGNORE INTO Employee (EmployeeID, Name, City)
VALUES (15007, 'Shikha', 'Delhi'), (15002, 'Ram', 'Mumbai'), (15009, 'Sam', 'Ahmedabad');
Output: The first and the last entries get inserted; the middle entry is simple ignored. No error is flashed. Disadvantage Most users do not prefer INSERT IGNORE over INSERT since some errors may slip unnoticed. This may cause inconsistencies in the table, thereby causing some tuples to not get inserted without the user having a chance to correct them. Hence, INSERT IGNORE must be used in very specific conditions.
Similar Reads
SQL INSERT INTO Statement The SQL INSERT INTO statement is one of the most essential commands for adding new data into a database table. Whether you are working with customer records, product details or user information, understanding and mastering this command is important for effective database management. How SQL INSERT I
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 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
5 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 help
4 min read
SQL MERGE Statement SQL MERGE Statement combines INSERT, DELETE, and UPDATE statements into one single query. MERGE Statement in SQLMERGE statement in SQL is used to perform insert, update, and delete operations on a target table based on the results of JOIN with a source table. This allows users to synchronize two tab
4 min read