Mysql can insert multiple records at one time through the insert statement, but this statement is a transaction, so it will succeed if it succeeds, and it will fail if it fails. Each record in the statement is enclosed by (). And this syntax is mysql's own, not standard syntax, and cannot be used universally.
mysql inserts multiple pieces of data at one time:
INSERT INTO hk_test(username, passwd) VALUES ('qmf2', 'qmf2'),('qmf3', 'qmf3'),('qmf4', 'qmf4'),('qmf5', 'qmf5') GO
Let’s first create a table Authors:
CREATE TABLE Authors( AuthID SMALLINT NOT NULL PRIMARY KEY, AuthFN VARCHAR(20), AuthMN VARCHAR(20), AuthLN VARCHAR(20) ) ENGINE=INNODB;
Then insert multiple pieces of data into the table at one time, sql insert code As follows:
INSERT INTO Authors VALUES (1006, 'H', 'S.', 'T'), (1007, 'J', 'C', 'O'), (1008, 'B', NULL, 'E'), (1009, 'R', 'M', 'R'), (1010, 'J', 'K', 'T'), (1011, 'J', 'G.', 'N'), (1012, 'A', NULL, 'P'), (1013, 'A', NULL, 'W'), (1014, 'N', NULL, 'A');
In fact, it is very similar to the SQL statements inserted one by one, except that multiple insert statements use commas to separate each piece of data.
The results are as follows:
The above is the detailed content of How to add multiple rows of data in mysql?. For more information, please follow other related articles on the PHP Chinese website!