INSERT statement in SQL
The INSERT statement in SQL is used to add new data rows to a table. Users can insert data into the table in 2 ways. Refer to each method in detail in the table in the following article.
INSERT statement in SQL
Method 1: Insert data directly into the table
The syntax of the INSERT statement is:
INSERT INTO TABLE_NAME
[ (col1, col2, col3,…colN)]
VALUES (value1, value2, value3, … valueN);
Inside:
col1, col2, … colN is the name of the column in the table that you want to insert data.
While inserting a row, if you add values to all table columns, you do not need to provide the column name in the SQL query. But you need to make sure the order of the values is the same as the columns in the table. The INSERT query in SQL has the following form:
INSERT INTO TABLE_NAME
VALUES (value1, value2, value3, … valueN);
For example:
If you want to insert an table into the employee list, the query is:
INSERT INTO employee (id, name, dept, age, salary location) VALUES (105, ‘Srinath’, ‘Aeronautics’, 27, 33000);
Note:
When adding rows, only date characters or values are enclosed in single quotes.
If inserting data into all columns, the column name may be omitted. The INSERT statement in SQL above can be written as follows:
INSERT INTO employee
VALUES (105, ‘Srinath’, ‘Aeronautics’, 27, 33000);
Method 2: Insert data into the table through the SELECT statement in SQL
The syntax for the INSERT statement in SQL is:
INSERT INTO table_name
[(column1, column2, … columnN)]
SELECT column1, column2, … columnN
FROM table_name [WHERE condition];
For example, to insert a row into the temporary employee list, the SQL query looks like this:
INSERT INTO employee (id, name, dept, age, salary location) SELECT emp_id, emp_name, dept, age, salary, location
FROM temp_employee;
If inserting data into all columns, the above INSERT statement can be written as:
INSERT INTO employee
SELECT * FROM temp_employee;
Note: Assuming the temp_employee table has columns emp_id, emp_name, dept, age, salary, location in the order provided above and has the same data type.
Important note:
1. When adding a new row, you need to make sure the data type (datatype) of the value and the column must match.
2. Follow the integrity constraints defined for the table, if any.
Thus, the article on Taimienphi.vn has just introduced you the INSERT statement in SQL. The next articles Taimienphi.vn will introduce you more DELETE statement in SQL, JOINS statement, RENAME command, … in SQL.
https://thuthuat.taimienphi.vn/lenh-insert-trong-sql-33105n.aspx
Also readers can refer to some articles on Taimienphi.vn to learn more about SQL Server such as How to deploy SQL Server through PowerShell DSC Please. Good luck !
.