Home  >  Article  >  Database  >  What is sql insert statement

What is sql insert statement

青灯夜游
青灯夜游Original
2023-02-01 16:18:5136684browse

SQL insert statement is "INSERT INTO", which is used to insert new data rows into the table. There are two basic syntaxes "INSERT INTO table name (field name 1, field name 2,... field name N) VALUES (value 1, value 2,... value N);" and "INSERT INTO table name VALUES (value 1, value 2,... value N);"; if the field name is omitted, ensure that the inserted The order of the values ​​is the same as the order of the fields in the table.

What is sql insert statement

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

SQL INSERT INTO statement: Insert data

The INSERT INTO statement has two basic uses

1 ) Insert data according to the specified column, the syntax is as follows:

INSERT INTO table_name (column1, column2, column3,...columnN) 
VALUES (value1, value2, value3,...valueN);
  • column1, column2, column3,...columnN represents the column name (field name) of the data to be inserted

  • value1, value2, value3,...valueN represents the value corresponding to each column.

2) Insert data for all columns, the syntax is as follows:

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

When adding data for all columns in the table, it does not need to be in the SQL statement Specify the column names, but make sure you insert the values ​​in the same order as the columns in the table.

INSERT insert statement example

1. Insert a single row of data

Example 1 : Insert a new course information into the course table

Method 1: You do not need to specify a column name, but the inserted value must be inserted in the order of the existing fields in the table

insert into suject values(4,4,'物理')

Execution result:

What is sql insert statement

Method 2: You can specify the column name, and the values ​​inserted later will be in the same order as the specified column name

insert into suject(Cid,Cname,Tid) values(5,'历史',5)

Execution result :

What is sql insert statement

2. Insert multiple rows of data

  • When multiple rows of data need to be inserted, separate them with commas. That’s it

Example 2: Insert three new course information into the course schedule

Method 1: You don’t need to specify a column name, but the inserted value The

insert into suject values(4,4,'物理'),(5,5,'历史'),(6,6,'科学');

must be inserted in the order of the existing fields in the table. The execution result is:

What is sql insert statement

Method 2: You can specify the column name, and the value inserted later will be the same as the specified one. The column names must be in the same order

insert into suject(Tid,Cname,Cid) values(7,'音乐',7),(8,'体育',8),(9,'书画',9);

Execution results:

What is sql insert statement

Example 3: Insert three new teacher information into the teches table

insert into teachers(Tname,Tid) values('黄江',4),('刘玲',5),('赵国',6);

Execution results:

What is sql insert statement

[Related recommendations: mysql video tutorial]

The above is the detailed content of What is sql insert statement. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn