0% found this document useful (0 votes)
6 views18 pages

Comp1121 Lec 05

The document discusses SQL and provides examples of SQL statements for creating, altering, and deleting tables as well as manipulating data. It covers SQL data types, the SELECT statement including filtering, sorting, aggregation, joins and more. The next lecture will cover additional SQL topics.

Uploaded by

colemanyau
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views18 pages

Comp1121 Lec 05

The document discusses SQL and provides examples of SQL statements for creating, altering, and deleting tables as well as manipulating data. It covers SQL data types, the SELECT statement including filtering, sorting, aggregation, joins and more. The next lecture will cover additional SQL topics.

Uploaded by

colemanyau
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

COMP1121

Databases

5: Structured Query Language


(SQL) - basic

1
Our journey so far ...
Database
Environment Relational Relational
Introduction & Algebra
Model
Architecture

SQL
Entity
Normalisation Relationship
Modelling

Database
Designs

Ethics of
Security & Transaction Using
Administration Management Databases

2
Relational Data Model
• Components of a data model
– A structural part – set of rules
• Relations  SQL CREATE TABLE
– A manipulative part – operations allowed on data
• Relational Algebra SQL SELECT
– A set of integrity constraints SQL CREATE TABLE
(PRIMARY KEY, FOREIGN KEY, CHECK, …)

3
Learning objectives
• To write SQL queries to create, alter, and
delete tables in database.
• To write SQL queries to manipulate data in
database.

4
Structured Query Language (SQL)
• “ess-que-ell” or “sequel”? – a matter of
preference.
• Two parts: DDL and DML
• Consists of standard English words such as
CREATE TABLE, INSERT INTO, SELECT .. FROM
..WHERE.
• Most components of an SQL statement are
case insensitive, except for literal character
data.
5
DDL
• Main SQL DDL statements are:

• Many DBMSs also provide:


CREATE INDEX DROP INDEX

6
CREATE TABLE

7
SQL Data Types

SQLite data types? Refer here

8
ALTER/DROP TABLE
• ALTER TABLE Staff ALTER position DROP DEFAULT;
• ALTER TABLE Client
ADD prefNoRooms SMALLINT;
• DROP TABLE PropertyForRent;

9
DML
• INSERT INTO TableName [ (columnList) ]
VALUES (dataValueList)

• UPDATE TableName
SET columnName1 = dataValue1
[, columnName2 = dataValue2...]
[WHERE searchCondition]

• DELETE FROM TableName


[WHERE searchCondition]

10
DML: SELECT statement

11
SELECT statements
• Specific columns, specific rows (eg SELECT
staffNo, fName,lName, salary FROM Staff;)

• DISTINCT to remove duplicates (eg SELECT


DISTINCT propertyNo FROM Viewing)

• Derived fields (eg SELECT staffNo, fName,


lName, salary/12 FROM Staff;)

• AS keyword to name column


• WHERE with =, <, >, >=, <= (eg WHERE salary >
10000 )
12
SELECT statements
• WHERE with logical AND, OR, NOT (eg WHERE
city = ‘London’ OR city = ‘Glasgow’ )

• WHERE with BETWEEN, NOT BETWEEN (eg


WHERE salary BETWEEN 20000 AND 30000)

• WHERE with IN, NOT IN (eg WHERE position IN


(‘Manager’, ‘Supervisor’)

• WHERE with LIKE, NOT LIKE (eg WHERE address


LIKE ‘%Glasgow%’)

13
SELECT statements
• ORDER BY(eg SELECT staffNo, fName, lName,
salary FROM Staff ORDER BY salary DESC)

• Aggregate COUNT(), SUM(), AVG(), MIN(),


MAX()
– SELECT COUNT(staffNo) AS myCount,
SUM(salary) AS mySum FROM Staff WHERE
position = ‘Manager’;
– SELECT MIN(salary) AS myMin, MAX(salary)
AS myMax, AVG(salary) AS myAvg FROM
Staff;

14
SELECT statements
• GROUP BY to get sub-totals
– All column names in SELECT list must
appear in GROUP BY clause unless name is
used only in an aggregate function
– SELECT branchNo, COUNT(staffNo) as
myCount FROM Staff GROUP BY branchNo;
– SELECT branchNo, COUNT(staffNo) AS
myCount, SUM(salary) AS mySum FROM Staff
GROUP BY branchNo ORDER BY branchNo;

15
SELECT statements
• HAVING to use with GROUP BY
– SELECT branchNo, COUNT(staffNo) AS
myCount, SUM(salary) AS mySum FROM Staff
GROUP BY branchNo HAVING COUNT(staffNo)
> 1 ORDER BY branchNo;

16
Summary
• SQL queries to create, alter, and delete
tables in database.
• SQL queries to manipulate data in
database.

Tomorrow, we look at more SQL.

17
Follow-up work
• Complete required reading:
– Connolly & Begg: Chapter 6:6.1-6.3.4, Chapter
7:7.1-7.3

• Reading for next lecture:


– Connolly & Begg: Chapter 6:6.3.5-6.3.10, Chapter
7:4

18

You might also like