MySQL:使用INSERT 插入多條記錄

dbasdk發表於2014-08-08

呼叫多次INSERT語句也可以插入多條記錄,但使用這種方法要增加伺服器的負荷,因為執行每一次SQL,伺服器都要同樣對SQL進行分析、最佳化等操作。

MySQL提供了另一種解決方案,就是使用一條INSERT語句來插入多條記錄。這並不是標準的SQL語法,因此只能在MySQL中使用。

 

示例:

INSERT INTO users(name, age) VALUES('姚明', 25), ('比爾.蓋茨', 50), ('火星人',600);

上面的INSERT語句向users表中連續插入了3條記錄。值得注意的是,上面的INSERT語句中的VALUES後必須每一條記錄的值放到一對(…)中,中間使用","分割。

 

INSERT INTO table1(i) VALUES(1),(2),(3),(4),(5);

當然,這種寫法也可以省略列名,這樣每一對括號裡的值的數目必須一致,而且這個數目必須和列數一致。如:

INSERT INTO table1 VALUES(1),(2),(3),(4),(5);

 

驗證:可行

[root@CentOS6 ~]# date

Tue Dec 31 09:40:09 CST 2013

[root@CentOS6 ~]# mysql -u root -p

Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.1.71 Source distribution

 

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

mysql> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| Test               |

| mysql              |

+--------------------+

3 rows in set (0.06 sec)

 

mysql> use Test;

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A

 

Database changed

mysql> show tables;

+----------------+

| Tables_in_Test |

+----------------+

| DB             |

+----------------+

1 row in set (0.00 sec)

 

mysql> select * from DB;

+----+--------+-----+--------+

| id | name   | age | gender |

+----+--------+-----+--------+

|  1 | 張三   | 23  | 男     |

|  2 | 李四   | 25  | 男     |

|  3 | 王五   | 24  | 男     |

|  4 | 鄭七   | 22  | 女     |

|  5 | 梁九   | 21  | 女     |

|  6 | Tom    | 24  | 男     |

|  7 | Jim    | 24  | 男     |

|  8 | Lucy   | 22  | 女     |

|  9 | John   | 23  | 男     |

+----+--------+-----+--------+

9 rows in set (0.00 sec)

 

mysql> insert into DB(id,name,age,gender) values('10','劉三','24','男'),('11','李六','25','男'),('12','Eric','27','男');

Query OK, 3 rows affected (0.00 sec)

Records: 3  Duplicates: 0  Warnings: 0

 

mysql> select * from DB;

+----+--------+-----+--------+

| id | name   | age | gender |

+----+--------+-----+--------+

|  1 | 張三   | 23  | 男     |

|  2 | 李四   | 25  | 男     |

|  3 | 王五   | 24  | 男     |

|  4 | 鄭七   | 22  | 女     |

|  5 | 梁九   | 21  | 女     |

|  6 | Tom    | 24  | 男     |

|  7 | Jim    | 24  | 男     |

|  8 | Lucy   | 22  | 女     |

|  9 | John   | 23  | 男     |

| 10 | 劉三   | 24  | 男     |

| 11 | 李六   | 25  | 男     |

| 12 | Eric   | 27  | 男     |

+----+--------+-----+--------+

12 rows in set (0.00 sec)

 

mysql> insert into DB values('13','劉七','24','女'),('14','魯六','21','男'),('15','Lily','22','女');

Query OK, 3 rows affected (0.00 sec)

Records: 3  Duplicates: 0  Warnings: 0

 

mysql> select * from DB;

+----+--------+-----+--------+

| id | name   | age | gender |

+----+--------+-----+--------+

|  1 | 張三   | 23  | 男     |

|  2 | 李四   | 25  | 男     |

|  3 | 王五   | 24  | 男     |

|  4 | 鄭七   | 22  | 女     |

|  5 | 梁九   | 21  | 女     |

|  6 | Tom    | 24  | 男     |

|  7 | Jim    | 24  | 男     |

|  8 | Lucy   | 22  | 女     |

|  9 | John   | 23  | 男     |

| 10 | 劉三   | 24  | 男     |

| 11 | 李六   | 25  | 男     |

| 12 | Eric   | 27  | 男     |

| 13 | 劉七   | 24  | 女     |

| 14 | 魯六   | 21  | 男     |

| 15 | Lily   | 22  | 女     |

+----+--------+-----+--------+

15 rows in set (0.00 sec)

 

mysql>

原釋出於

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29734436/viewspace-1247999/,如需轉載,請註明出處,否則將追究法律責任。

相關文章