Home > Database > Mysql Tutorial > How to write sql insert statement

How to write sql insert statement

青灯夜游
Release: 2022-04-08 20:05:48
Original
136386 people have browsed it

How to write the sql insert statement: 1. "insert into table name values ​​(value 1, value 2...);", insert the specified data into the existing table; 2. "Insert into table Name 1 select * from table name 2;" to find out the data in another table and insert it into the existing table.

How to write sql insert statement

SQL's insert statement is an insert statement, which is used to insert new rows (new data) into the table.

There are three ways to write the insert statement:

1, insert into...valuesStatement

insert...values The statement inserts the specified data into the existing table, which can be divided into two situations:

1). There is no need to specify the column name of the data to be inserted, just provide the inserted value:

insert into table_name
values (value1,value2,value3,...);
Copy after login

2), you need to specify the column name and the value to be inserted:

insert into table_name (column1,column2,column3,...)
values (value1,value2,value3,...);
Copy after login

2, insert into...setstatement

Like the insert...values statement, the insert...set statement also inserts the specified data into an existing table. Basic syntax:

Insert into table_name set column1=value1,column2=value2,........;
Copy after login

3, insert into...select statement

insert...select statement is to query data in another table out and inserted into a ready-made table. Basic syntax:

Insert into table_name select * from table_name2;
Copy after login

Example:

mysql> desc students;
+-------+-------------+------+-----+---------+-------+
| Field     | Type  | Null  | Key    | Default | Extra |
+-------+-------------+------+-----+---------+-------+ 
|sid        |int(11)    |YES        |   |NULL   |   |
|sname  |varchar(20)    |YES    |   |NULL   |   |
+-------+-------------+------+-----+---------+-------+

Insert into students values(1,’aaa’);
Insert into students set sid=2,sname=‘bbb’;
Insert into students select * from students_bak;

mysql> select * from students;
+------+-------+
|sid |sname|
+------+-------+
| 1 | aaa |
| 2 | bbb |
| 3 | ccc |
+------+-------+
Copy after login

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

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template