(Structured Query
Language)
FirstName
Address
City
Hansen
Ola
Timoteivn 10
Sandnes
Svendson
Tove
Borgvn 23
Sandnes
Pettersen
Kari
Storgt 20
Stavanger
Data Types
SQL data type is an attribute that specifies type of
data of any object. Each column, variable and
expression has related data type in SQL.
You would use these data types while creating
your tables. You would choose a particular data
type for a table column based on your requirement
From
To
-9,223,372,036,854,775,808
9,223,372,036,854,775,808
-2,147,483,648
2,147,483,648
-32,768
32,768
tinyint
255
bit
decimal
-10^38 +1
10^38 -1
numeric
-10^38 +1
10^38 -1
big int
int
smallint
From
To
-1.79E + 308
1.79E + 308
-3.40E + 38
3.40E + 38
From
To
datetime
Jan 1, 1753
smalldateti
me
Jan 1, 1900
Jun 6, 2079
date
time
From
char
varchar
text
Create a Database
To create a table in a database:
Syntax
CREATE DATABASE database_name
Example
CREATE DATABASE School
Create a Table
To create a table in a database:
Syntax
DROP Table
To drop a table in a database:
Syntax
DROP TBALE table_name
Example
DROP TBALE Student
ALTER TABLE
To make changes in table definition:
Syntax
Student:
LastName
Pettersen
FirstName
Kari
Address
Storgt 20
Example:
To add a column named "City" in the "Person" table:
ALTER TABLE Person ADD City VARCHAR(30)
Result:
LastName
Pettersen
FirstName
Kari
Address
Storgt 20
City
Example:
To drop the "Address" column in the "Person" table:
ALTER TABLE Person DROP COLUMN Address
LastName
Pettersen
FirstName
Kari
City
CONSTRAINTS
UNIQUE CONSTRAINT
Note that you can have many UNIQUE constraints per table,
but only one PRIMARY KEY constraint per table.
Syntax:
CHECK CONSTRAINT
The following SQL creates a CHECK constraint on
the "P_Id" column when the "Persons" table is
created. The below CHECK constraint specifies that
the column "P_Id" must only include integers
greater
than 0.
Syntax:
CREATE TABLE Persons
(
P_Id INT NOT NULL,
LastName VARCHAR(255) NOT NULL,
FirstName VARCHAR(255),
Address VARCHAR(255),
City VARCHAR(255),
CONSTRAINT chk_Person CHECK (P_Id>0)
)
Thanks