CREATE TABLE Statement
Table is a basic element of relational database and creating table is very important before you are working with data which you want to store. In this tutorial, you will learn how to create table by using CREATE TABLE statement.
Basically, the normal form of CREATE TABLE statement is as follows:
CREATE TABLE table_name ( column_name1 data_type(size) [NULL|NOTNULL], column_name2 data_type(size) [NULL|NOTNULL], ....... )
Let’s explain the syntax:
- After the CREATE TABLE, you have to declare name of table you want to create. The name of table has to follow the rules of database vendor specifications.
- For each column in the table you have to name the column name, its data type and size. SQL data types define the type of data of each column that can be stored in data tables. SQL data type enforces basic integrity by limiting the type and size of the data that can be stored. Choosing the correct data type for each column is very important to increase efficient data storage and performance. SQL data types is SQL data can be vary in different relational database management system (RDBMS).
Here is an example of creating employee table in MySQL database:
CREATE TABLE employee( id INT NOT NULL AUTO_INCREMENT , name VARCHAR(255) NOT NULL , salary DECIMAL NOT NULL, PRIMARY KEY (id) )
employee is the table name and it is defined after CREATE TABLE. There are three columns in the table id, name and salary. Beside defining column characteristics such as data types (INT, VARCHAR and DECIMAL), NULL or NOT NULL, you also see that CREATE TABLE statement also enables to create constraint to define id is primary key of the table.