Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
TOPIC 4
Structured Query Language
Introduction to SQL
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
SQL Syntax
SQL can be divided into two parts:
1.
2.
SQL Syntax
The query and update commands form the DML part of
SQL:
SELECT - extracts data from a database
UPDATE - updates data in a database
DELETE - deletes data from a database
INSERT INTO - inserts new data into a database
SQL Syntax
The DDL part of SQL permits database tables to be created or
deleted.
CREATE DATABASE - creates a new database
CREATE TABLE - creates a new table
ALTER TABLE - modifies a table
DROP TABLE - deletes a table
DROP DATABASE - deletes a database
PersonID
LastName
FirstName
Address
City
LastName FirstName
Address
City
NAME
AGE
STATUS
(4 marks)
SALARY
LastName FirstName
Address
City
Hasan
Othman
Suraya
Tamin
Tmn
Cempaka
Ipoh
Puteri
Karim
Tmn Jati
Kota Bharu
DateOfBirt
h
SELECT Statement
The SQL SELECT Statement
The SELECT statement is used to select data from a database.
The result is stored in a result table, called the result-set.
SELECT Statement
An SQL SELECT Example
Persons
P_Id
1
2
3
City
Ipoh
Ipoh
Kota Bharu
The table above contains three records (one for each person)
and five columns (P_Id, LastName, FirstName, Address, and
City).
SELECT Statement
SELECT LastName,FirstName
FROM Persons;
P_Id
1
2
3
FirstName
Othman
Tamin
Karim
City
Ipoh
Ipoh
Kota Bharu
SELECT Statement
SELECT * FROM Persons
P_Id
1
2
3
City
Ipoh
Ipoh
Kota Bharu
City
Ipoh
Ipoh
Kota Bharu
City
Ipoh
Ipoh
Kota Bharu
City
Ipoh
Kota Bharu
Jusoh
City
Ipoh
Ipoh
Kota Bharu
Kota Bharu
Ipoh
UPDATE Persons
SET Address=' Tmn Timah', City=' Ipoh '
P_Id
LastName FirstName Address
1
Hasan
Othman
Tmn Timah
2
Suraya
Tamin
Tmn Timah
3
Puteri
Karim
Tmn Timah
The result-set
will look
like this: Tmn Timah
4
Naemah
Talib
5
Sani
Jusoh
Tmn Timah
City
Ipoh
Ipoh
Ipoh
Ipoh
Ipoh
City
Ipoh
City
Ipoh
Ipoh
Kota Bharu
Kota Bharu
Ipoh
Ipoh
Kota Bharu
Kota Bharu
City
Ipoh
Ipoh
Kota Bharu
City
Ipoh
Ipoh
Kota Bharu
Kota Bharu
City
Ipoh
Ipoh
Kota Bharu
Kota Bharu
3
4
Sani
Jusoh
City
Ipoh
Ipoh
Kota Bharu
Kota Bharu
INSERT
b)
DELETE
(4 marks)
Jun 2013
Section A : Question 10
Build the SQL command to insert a new record containing information
which is EC601,Database System,2 to COURSE table as in table
below.:
COURSE
CourseCode
CourseName
Credit
WHERE Clause
The WHERE clause is used to filter records. The WHERE
clause is used to extract only those records that fulfill a
specified criterion.
SQL WHERE Syntax
SELECT column_name(s)
FROM table_name
WHERE column_name operator value
WHERE Clause
SELECT * FROM Persons
WHERE City=Ipoh'
P_Id
1
2
3
City
Ipoh
Ipoh
Kota Bharu
City
Ipoh
Ipoh
=
<>
Equal
Not equal
>
Greater than
Less than
<
>=
<=
BETWEEN
LIKE
IN
City
Ipoh
Ipoh
Kota Bharu
City
Ipoh
City
Ipoh
Ipoh
Kota Bharu
City
Ipoh
Ipoh
City
Ipoh
Ipoh
Kota Bharu
City
Ipoh
LastName FirstName
Suraya
Tamin
3
4
1
Puteri
Naemah
Hasan
Karim
Talib
Othman
Address
Tmn
Cempaka
Tmn Jati
Tmn Ria
Tmn Timah
City
Ipoh
Ipoh
Kota Bharu
Kota Bharu
City
Ipoh
Kota Bharu
Kota Bharu
Ipoh
City
Ipoh
LastName
Hasan
Naemah
Puteri
City
Ipoh
Kota Bharu
Kota Bharu
Suraya
FirstName Address
Othman
Tmn Timah
Talib
Tmn Ria
Karim
Tmn Jati
Tmn
Tamin
Cempaka
Ipoh
Kota Bharu
Kota Bharu
Ipoh
Hasan
Othman
Suraya
Tamin
Puteri
Karim
Address
Tmn Timah
Tmn
Cempaka
Tmn Jati
LastName FirstName
Hasan
Othman
City
Ipoh
Ipoh
Kota Bharu
City
Ipoh
Ipoh
Kota Bharu
Hasan
Othman
Suraya
Tamin
Puteri
Kamal
Tmn Timah
Tmn
Cempaka
Tmn Jati
LastName FirstName
Suraya
Tamin
Puteri
Kamal
City
Ipoh
Ipoh
Kota Bharu
City
Ipoh
Ipoh
Kota Bharu
Cust
omer SELECT
CustomerName
* FROM ContactName
Customers
ID
1
Address
City
PostalCo
de
Country
Obere Str.
57
Berlin
12209
Germany
Ana Trujillo
Emparedados y
helados
Ana Trujillo
Avda. de la
Constitucin
2222
Mxico
D.F.
05021
Mexico
Antonio Moreno
Taquera
Antonio Moreno
Mataderos
2312
Mxico
D.F.
05023
Mexico
Thomas Hardy
120 Hanover
Sq.
London
WA1
1DP
UK
Berglunds
snabbkp
Christina
Berglund
Berguvsvge
n8
Lule
S-958
22
Sweden
Alfreds Futterkiste
ProductName
Suppl
ierID
Categ
Unit
oryID
Pric
e
Chais
10 boxes x 20
bags
18
Chang
24 - 12 oz bottles
19
Aniseed Syrup
12 - 550 ml
bottles
10
Chef Anton's
Cajun
Seasoning
48 - 6 oz jars
22
Chef Anton's
Gumbo Mix
36 boxes
21.3
5
FirstName
Address
City
Ola
Timoteivn
10
Sandnes
Tove
Borgvn 23
Sandnes
Hansen
2
Svendson
The "Orders"
table:P_Id
O_Id
OrderNo
1
77895
44678