0% found this document useful (0 votes)
22 views

T S Q L (SQL) : HE Tructured Uery Anguage

The document discusses the structured query language (SQL) and its use for defining and querying relational databases, including creating tables, inserting, updating, and deleting rows, and examples of different types of queries using joins across multiple tables to retrieve and filter data. SQL allows the creation of relational databases and querying of data through its DDL and DML sublanguages for schema definition and data manipulation.

Uploaded by

anmsumathipala
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

T S Q L (SQL) : HE Tructured Uery Anguage

The document discusses the structured query language (SQL) and its use for defining and querying relational databases, including creating tables, inserting, updating, and deleting rows, and examples of different types of queries using joins across multiple tables to retrieve and filter data. SQL allows the creation of relational databases and querying of data through its DDL and DML sublanguages for schema definition and data manipulation.

Uploaded by

anmsumathipala
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

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
Creating Tables
Student
s
sid sname sex age CREATE TABLE Students (
sid INTEGER,
1 Nimal m 14 sname CHAR(20),
2 Saman m 16 sex CHAR(1),
age INTEGER,
3 Nirmala f 15 PRIMARY KEY (sid))
4 Kusum f 12
Creating Tables
Subject
ssid CREATE TABLE Subjects (
sname sid INTEGER,
1 Maths sname CHAR(20),
PRIMARY KEY (sid))
2 Science
3 English
4 Sinhala
Creating Tables
Enrollment
CREATE TABLE Enrollment (
sid subid sid INTEGER,
1 1 subid INTEGER,
PRIMARY KEY (sid, subid),
1 2 foreign KEY references Students (sid),
foreign KEY references Subjects (sid))
2 1
3 1
3 2
3 4
4 3
Inserting rows to a table

Student
s
sid sname sex age Insert into Students(sid,sname,sex,age)
Values(1,Nimal,m,14);
1 Nimal m 14
2 Saman f 16 Insert into Students(sid,sname,sex,age)
Values(2,Saman,f,16);
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 4 3
order by age desc

Select sid,sname from Students where sex = f


order by age asc
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.sname, sub.sname 4 3
from Students std, Subjects sub, Enrollment en
where en.sid = std.sid and en.subid = sub.sid
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.sname, sub.sname 4 3
from Students std, Subjects sub, Enrollment en
where en.sid = std.sid and en.subid = sub.sid and std.sid = 1
Views (Virtual Tables)
CREATE VIEW male_students AS
SELECT *
FROM students
WHERE sex = m
ORDER BY sid

You might also like