THE STRUCTURED

QUERY LANGUAGE
(SQL)
Dr Damitha D Karunaratna

.Classification of SQL • Two sublanguages: • DDL – Data Definition Language • Define and modify schema • DML – Data Manipulation Language • Queries can be written intuitively.

Example Database Enrollment Student s sid subid sid sname sex age 1 1 1 Nimal m 14 1 2 2 Saman m 16 2 1 3 Nirmala f 15 3 1 4 Kusum f 12 3 2 Subject 3 4 ssid sname 4 3 1 Maths 2 Science 3 English 4 Sinhala .

3 Nirmala f 15 PRIMARY KEY (sid)) 4 Kusum f 12 . 2 Saman m 16 sex CHAR(1). 1 Nimal m 14 sname CHAR(20). age INTEGER. Creating Tables Student s sid sname sex age CREATE TABLE Students ( sid INTEGER.

Creating Tables Subject ssid CREATE TABLE Subjects ( sname sid INTEGER. PRIMARY KEY (sid)) 2 Science 3 English 4 Sinhala . 1 Maths sname CHAR(20).

Creating Tables Enrollment CREATE TABLE Enrollment ( sid subid sid INTEGER. subid). foreign KEY references Subjects (sid)) 2 1 3 1 3 2 3 4 4 3 . 1 1 subid INTEGER. 1 2 foreign KEY references Students (sid). PRIMARY KEY (sid.

’Saman’.age) Values(2.14).’m’.sname.’f’. 1 Nimal m 14 2 Saman f 16 Insert into Students(sid.age) Values(1.sname.Inserting rows to a table Student s sid sname sex age Insert into Students(sid.’Nimal’. .16).sex.sex.

Changing(Modifying) values Student s sid sname sex age Update Students Set sex=‘m’ 1 Nimal m 14 Where sid = 2 2 Saman f 16 .

Deleting Rows Student s sid sname sex age delete from Students Where sid = 2 1 Nimal m 14 2 Saman f 16 .

Querying the Database (Example 1) Subject Enrollment Student ssid sname sid subid s sid sname sex age 1 Maths 1 1 1 Nimal m 14 2 Science 1 2 2 Saman m 16 3 English 2 1 3 Nirmala f 15 4 Sinhala 3 1 4 Kusum f 12 3 2 3 4 Select * from Students 4 3 .

Querying the Database (Example 2) Subject Enrollment Student ssid sname sid subid s sid sname sex age 1 Maths 1 1 1 Nimal m 14 2 Science 1 2 2 Saman m 16 3 English 2 1 3 Nirmala f 15 4 Sinhala 3 1 4 Kusum f 12 3 2 3 4 Select sid.sname from Students 4 3 .

Querying the Database (Example 3) Subject Enrollment Student ssid sname sid subid s sid sname sex age 1 Maths 1 1 1 Nimal m 14 2 Science 1 2 2 Saman m 16 3 English 2 1 3 Nirmala f 15 4 Sinhala 3 1 4 Kusum f 12 3 2 3 4 Select sid.sname from Students where sex = ‘f’ 4 3 .

Querying the Database (Example 4) Subject Enrollment Student ssid sname sid subid s sid sname sex age 1 Maths 1 1 1 Nimal m 14 2 Science 1 2 2 Saman m 16 3 English 2 1 3 Nirmala f 15 4 Sinhala 3 1 4 Kusum f 12 3 2 3 4 Select sid.sname from Students where sex = ‘f’ 4 3 order by age .

Querying the Database (Example 5) Subject Enrollment Student ssid sname sid subid s sid sname sex age 1 Maths 1 1 1 Nimal m 14 2 Science 1 2 2 Saman m 16 3 English 2 1 3 Nirmala f 15 4 Sinhala 3 1 4 Kusum f 12 3 2 3 4 Select sid.sname from Students where sex = ‘f’ order by age asc .sname from Students where sex = ‘f’ 4 3 order by age desc Select sid.

sname 4 3 from Students std.sid . sub.sid = std.sid and en. Enrollment en where en. Querying the Database (Example 6) Subject Enrollment Student ssid sname sid subid s sid sname sex age 1 Maths 1 1 1 Nimal m 14 2 Science 1 2 2 Saman m 16 3 English 2 1 3 Nirmala f 15 4 Sinhala 3 1 4 Kusum f 12 3 2 3 4 Select std. Subjects sub.subid = sub.sname.

sname 4 3 from Students std.subid = sub.sid = std. Querying the Database (Example 7) Subject Enrollment Student ssid sname sid subid s sid sname sex age 1 Maths 1 1 1 Nimal m 14 2 Science 1 2 2 Saman m 16 3 English 2 1 3 Nirmala f 15 4 Sinhala 3 1 4 Kusum f 12 3 2 3 4 Select std.sid and std.sid = 1 .sname.sid and en. Enrollment en where en. sub. Subjects sub.

Views (Virtual Tables) CREATE VIEW male_students AS SELECT * FROM students WHERE sex = ‘m’ ORDER BY sid .