You are on page 1of 24

WINTER

Template
Lab 1
Introduction
SQL
TA.Rawan Sanyour

WINTER
Template
Sanyour_r@hotmail.com


What is a database and SQL
SQL
is a standard language
for accessing and
manipulating databases.

Databases
A collection of data organized
in such a way that a computer
program can quickly select
desired pieces of data
What is SQL
SQL stands for Structured
Query Language
SQL lets you access and
manipulate databases
SQL is an ANSI (American
National Standards Institute)
standard
What can SQL do?
execute queries against a
database
retrieve data from a
database
insert ,delete and update
records in a database
What can SQL do?
create new databases
create new tables in a
database
set permissions on tables,
procedures, and views
DataBases tables
A database most often contains one or more tables.
A table is a collection of related data entries and it
consists of columns and rows
Each table is identified by a name (e.g. "Customers"
or "Orders"). Tables contain records (rows) with
data.
SQL
Keep in Mind That...

SQL is NOT case sensitive:
SELECT is the same as select
Most important SQL commands
extracts data from a database
updates data in a database
deletes data from a database
Most important SQL commands
inserts new data into a
database
creates a new database
modifies a database
Most important SQL commands
creates a new table
modifies a table
deletes a table
Creating a database
Creating a database
Creating a table
CREATE TABLE
table_name
(
column_name1
data_type(size),
column_name2
data_type(size),
column_name3
data_type(size),
....
);
Creating table example
we want to
create a table
called "Persons"
that contains
five columns:
PersonID,
LastName,
FirstName,
Address, and
City.
CREATE TABLE Persons
(
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
constraints
NOT NULL -
Indicates that a
column cannot
store NULL value
PRIMARY KEY - A combination
of a NOT NULL and UNIQUE.
Ensures that a column have an
unique identity which helps to
find a particular record in a
table
FOREIGN KEY - Ensure the
referential integrity of the data
in one table to match values in
another table
CHECK - Ensures that the value
in a column meets a specific
condition
DEFAULT -
Specifies a
default value when
specified none for
this column
UNIQUE - Ensures that
each row for a column must
have a unique value
SQL CREATE TABLE + CONSTRAINT Syntax
CREATE TABLE table_name
(
column_name1 data_type(size) constraint_name,
column_name2 data_type(size) constraint_name,
column_name3 data_type(size) constraint_name,
....
);
NOT NULL and UNIQUE constraint
CREATE TABLE PersonsUnique
(
P_Id int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
CREATE TABLE suppliers (
supplier_id varchar(10) not null,
supplier_name varchar(50) not null,
contact_name varchar(50) );
PRIMARY KEY and CHECK constraint
CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY
KEY,
LastName varchar(255) NOT
NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
CREATE TABLE Persons
(
P_Id int NOT NULL CHECK
(P_Id>0) ,
LastName varchar(255) NOT
NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
);
FOREIN KEY constraint
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY
KEY,
OrderNo int NOT NULL,
P_Id int FOREIGN KEY
REFERENCES Persons(P_Id)
);
Creating Database Example
Creating Database Example
Creating Database Example
Any question?

You might also like