Home > Database > SQL > What is sql insert statement

What is sql insert statement

青灯夜游
Release: 2023-02-01 16:18:51
Original
36768 people have browsed it

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);
Copy after login
  • 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);
Copy after login

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,'物理')
Copy after login

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)
Copy after login

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,'科学');
Copy after login

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);
Copy after login

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);
Copy after login

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!

Related labels:
sql
Statement of this Website
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
Latest Issues
sql file
From 1970-01-01 08:00:00
0
0
0
php - Overhead of prepare vs sql?
From 1970-01-01 08:00:00
0
0
0
Print sql statement
From 1970-01-01 08:00:00
0
0
0
Pass array to SQL insert query using PHP
From 1970-01-01 08:00:00
0
0
0
sql optimization or
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template