Home  >  Article  >  Database  >  How to write sql insert statement

How to write sql insert statement

青灯夜游
青灯夜游Original
2019-05-15 13:51:32136376browse

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,...);

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,...);

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,........;

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;

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 |
+------+-------+

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!

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