How to Create a Table in SQL
The create table command is used to create a table in SQL language.
CREATE TABLE name ( field1, field2, ... );
This command creates a table on the database.
The fields in the table are indicated in parentheses and separated by a comma.
Note. The type of data ( integer, text, char .... ), the length of the field in parentheses and the indication if the field is a primary key ( primary text ) are indicated to the right of the field name.
Practical example
Example 1
This query creates a table "students".
CREATE TABLE students ( name char(20), surname char(20), vote integer(5) );
The table is made up of three fields:
- The name field is an alphanumeric (char) field 20 characters long.
- The surname field is an alphanumeric (char) field 20 characters long.
- The vote field is a 5-digit long integer field.
Example 2
This query creates a table with two fields.
The table is called registry.
CREATE TABLE registry ( id integer(5) PRIMARY KEY , name char(20) );
The first field (id) is a primary key in the table because it contains the "primary key" option.
The second field (name) is alphanumeric and it's 20 characters long.