O'Reilly logo

MySQL in a Nutshell by Russell J.T. Dyer

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

INSERT

Synopsis

INSERT [LOW_PRIORITY|DELAYED|HIGH_PRIORITY] [IGNORE]
    [INTO] table
    SET column={expression|DEFAULT}, ...
    [ON DUPLICATE KEY UPDATE column=expression, ... ]
INSERT [LOW_PRIORITY|DELAYED|HIGH_PRIORITY] [IGNORE]
    [INTO] table [(column, ...)]
    VALUES ({expression|DEFAULT},...),(...),...
    [ON DUPLICATE KEY UPDATE column=expression, ... ]
INSERT [LOW_PRIORITY|DELAYED|HIGH_PRIORITY] [IGNORE]
    [INTO] table [(column, ...)]
    SELECT...

Use this statement to add rows of data to a table. Three statement structures are available. The first syntax format, the INSERT...tableSET... method, can insert only one row of data per statement. The second syntax structure shown, the INSERT...table(columns) VALUES(values) method, can handle one or more rows in one statement. The columns and their order are specified once, but values for multiple rows may be given. Each row of values is to be contained in its own set of parentheses, separated by commas. The third syntax structure for this statement, the INSERT...table...SELECT... method, allows columns from rows in other tables to be inserted. Explanations of each type of statement, their various clauses and flags, and examples of their use follow.

Single-row insertion with SET clause

INSERT [LOW_PRIORITY|DELAYED|HIGH_PRIORITY] [IGNORE]
    [INTO] table
    SET column={expression|DEFAULT}, ...
    [ON DUPLICATE KEY UPDATE column=expression, ... ]

This method of the INSERT statement allows only one row of data to be inserted into a table at a time. Each column name ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required