This tutorial provides a comprehensive guide on the SQL INSERT INTO statement, a fundamental command for adding new records to existing tables in relational databases. You will gain a solid understanding of its uses and nuances through clear explanations and practical examples.



What Is SQL INSERT INTO Statement?

The SQL INSERT INTO statement is a fundamental command that allows you to add one or more records to an existing table within a relational database. This command is essential for populating and maintaining your database with relevant data. Here's a breakdown of the basic syntax:

Syntax:

INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);

Let's break down each component of the above syntax:

  • INSERT INTO: Keyword signifying insertion of a new record.
  • table_name: Name of the table where the new record will be inserted.
  • (column1, column2, column3, ...): Optional list of specific columns to insert data into.
  • VALUES (value1, value2, value3, ...): List of data values corresponding to the respective columns.

Using Shorter Syntax

You can also use a shorter syntax to insert data into all the table's columns. In this case, you do not need to specify the column names, but you need to ensure that the order and number of the values match the order and number of the columns in the table.

Syntax:

INSERT INTO table_name
VALUES (value1, value2, value3, ...);

SQL INSERT INTO Statement Examples

Consider a "Customers" table with columns like CustomerID, CustomerName, ContactName, Address, City, PostalCode, and Country.

Inserting a Single Record

INSERT INTO Customers (CustomerID, CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES (4, 'Global Spices', 'Neil Sharma', '123 Liberty St.', 'New York', '10005', 'USA');

Inserting Multiple Records

INSERT INTO Customers (CustomerID, CustomerName, ContactName, Address, City, PostalCode, Country)
VALUES (5, 'Sunrise Electronics', 'Maya Patel', '45 Silicon Ave.', 'San Jose', '95113', 'USA'),
       (6, 'Green Valley Textiles', 'Raj Gupta', '78 Market St.', 'Houston', '77002', 'USA'),
       (7, 'Oceanic Imports', 'Anita Singh', '32 Harbor Way', 'Seattle', '98104', 'USA');

Inserting Data into All Columns

INSERT INTO Customers
VALUES (8, 'Lotus Home Decor', 'Arun Kumar', '67 Maple Dr.', 'Atlanta', '30301', 'USA');

Conclusion

You have learned how to use the SQL INSERT INTO statement to add new records to a table in a database. Understanding the basic syntax and advanced features allows you to manage and populate your tables with the necessary information effectively.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram