Inserting Records

After your database and its table(s) have been created, you can start populating them using the INSERT command. Before demonstrating the syntax, I want to be explicit as to how you should treat the different data types in your SQL commands. Always abide by these rules:

  • Numeric values shouldn’t be quoted.

  • String values (for CHAR, VARCHAR, and TEXT column types) must always be quoted.

  • Date and time values must always be quoted.

  • Functions cannot be quoted.

  • The word NULL must not be quoted.

With that in mind, there are two formats for inserting data. With the first, you specify the columns to be used:

INSERT INTO tablename (column1,
→ column2 ...) VALUES ('value1',
→ 'value2' ...)

INSERT INTO tablename (column4,
→ column8) VALUES ('valueX', ...

Get PHP and MySQL for Dynamic Web Sites: Visual Quickpro Guide, Second Edition 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.