5Cosc020W Database Systems - Lecture 05
5Cosc020W Database Systems - Lecture 05
Dr Francois ROUBERT
[email protected]
1
Lecture 05 – Outline
o DBMS and DB Languages.
• Data Definition Language (DDL); Data Manipulation Language (DML); Data
Control Language (DCL).
o SQL – Structured Query Language.
• Capabilities, Properties & Statements.
o Popular RDBMSs
• Oracle and MySQL; MySQL architecture; MySQL compliance with standard SQL
o SQL to create and populate tables.
• Creating tables with CREATE; populating tables with INSERT INTO.
o SQL to query existing tables.
• Retrieving columns with SELECT FROM; arithmetic expressions; concatenating.
• Restricting rows with WHERE; comparison and logical operators; precedence.
• Sorting data with ORDER BY.
2
Phases and outputs of Database Design (recap)
5COSC024W
Server-side
Web Development
5COSC020W
DATABASE
SYSTEMS
4
Database Management System (DBMS)
Software that allows users to:
o Multi-user
• DBA, Database Designer, Application Developer.
o Non procedural
• Specify what data is required, not how to get it
9
2 essential DBMSs: Oracle and MySQL
o Oracle
• Commercial multi-model DBMS: primarily ORDBMS with add-ons.
• Commonly used for online transaction processing (OLTP) and Data Warehousing
(DW) and mixed (OLTP & DW).
• Available on-prem, on-cloud or as hybrid cloud installation.
• Latest release – Oracle Database 21c: native JSON data type, In-database
JavaScript execution, SQL Macros to call PL/SQL, optimised graph models, in-
database machine learning, blockchain tables, etc. (Oracle Database Insider, 2021)
7 IBM Db2 Relational MM: relational, document store, RDF store, spatial
8 Elasticsearch Search Engine MM: search engine, document store, spatial
9 SQLite Relational SM: relational only
10 MS Access Relational SM: relational only
(DB-Engines,2021) 12
MySQL Storage engine architecture
o MySQL DBMS
• Claims to comply with standard SQL and to support “most of the W3C XPath
standard”. (MySQL Developer Zone, 2021b)
• Adds extensions to SQL or support for non-SQL features to increase the usability of
MySQL Server. (MySQL Developer Zone, 2021b)
o Examples of differences
• General syntax: ' and " both accepted; \ is the escape character
• SQL syntax: use IF EXISTS to drop a table; drop multiple tables, etc.
• Operators: can be used in SELECT clause
• Database objects & data types: see (MySQL Developer Zone, 2021c). 14
Database Objects
Standard SQL (Oracle) SQL in MySQL
Table / Basic unit of storage; Basic unit of storage;
Relation composed of rows composed of rows
Subsets of data from one or more Subsets of data from one or
View tables more tables
Index Improves performance of some Improves performance of some
queries queries
Sequence Separate object; Not a separate object;
generates numeric values use AUTO_INCREMENT
Separate object;
Synonym gives alternative names to objects Not a separate object
15
Simple Logical ERD has
Dept Emp
deptNo{PK} 0..1 0..* empId {PK}
dName fName
dLoc lName
createDate position
hireDate
salary
commPct
Optional on Optional on email
Dept side Emp side deptNo{FK}
17
Creating Emp table
INSERT INTO
Dept (deptNo, dName, dLoc, createDate)
VALUES
(10, 'Database Management', 'New Cav', '2021-03-21'),
(20, 'Systems Design', 'Harrow', '2021-01-06'),
(30, 'IT Development', 'New Cav', '2021-04-25'),
(40, 'Project Management', 'Regents', '2021-02-12'),
(50, 'Systems Testing', 'Marylebone', NULL);
19
Populating Emp tables
INSERT INTO
Emp (empId, fName, lName, position, hireDate, salary, commPct, email, deptNo)
VALUES
(101, 'Joe', 'Bloggs', 'Project Manager', '2021-03-01', 5200.00, 0.25, '[email protected]', 40),
(102, 'Jim', 'Marts', 'Database Admin', '2021-03-22', 4400.00, NULL, '[email protected]', 10),
(103, 'Jen', 'Fonts', 'Python Developer', '2021-04-28', 4800.00, 0.12, '[email protected]', 30),
(104, 'Jon', 'Pop', 'Database Architect', '2021-03-23', 4000.00, NULL, '[email protected]', 10),
(105, 'Tom', 'Dogs', 'UI Designer', '2021-01-10', 4000.00, NULL, '[email protected]', 20),
(106, 'Tek', 'Roggs', 'Project Manager', '2021-01-01', 5200.00, 0.13, '[email protected]', 40),
(107, 'Tim', 'Clogs', 'Java Developer', '2021-04-29', 4300.00, 0.15, '[email protected]', NULL),
(108, 'Tam', 'Kelps', 'UX Designer', '2021-01-15', 3900.00, NULL, '[email protected]', NULL);
20
Retrieving columns with SELECT…FROM clauses
SELECT *
FROM Dept;
o Example:
SELECT deptNo
FROM Emp;
o To remove duplicates use DISTINCT.
Operator Description
AND Returns TRUE if BOTH conditions are true
OR Returns TRUE if EITHER conditions are true
NOT Returns TRUE if condition is FALSE
30
Comparing with one or two values with BETWEEN…AND
o Comparison operator in WHERE clause.
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE salary >= 4500;
o BETWEEN … AND operator (by default boundaries values are included!).
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE salary BETWEEN 4000 AND 4800;
o Two comparison operators with the AND logical operator.
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE salary > 4000 AND salary < 4800; 31
Comparing with a list of values with IN
o Using IN operator to see if a value match any of the values in a list.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE deptNo IN (10, 30, 40);
o Three = comparison operators with OR logical operator to do the same.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE deptNo = 10 OR deptNo = 30 OR deptNo = 40;
o Combining NOT and IN operator to get the opposite.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE deptNo NOT IN (10, 30, 40); 32
Matching Character Patterns with LIKE
o Finding words that start with a specific letter.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE lName LIKE 'M%';
o Finding words that contain a specific letter anywhere.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE lName LIKE '%o%';
o Finding words that contain a specific letter in a specific position.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE lName LIKE '_o%'; 33
Finding null values with IS NULL
o Retrieving records for which one attribute has no values.
SELECT deptNo, dName, dLoc, createDate
FROM Dept
WHERE createDate IS NULL;
o Retrieving records for which one attribute has values.
SELECT empId, lName, position, commPct, deptNo
FROM Emp
WHERE commPct IS NOT NULL;
o Retrieving children records with no values in their FKs.
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE deptNo IS NULL; 34
Using logical operators
o AND operator
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE position LIKE '%Database%' AND salary > 4300;
o OR operator
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE position LIKE '%Database%' OR salary > 4300;
o AND with NOT
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE deptNo NOT IN (10,30) AND salary > 4300; 35
Rules of Precedence
Precedence Description
1 Arithmetic Operators
2 Concatenation
3 Comparison conditions
4 IS NULL, LIKE, IN
5 BETWEEN
6 Not equal to
7 NOT Logical conditions
8 AND Logical conditions
9 OR Logical conditions
Use brackets to override rules of precedence! 36
Precedence
o Without brackets: AND takes precedence over OR.
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE position LIKE '%Database%'
OR position LIKE '%Developer%'
AND salary > 4400;
o With brackets: override AND precedence and make OR take precedence.
SELECT empId, lName, position, salary, deptNo
FROM Emp
WHERE (position LIKE '%Database%'
OR position LIKE '%Developer%')
AND salary > 4400;
37
Sort DATA with ORDER BY
o Sorting in ascending order: the use ASC keyword is optional.
SELECT empId, lName, hireDate, salary
FROM Emp
ORDER BY hireDate;
o Sorting in descending order and using an alias.
SELECT empId, lName, hireDate, salary * 12 AS AnnSal
FROM Emp
ORDER BY AnnSal DESC;
o Sorting by multiple columns
SELECT empId, lName, hireDate, salary, deptNo
FROM Emp
ORDER BY deptNo, salary DESC; 38
References
• Module Reading List:
https://rl.talis.com/3/westminster/lists/2CAA7D6B-DCAD-AB71-C97B-7FEFCB499
C28.html