Home » SQL INSERT STATEMENT

SQL INSERT STATEMENT

SQL INSERT STATEMENT

The SQL INSERT statement is a fundamental command used to add new records or rows into a database table. It allows users to specify the values to be inserted into specific columns within the table, either explicitly or through a query. This statement is crucial for populating databases with initial data or adding new information dynamically. In this introduction, we’ll explore the syntax, usage, and best practices associated with SQL INSERT statements.

The SQL INSERT INTO Statement

The SQL INSERT INTO Statement is used to add new rows of data into a table in the database. Almost all the RDBMS provide this SQL query to add the records in database tables.

Each value in the records we are inserting in a table using this statement should be of the same datatype as the respective column and satisfy the constraints of the column (if any). The values passed using an insert statement should match the number of columns in the table or, the number of columns mentioned in the current query. If any of these conditions are not satisfied, this statement generates an error.

Syntax

There are two basic syntaxes of the SQL INSERT INTO statement which are shown below −

INSERT INTO TABLE_NAME (column1, column2...columnN) 
VALUES (value1, value2...valueN);
SQL

Here, column1, column2, column3, and columnN are the names of the columns in the table into which you want to insert the data.

There is another syntax of INSERT INTO statement where you can specify only column values without column names. But, make sure the order of the values is in the same order as the columns in the table.

Following is the second syntax of the SQL INSERT Query −

INSERT INTO TABLE_NAME 
VALUES (value1,value2...valueN);
SQL

EXAMPLES OF INSERT STATEMENT

Certainly! Let’s create a different table named “Products” with columns for product information such as ProductID, Name, Price, and StockQuantity.

CREATE TABLE Products (
    ProductID INT PRIMARY KEY,
    Name VARCHAR(100),
    Price DECIMAL(10, 2),
    StockQuantity INT
);
SQL
INSERT INTO Products (ProductID, Name, Price, StockQuantity)
VALUES (1, 'Laptop', 999.99, 50);

INSERT INTO Products (ProductID, Name, Price, StockQuantity)
VALUES (2, 'Smartphone', 699.99, 100);

INSERT INTO Products (ProductID, Name, Price, StockQuantity)
VALUES (3, 'Tablet', 299.99, 75);

INSERT INTO Products (ProductID, Name, Price, StockQuantity)
VALUES (4, 'Headphones', 149.99, 200);
SQL

Here are four examples of INSERT statements:

1. Inserting a new product with ID 5, name ‘Smartwatch’, price 199.99, and stock quantity 30:

INSERT INTO Products (ProductID, Name, Price, StockQuantity)
VALUES (5, 'Smartwatch', 199.99, 30);
SQL

2. Inserting a new product with ID 6, name ‘Printer’, price 299.99, and stock quantity 25:

INSERT INTO Products (ProductID, Name, Price, StockQuantity)
VALUES (6, 'Printer', 299.99, 25);
SQL

3. Inserting a new product with ID 7, name ‘Wireless Mouse’, price 29.99, and stock quantity 50:

INSERT INTO Products (ProductID, Name, Price, StockQuantity)
VALUES (7, 'Wireless Mouse', 29.99, 50);
SQL

4. Inserting a new product with ID 8, name ‘External Hard Drive’, price 129.99, and stock quantity 40:

INSERT INTO Products (ProductID, Name, Price, StockQuantity)
VALUES (8, 'External Hard Drive', 129.99, 40);
SQL

Conclusion

In conclusion, SQL INSERT statements are used to add new records or rows into a table within a relational database. They allow you to specify the values for each column of the table, ensuring data integrity and consistency.

INSERT statements follow a structured syntax where you specify the table name, column names (optional), and the values to be inserted into those columns. These values must match the data types and constraints defined for each column in the table.

By using INSERT statements, you can efficiently populate your database tables with new data, whether it’s adding individual records or bulk-inserting multiple rows at once. This capability is fundamental for maintaining and updating the data within a database, supporting various applications and business processes.

Frequently Asked Questions

What is an SQL INSERT statement?

An SQL INSERT statement is a query used to add new records or rows into a table within a relational database.

Do I need to specify column names in an SQL INSERT statement?

It’s optional to specify column names. If omitted, the values must be provided in the same order as the columns in the table.

Can I insert multiple rows with a single INSERT statement?

Yes, you can insert multiple rows with a single INSERT statement by providing multiple sets of values separated by commas, like this:

Can I insert data into specific columns only?

Yes, you can specify the columns into which you want to insert data. Just provide the column names in the INSERT statement, and make sure the values match the data types of those columns.

What happens if I try to insert a duplicate primary key value?

If you attempt to insert a duplicate primary key value, it will result in a constraint violation error, and the insertion will fail. You need to ensure that primary key values are unique.