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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.