SQL Create Table sql-create-table.pdf


Table is where database store structured data. For creating a simple or you can say basic database table you need a unique table name in that database and need to define columns and their data types.

CREATE TABLE command is used for this purpose.


Syntax for creating a simple database table is

CREATE TABLE table_name 
(column_one data_type, column_two data_type, 
column_three data_type, primary key(column_name));


Now let us create a hostel_list table which will contain the list of hostels.

CREATE TABLE hostel_list 
(id int(2) auto_increment, name varchar(100) not null, 
occupancy_type tinyint(1) not null, 
max_capacity int(4) not null, primary key (id));
| Field | Type | Null | Key | Default | Extra |
| id | int(2) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| occupancy_type | tinyint(1) | NO | | NULL | |
| max_capacity | int(4) | NO | | NULL | |
4 rows in set (0.01 sec)

CREATE TABLE command is used to create table in our case table name is hostel_list and we have four columns with data types. So lets have a look at each of the columns.

id int(2) auto_increment ==> column_name is "id" and the data type int(2) shows that it can hold integer value. auto_increment denotes that it will be auto incremented each time a new row is inserted in the table.

name varchar(100) not null ==> column_name is "name" and the data type varchar(100) shows that it will contain character value up to the limit of 100 characters.

primary key (id) ==> we have denoted that id is a primary key.

Primary Key

Primary key is a key in relational database which uniquely defines each record in a table. Database use to create index for the primary key.

This article is contributed by Ankur Rastogi on Tue Mar 15 2016 23:57:01 GMT+0530 (IST)

If you want to add more comments to the article or you see any thing incorrect please write a comment below and we will surely get back to you.