0% found this document useful (0 votes)
116 views39 pages

5Cosc020W Database Systems - Lecture 05

The document discusses database implementation and querying using SQL. It covers creating tables, populating tables with data, and retrieving data from single tables using simple queries. The key SQL statements covered are CREATE TABLE, INSERT INTO, SELECT, WHERE, and ORDER BY.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views39 pages

5Cosc020W Database Systems - Lecture 05

The document discusses database implementation and querying using SQL. It covers creating tables, populating tables with data, and retrieving data from single tables using simple queries. The key SQL statements covered are CREATE TABLE, INSERT INTO, SELECT, WHERE, and ORDER BY.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

5COSC020W DATABASE SYSTEMS – LECTURE 05

Database Implementation & Querying – Simple Queries


Creating tables and retrieving records from single Tables in SQL

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)

Data Requirements BUSINESS USER’S DATA NEEDS


to support business processes

CONCEPTUAL DATA MODEL OR CONCEPTUAL SCHEMA


 CONCEPTUAL DESIGN
of the data required by business users

LOGICAL DATA MODEL OR LOGICAL SCHEMA


 LOGICAL DESIGN (Mapping)
based on specific file data organisation (e.g. relational model)

PHYSICAL OR INTERNAL SCHEMA


 PHYSICAL DESIGN
storage structures, file organizations, indexes, access paths

DATABASE IMPLEMENTED & QUERIED IN DBMS


 DATABASE IMPLEMENTATION & QUERYING data structures, constraints, data values, data description
3
Database System

5COSC024W
Server-side
Web Development

5COSC020W
DATABASE
SYSTEMS

4
Database Management System (DBMS)
Software that allows users to:

o Define a database Data Definition Language (DDL)


• Specify data types, structures & constraints of the data
• Specify the meta-data: description of the data

o Construct a database Data Manipulation Language (DML)


• Store data in storage medium controlled by DBMS

o Manipulate a database Data Manipulation Language (DML)


• Querying, updating and reporting on the data

o Control access to a database Data Control Language (DCL)


• Ensuring security, integrity, concurrency & recovery 5
SQL – Structured Query Language

o Pronounced “S.Q.L” or “Sequel”.

o Comprehensive database language, allows users to:


• create the database and relation structures;
• perform insertion, modification, deletion of data from relations;
• perform simple and complex queries.

o SQL enables the CRUD operations


• Create new records in the database tables.
• Read or Retrieve records from the database tables.
• Update existing records in database tables.
• Delete records from database tables
6
SQL Capabilities
o Supporting data definition, queries and updates.
• CRUD operations

o Defining views on the database.


• Customised access to a subset of the database.

o Specifying security and authorisation.


• Ensure only authorised users can access DB: which DB objects & which operation.

o Defining integrity constraints


• Entity integrity (rule on PK) and referential integrity (rule on FK).

o Embedding SQL statements into other programming language 7


SQL Properties
o Portable and standardised
• ANSI/ISO make it the formal and standard language for relational DBs.
• Core: implemented by all RDBMs vendors.
• Specialised extensions: spatial data, temporal data, data mining, data warehousing,
OLAP.

o Multi-user
• DBA, Database Designer, Application Developer.

o Non procedural
• Specify what data is required, not how to get it

o Close to natural language and easy to learn: simple English keywords


• CREATE; INSERT INTO; ALTER; DELETE
• SELECT FROM WHERE 8
SQL Statements and subsets of SQL
SELECT
INSERT
UPDATE Data Manipulation Language (DML)
DELETE
MERGE
CREATE
ALTER
DROP
RENAME Data Definition Language (DDL)
TRUNCATE
COMMENT
GRANT
REVOKE Data Control Language (DCL)
COMMIT
ROLLBACK Transaction Control
SAVEPOINT

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)

o MySQL ⚠️Not to be confused with language SQL


• Open-source multi-model DBMS: primarily RDBMS with add-ons.
• Bought by Sun Microsystems (2008) which was then bought by Oracle (2010).
• Latest release – MySQL 8.0: transactional data dictionary, SQL roles for
permissions, MySQL DB service for cloud-native apps, document store for
NoSQL apps, JSON support (MySQL, 2021) 10
Classifying DBMSs based on Data Models
o Relational Databases or SQL Databases (covered in lectures 01-07)
• Structured Data as collection of tables with fields, records, PKs and FKs.
• Uses high level Structured Query Language (SQL) for CRUD operations.

o XML Databases (covered in lectures 08-10)


• Semistructured data as elements that can be nested to create tree structures.
• Standard for structuring and exchanging data over the Web.

o NoSQL Databases (covered in lectures 11)


• Key-value data model: each data value associated to unique key for fast retrieval.
• Graph data model: data represented as graphs i.e. labelled nodes and edges.
• Document data model: data as collection of documents in JSON or XML.

o Object Databases (not covered in this module)


• Data as collection of objects (in classes) with properties & operations. 11
Most popular Database Management Systems (August 2021)
DBMS DB Model Multi-Model (MM) or Single-Model (SM)
1 Oracle Relational MM: relational, document store, graph, RDF store, spatial
2 MySQL Relational MM: relational, document store, spatial
3 MS SQL Server Relational MM: relational, document store, graph, spatial
4 PostgreSQL Relational MM: relational, document store, spatial
5 MongoDB Document MM: document store, spatial, search engine, time series
6 Redis Key-value MM: key-value, document store, graph, spatial, search engine, time series

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

(MySQL Developer Zone, 2021a) 13


MySQL compliance with Standard SQL
o Standard SQL
• Defined by the ISO/ANSI standardisation.
• Multiple revisions. SQL:1999; SQL:2003; SQL:2006; SQL:2011; SQL:2016

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}

o Cardinalities are 1 and *


• One-to-Many Relationship, Dept is the Parent table, Emp is the Child table.
• Child Emp carries a FK deptNo that references deptNo the PK of its parent Dept
o Participations are both 0
• A record in Dept may not have any corresponding records in Emp.
• A record in Emp may not have any corresponding records in dept: FK can be null.
16
Creating Dept table

CREATE TABLE Dept


(
deptNo INTEGER,
dName VARCHAR(20) UNIQUE NOT NULL,
dLoc VARCHAR(30) NOT NULL,
createDate DATE,
CONSTRAINT d_dno_pk PRIMARY KEY (deptNo)
);

17
Creating Emp table

CREATE TABLE Emp


(
empId INTEGER,
fName VARCHAR(50) NOT NULL,
lName VARCHAR(50) NOT NULL,
position VARCHAR(50) NOT NULL,
hireDate DATE NOT NULL,
salary DECIMAL(7,2) NOT NULL,
commPct DECIMAL(2,2),
email VARCHAR(100) UNIQUE NOT NULL,
deptNo INTEGER,
CONSTRAINT e_eid_pk PRIMARY KEY (empId),
CONSTRAINT e_dno_fk FOREIGN KEY (deptNo) REFERENCES Dept(deptNo)
);
18
Populating Dept tables

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

o Selecting all columns

SELECT *
FROM Dept;

o Selecting specific columns

SELECT dName, dLoc


FROM Dept;
21
Arithmetic Expressions
o Use arithmetic operators with number data and date data.
Operator Description
+ Add
- Substract
* Mulltiply
/ Divide

o Example:

SELECT lName, salary, salary + 300


FROM Emp;
22
Operator Precedence
o Without brackets: * takes precedence over +.

SELECT lName, salary, 12 * salary + 200


FROM Emp;
o With brackets: override * precedence and make + take precedence

SELECT lName, salary, 12 * (salary + 200)


FROM Emp;
Which one is better from an employee’s perspective? 23
Null Values
o A null value is an unavailable, unassigned, unknown or inapplicable value.
o A null value is not the same as zero or a blank value.

SELECT lName, salary, commPct


FROM Emp;
o An arithmetic expression containing a null value will return a null value.

SELECT lName, salary, 12 * salary * commPct


FROM Emp;
24
Column Aliases

o A column alias renames a column heading.


o It follows the AS keyword and uses " " to display the exact same label.

SELECT lName AS "Surname", salary AS "Monthly Salary",


commPct AS "Commission"
FROM Emp;

o It is useful as part of calculations.

SELECT lName AS "Surname", 12 * salary AS "Yearly Salary",


12 * (salary + salary * commPct) AS "Final Intake"
FROM Emp;
25
Concatenation
o Concatenate columns: use CONCAT function.

SELECT CONCAT (fName,lName) AS "Full Name"


FROM Emp;
o Concatenate and introduce a blank space.
SELECT CONCAT (fName,' ',lName) AS "Full Name"
FROM Emp;
o Concatenate and introduce a sting.
SELECT CONCAT (fName,' ',lName, ' is a ',position)
AS "Full Name and Job"
FROM Emp;
26
Duplicate Rows
o Default display: all rows, including duplicates.

SELECT deptNo
FROM Emp;
o To remove duplicates use DISTINCT.

SELECT DISTINCT deptNo


FROM Emp;
Why are many values of deptNo duplicated in the Emp table? 27
Restricting rows with the WHERE clause
o No quotes required in WHERE clause with numbers.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE deptNo = 10;
o Quotes required in WHERE clause with strings.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE position = 'Database Architect';
o Quotes required in WHERE clause with dates.
SELECT empId, lName, position, hireDate, deptNo
FROM Emp
WHERE hireDate >= '2021-03-01'; 28
Comparison Operators
Operator Description
= Equal to
> Strictly greater than
>= Greater than or equal to
< Strictly less than
<= Less than or equal to
<> Not equal to
BETWEEN … AND … Between 2 values (inclusive)
IN (SET) Match any of the list of values
LIKE Match a character pattern
IS NULL Is a null value 29
Logical Operators

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

• Connolly, T. & Begg, C. E. (2015). Database systems: a practical approach to design,


implementation and management. 6th Edition (Global Edition). Pearson Education.
• DB-Engines (2022). DB-Engines. Available from https://db-engines.com/. [Accessed 15
September 2022].
• Elmasri, R. & Navathe, S. (2017). Fundamentals of Database Systems. 7th Edition
(Global Edition). Pearson Education.
• MySQL(2022). MySQL Enterprise Edition. https://www.mysql.com/products/enterprise/.
[Accessed 15 September 2022].
• MySQL Developer Zone (2022a). Overview of MySQL Storage Engine Architecture.
https://dev.mysql.com/doc/refman/8.0/en/pluggable-storage-overview.html. [Accessed 15
September 2022].
• MySQL Developer Zone (2022b). MySQL Standards Compliance.
https://dev.mysql.com/doc/refman/8.0/en/compatibility.html. [Accessed 15 Sept 2022].
• MySQL Developer Zone (2022c). MySQL Data Types. 39

You might also like