Creating Tables in SQL: Using Create Table Command

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 9

CREATING TABLES IN SQL

USING CREATE TABLE COMMAND

BY – CHANDRANSHU JAIN
Introduction to CREATE TABLE
As learnt previously that we can create databases in sql . Now we know
that we need to store structured data(tables) in the created databases.
Using the CREATE TABLE command in sql we can easily create tables in
sql. A table comprises of rows and columns. Using the create table
command we inform sql the names of columns (features ) , their
datatype , etc. We can also enter some constraints on the columns to
restrict the type of data to be stored in the table and make data more
precise , accurate and reliable . Create table command is very helpful
especially when we have to test/try some query(s) .
Syntax of CREATE TABLE
create table table_name (
column 1_name type1(size) [constraint 1],
column 2_name type2(size) [constraint 2],
.
.
column n_name typen(size) [constraint n]);
Name Definition
table_name name of table
column i_name name of the ith column
typei type of data that is stored in the columns
size Size of the data we can store in column.
Example: if we enter int(10) , it means we want to
enter integer data type which can store maximum of
10 digits.
Constraint i Type of constraint on the ith column
Example :
create table customer (
cust_id int ,
cust_name varchar ,
phone_no int not null unique,
primary key(cust_id));

create table orders (


order_id int,
cust_id int,
order_month int,
order_value int not null,
primary key(order_id),
foreign key(cust_id) references customer(cust_id));
Constraints
The constraints in sql create table are:
• NOT NULL: This constraint tells that we cannot store a null value in a column. That is, if a
column is specified as NOT NULL then we will not be able to store null in this particular
column any more.
• UNIQUE: This constraint when specified with a column, tells that all the values in the column
must be unique. That is, the values in any row of a column must not be repeated.
• PRIMARY KEY: A primary key is a field which can uniquely identify each row in a table. And
this constraint is used to specify a field in a table as primary key.
• FOREIGN KEY: A Foreign key is a field which can uniquely identify each row in a another table.
And this constraint is used to specify a field as Foreign key.
• CHECK: This constraint helps to validate the values of a column to meet a particular condition.
That is, it helps to ensure that the value stored in a column meets a specific condition.
• DEFAULT: This constraint specifies a default value for the column when no value is specified
by the user.
KEYS IN A DATA TABLE IN SQL
There are many types of keys in sql , but we will focus mainly on primary key and
foreign key
1)Primary Key : A primary key is a field in a table which uniquely identifies each
row/record in a database table. Primary keys must contain unique values. A primary
key column cannot have NULL values. A table can have only one primary key, which
may consist of single or multiple fields. When multiple fields are used as a primary
key, they are called a composite key.
2)Foreign Key : Foreign keys links data in one table to the data in another table. A
foreign key column in a table references to a column with unique values in another
table (often the primary key column) to create a way of cross-referencing the two
tables. If a column is assigned a foreign key, each instance(row) of that
column must contain a value that exists in the ‘foreign’ column it references.(i.e. all
the values of the foreign key column must be contained in the column it references
to).
Visualising using relational diagram
Attributes of customer Attributes of orders table
table
Cust_id (foreign key)
Cust_id (primary key)
Order_id (primary key)
Cust_name
Order_month
Phone_no
Order_value

So we see here that the customer table has cust_id as primary key (non null and unique values) and the
orders table has order_id as primary key and the cust_id in orders table the foreign key (i.e. it links or
references to the customer table ) , so in this way the customer and orders table are related.
More Examples
create table employee (
id int ,
name varchar not null,
phone_number int unique,
age int check(age>=18) default 18,
primary key(id));

So here some points to note:


Id column stores the id of employee which is of integer type and every employee has it and it is
unique(so no 2 employees have same id)
Age column stores the age of employee and we check that every employee has age of 18 or above
and if no age is given we give it default value 18.

You might also like