In the previous chapter we have learned how to create a database on the database server. Now it's time to create some tables inside our database that will actually hold the data. A database table simply organizes the information into rows and columns.
The SQL CREATE TABLE
statement is used to create a table.
Syntax
The basic syntax for creating a table can be given with:
CREATE TABLE table_name [ column1_name data_type constraints, column2_name data_type constraints, .... ];
To understand this syntax easily, let's create a table in our demo database. Type the following statement on MySQL command-line tool and press enter:
-- Syntax for MySQL Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
-- Syntax for SQL Server Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY IDENTITY[1,1],
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
The above statement creates a table named persons with four columns id, name, birth_date and phone. Notice that each column name is followed by a data type declaration; this declaration specifies that what type of data the column will store, whether integer, string, date, etc.
Some data types can be declared with a length parameter that indicates how many characters can be stored in the column. For example, VARCHAR[50]
can hold up to 50 characters.
Note: The data type of the columns may vary depending on the database system. For example, MySQL and SQL Server supports INT
data type for integer values, whereas the Oracle database supports NUMBER
data type.
The following table summarizes the most commonly used data types supported by MySQL.
Data TypeDescription
INT
Stores numeric values in the range of -2147483648 to 2147483647DECIMAL
Stores decimal values with exact precision.-- Syntax for MySQL Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
-- Syntax for SQL Server Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY IDENTITY[1,1],
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
0Stores fixed-length strings with a maximum size of 255 characters.-- Syntax for MySQL Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
-- Syntax for SQL Server Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY IDENTITY[1,1],
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
1Stores variable-length strings with a maximum size of 65,535 characters.-- Syntax for MySQL Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
-- Syntax for SQL Server Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY IDENTITY[1,1],
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
2Stores strings with a maximum size of 65,535 characters.-- Syntax for MySQL Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
-- Syntax for SQL Server Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY IDENTITY[1,1],
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
3Stores date values in the YYYY-MM-DD format.-- Syntax for MySQL Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
-- Syntax for SQL Server Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY IDENTITY[1,1],
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
4Stores combined date/time values in the YYYY-MM-DD HH:MM:SS format.-- Syntax for MySQL Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
-- Syntax for SQL Server Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY IDENTITY[1,1],
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
5Stores timestamp values. -- Syntax for MySQL Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
-- Syntax for SQL Server Database
CREATE TABLE persons [
id INT NOT NULL PRIMARY KEY IDENTITY[1,1],
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
5 values are stored as the number of seconds since the Unix epoch ['1970-01-01 00:00:01' UTC].Please check out the reference section SQL DB data types for the detailed information on all the data types available in popular RDBMS like MySQL, SQL Server, etc.
There are a few additional constraints [also called modifiers] that are set for the table columns in the preceding statement. Constraints define rules regarding the values allowed in columns.
- The
7 constraint ensures that the field cannot accept a-- Syntax for MySQL Database CREATE TABLE persons [ id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR[50] NOT NULL, birth_date DATE, phone VARCHAR[15] NOT NULL UNIQUE ]; -- Syntax for SQL Server Database CREATE TABLE persons [ id INT NOT NULL PRIMARY KEY IDENTITY[1,1], name VARCHAR[50] NOT NULL, birth_date DATE, phone VARCHAR[15] NOT NULL UNIQUE ];
8 value.-- Syntax for MySQL Database CREATE TABLE persons [ id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR[50] NOT NULL, birth_date DATE, phone VARCHAR[15] NOT NULL UNIQUE ]; -- Syntax for SQL Server Database CREATE TABLE persons [ id INT NOT NULL PRIMARY KEY IDENTITY[1,1], name VARCHAR[50] NOT NULL, birth_date DATE, phone VARCHAR[15] NOT NULL UNIQUE ];
- The
9 constraint marks the corresponding field as the table's primary key.-- Syntax for MySQL Database CREATE TABLE persons [ id INT NOT NULL PRIMARY KEY AUTO_INCREMENT, name VARCHAR[50] NOT NULL, birth_date DATE, phone VARCHAR[15] NOT NULL UNIQUE ]; -- Syntax for SQL Server Database CREATE TABLE persons [ id INT NOT NULL PRIMARY KEY IDENTITY[1,1], name VARCHAR[50] NOT NULL, birth_date DATE, phone VARCHAR[15] NOT NULL UNIQUE ];
- The
Data Type
0 attribute is a MySQL extension to standard SQL, which tells MySQL to automatically assign a value to this field if it is left unspecified, by incrementing the previous value by 1. Only available for numeric fields. - The
Data Type
1 constraint ensures that each row for a column must have a unique value.
We will learn more about the SQL constraints in next chapter.
Note: The Microsoft SQL Server uses the
Data Type2 property to perform an auto-increment feature. The default value is
Data Type3 which means the seed or starting value is 1, and the incremental value is also 1.
Tip: You can execute the command
Data Type4 to see the column information or structure of any table in MySQL and Oracle database, whereas
Data Type5 in SQL Server [replace the table_name with actual table name].
Create Table If Not Exists
If you try to create a table that is already exists inside the database you'll get an error message. To avoid this in MySQL you can use an optional clause
Data Type6 as follow:
CREATE TABLE IF NOT EXISTS persons [
id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR[50] NOT NULL,
birth_date DATE,
phone VARCHAR[15] NOT NULL UNIQUE
];
Tip: If you want to see the list of tables inside the currently selected database, you can execute
Data Type7 statement on the MySQL command line.