Before you can put any data into or retrieve any data from a database table, you must create the table. This is usually a one-time operation. You tell the database program to create a new table once. Your PHP program that uses the table may read from or write to that table every time it runs. But it doesn't have to re-create the table each time. If a database table is like a spreadsheet, then creating a table is like making a new spreadsheet file. After you create the file, you open it many times to read or change it.
The SQL command to create a table is
TABLE. You provide the name of the table and the
names and types of all the columns in the table. Example 7-5 shows the SQL command to create the
dishes table pictured in Figure 7-1.
Example 7-5. Creating the dishes table
CREATE TABLE dishes ( dish_id INT, dish_name VARCHAR(255), price DECIMAL(4,2), is_spicy INT )
Example 7-5 creates a table called
dishes with four columns. The
dishes table looks like the one pictured in Figure 7-1. The columns in the table are
is_spicy columns are integers. The
price column is a decimal number. The
dish_name column is a string.
After the literal
TABLE comes the name of the table. Then, between the
parentheses, is a comma-separated list of the columns in the table. The phrase that
defines each column has two parts: the column name and the column type. In Example 7-5, the column names are