Creating Databases and Tables

The first logical use of SQL and MySQL will be to create a database. The syntax for creating a new database is

CREATE DATABASE databasename;

The CREATE term is also used for making tables.

CREATE TABLE tablename (column1name description, column2name description ...);

As you can see from the syntax above, after naming the table, you define each column—in order—within parentheses. Each column and description should be separated by a comma. Should you choose to create indexes at this time (see the sidebar, Indexes and Keys), you can add those at the end of the creation statement, but you can add indexes at a later time as well.

To create databases and tables:

1.
Access the mysql monitor.
 mysql -u username -p ...

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