SQL

[Basic Grammar] SQL INSERT INTO Statement

grace21110 2023. 10. 14. 11:03
반응형

The INSERT INTO statement is used to insert new records in a table. 

 

  • Syntax 
  • There are two different ways to write INSERT INTO statement: 

1) Specify both the column names and the values to be inserted: 

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

2) When you do not need to specify the column names in the SQL query: 

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

 

Example 

Insert a new records in the "Products" table: 

INSERT INTO Products (ProductName, Address, City, PostalCode, Country)
VALUES ('Computer', 'Guro', 'Seoul', '08043', 'South Korea');

 

  • Insert Multiple Rows 

We use the same INSERT INTO statement but with multiple rows:

Example 

INSERT INTO Brands (BrandName, ContactName, Address, City, Country)
VALUES 
('Nike', 'Grace', 'Guro', 'Seoul', 'South Korea'),
('Adidas', 'David', 'Gangnam', 'Seoul', 'South Korea');

** Do not forget comma(,) when you write INSERT INTO with multiple rows.**

 

 

Exercises 

Insert a new product record into the "Products" table with the following details: ProductID 105, ProductName 'New Product', Category 'Electronics', and Price $99.99.

INSERT INTO Products(ProductID, ProductName, Category, Price)
VALUES (105, 'New Product',  'Electronics', $99.99);

** On the first line of INSERT INTO statement, there is no comma at the end of the parentheses **

** No single quotation marks on the columns **