调用多次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)