Helpful ways to nuance an INSERT statement

Like SELECT has other helpful quantifiers to weed through the data being returned, INSERT has ways of nuancing the origin of the data to be inserted as well as the timing and conditions of the insertion. The three most common ways of altering the way MySQL processes an INSERT statement are:

  • INSERT...SELECT...
  • INSERT DELAYED...
  • INSERT...ON DUPLICATE KEY UPDATE...

In the following section, we take each one in turn.

INSERT...SELECT...

Using INSERT...SELECT... we can tell MySQL to draw from different tables without having to draw them into Python or to set a variable in MySQL. It functions on the following syntactic template:

INSERT INTO <target table>(target column name) SELECT <source column name> FROM <source ...

Get MySQL for Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.