MySQL Logo</img>

Our Tuesday MySQL tip, how to create Tables in a MySQL Database.

Tables are somehow like spreadsheets, and one database may have lots of tables into it.

So, once you have created your Database you may create the tables inside the Database.

We’ll do it, working on the command line:

First connect to the MySQL

mysql -u [user] -p

This will ask the password for the user [user], it should be the user you have given (grant) permissions to work on that database, when you created the MySQL database.

The syntax is:

CREATE TABLE [table_name] ([field_name1] [field_type1], [field_name2] [field_name2]);

You can add as many fields as you need.

So let’s imagine we’re creating a TABLE with this fields on it.

  • id, Type int
  • name, Type varchar, with 15 chars
  • country, Type varchar, with 15 chars

The command should be something like this:

CREATE TABLE personal_data (id int, name varchar(15), country varchar(15));

If the line is too long, you may enter it at the interactive prompt in sections, like this:

CREATE TABLE personal_data (
id int,
name varchar(15),
country varchar(15)
);

You have finished.