MySQL CREATE TABLE

The MySQL CREATE TABLE command is used to create a new table into the database. A table creation command requires three things:

  • Name of the table
  • Names of fields
  • Definitions for each field

Syntax:

Following is a generic syntax for creating a MySQL table in the database.

snippet
CREATE TABLE table_name (column_name column_type...);

Example:

Here, we will create a table named "cus_tbl" in the database "customers".

snippet
CREATE TABLE cus_tbl(
   cus_id INT NOT NULL AUTO_INCREMENT,
   cus_firstname VARCHAR(100) NOT NULL,
   cus_surname VARCHAR(100) NOT NULL,
   PRIMARY KEY ( cus_id )
);

Note:

  1. Here, NOT NULL is a field attribute and it is used because we don't want this field to be NULL. If you will try to create a record with NULL value, then MySQL will raise an error.
  2. The field attribute AUTO_INCREMENT specifies MySQL to go ahead and add the next available number to the id field.PRIMARY KEY is used to define a column as primary key. You can use multiple columns separated by comma to define a primary key.

Visual representation of creating a MySQL table:

mysql create table

See the created table:

Use the following command to see the table already created:

snippet
SHOW tables;
mysql show table

See the table structure:

Use the following command to see the table already created:

snippet
DESCRIBE cus_tbl;
mysql describe table
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +