Create a basic MySQL table

Home -> Database -> MySQL

27388 views

From the computer of: qmchenry (339 recipes)
Created: Feb 08, 2004


0 comments:
View all comments

Add a comment

Add to:
Add to stumbleuponAdd to del.icio.usDigg itAdd to FURL

Creating tables in databases is an important first step to storing data. The CREATE TABLE statement is rich and sometimes confusing. This recipe describes the basics of creating a table in MySQL.

Creating a table involves describing the columns and their attributes, whether they contain text, numbers, dates, and so on. In this recipe, we will create a table to hold contact information with four columns: contact_id, name, email, and birthdate.

The contact_id column is an integer number that is 10 decimal places long (therefore, it is created with an INT(10) datatype). This column will act as the primary key for this table, although that is another recipe.

The name column holds the full name of a contact, which we guess will be no longer than 40 characters long, so the datatype is VARCHAR(40).

The contact's birthdate will be stored as a DATE datatype.

The following SQL command will create a table called contacts as described above:

CREATE TABLE contacts (
   contact_id INT(10),
   name VARCHAR(40),
   birthdate DATE,
);


MySQL doesn't care if the command includes carriage returns (thoughtfully placed, of course, like not in the middle of a keyword) If you are entering this command from the mysql command-line interface, it will need to be terminated with a semicolon. If it is being submitted through a programming interface, as from a PHP script, the semicolon is optional.

Subscribe to the Tech-Recipes Newsletter

You can get tips like this delivered in your email every week!

Enter your Email

We will never, ever sell your email address or spam you.





Related recipes:

  Add a column to an existing MySQL table
  Create a MySQL table with a primary key
  Modify an existing MySQL column
  Describe the column structure of a MySQL table
  Drop or delete a table in MySQL
  Rename or change name of MySQL table
  Copy an existing MySQL table to a new table
  Use regular expressions in MySQL SELECT statements
  Import CSV file directly into MySQL
  Delete a column from an existing MySQL table

 

Sponsored links

 

Login

Nickname

Password

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.