Lecture04 -SQL Part 1
Lecture04 -SQL Part 1
to SQL – Part 1
Parvez Jugon
Structured Query Language (SQL) –
An Introduction
What is SQL?
SQL is a Programming Language, designed to manage data in a relational database.
Uses simple declarative statements (non-procedural)
Structured
Query Entities
Language
(SQL) Attributes (Column names)
Data types
Exact Numerics:
tinyint (0 to 255) smallint (-32,768 to 32,767),
int (-2,147,483,648 to 2,147,483,648) bigint (-2^63 to 2^63-1)
decimal [ (p[ ,s] )] – precision and scale e.g. decimal (7,2)
Constraints can be named – as earlier with PRIMARY KEY in the CREATE TABLE statements –
or applied directly to a column/attribute – as with not null.
We’ll look at the FOREIGN KEY constraint now and INDEX (used to create and retrieve data
from the database very quickly) later in the module.
SQL – An Introduction
Building a Database – Create the Links (FKs) and establish Referential Integrity
Relational Schema: EMPLOYEE (enum , ename, salary, floor)
PROJECT (pnum , pname, leader)
WORKS_ON (enum*, pnum*, role)
Remember: If a relational table includes a foreign key (FK) matching the primary key (PK) of
another relational table, then every value of the FK must:
• either be equal to a value of the PK in some tuple (row)
• or be wholly NULL
Foreign Keys can be created, and Referential Integrity established, using an ALTER TABLE
statement OR at time when table is created.
Requires referencing from the Foreign Key towards a corresponding Primary Key in another
table
Attributes don’t have to
To put referential integrity between enum from WORKS_ON (FK) and have the same name,
enum from EMPLOYEE (PK) but MUST have the same
ALTER TABLE WORKS_ON Data Type
ADD CONSTRAINT fk_wo1 FOREIGN KEY (enum) REFERENCES EMPLOYEE (enum);
To put referential integrity between pnum from WORKS_ON (FK) & pnum from PROJECT (PK)
ALTER TABLE WORKS_ON
ADD CONSTRAINT fk_wo2 FOREIGN KEY (pnum) REFERENCES PROJECT (pnum);
SQL – An Introduction In general, may not be possible.
Depends on data types and if data
Other useful ALTER TABLE Commands has been entered already.
Changing a datatype/size (to whole numbers only)
ALTER TABLE EMPLOYEE
MODIFY COLUMN salary int; Delete budget attribute from PROJECT
Adding a new attribute (budget to PROJECT) ALTER TABLE PROJECT
DROP COLUMN budget;
ALTER TABLE PROJECT
ADD budget decimal (9,2);
Other ways to inserting records in a table (Selecting records from other table)
INSERT INTO CUSTOMERS(EMPLOYEE_ID, LASTNAME, FIRSTNAME, STARTDATE,
SALARY) SELECT EMPLOYEE_ID, LASTNAME, FIRSTNAME, STARTDATE, SALARY
FROM CUSTOMERS;
NOTE – Referential Integrity must be considered when adding, deleting or updating records
Using the relational schema and sample data on slide 7, consider each of the examples
above. Will the queries execute correctly? INSERT INTO EMPLOYEE
VALUES (852559, 'Brown’,
1. Add new employee record ‘2021/02/15’, 18000, 4);
852559 , 'Brown', ‘2021/02/15’, 18000, 4
DELETE FROM EMPLOYEE
2. Delete employees earning WHERE salary > 18000
above 18000 on the 2nd floor AND floor = 2 ;
Get a list of staff and the salaries they earn ordering by those who earn the highest salary?
**Use parentheses
SELECT ename, salary, 12*(salary+100)
FROM EMPLOYEE;
SELECT ename
SQL – Comparison Conditions FROM EMPLOYEE
WHERE ename LIKE 'S%';
SELECT * EMPLOYEE
WHERE floor IS NULL;
SELECT * EMPLOYEE
WHERE floor IS NOT NULL;
Some Examples:
Get the names of employees starting with 'S‘ Get the names of employees containing the
string 'row' anywhere
The NVL2 function examines the first expression. If the first expression is not null,
then the NVL2 function returns the second expression. If the first expression is null,
then the third expression is returned.
NULLIF
SELECT last_name,
COALESCE(commission_pct, salary, 10) comm
FROM employee ORDER BY commission_pct;
Concatenation
SELECT CONCAT(enum, ” ", ename, ” ", salary)
FROM Employee;
SQL – Date
DEFAULT DATE IN MariaDB is YYYY-MM-DD
Today’s date Now()
SELECT COUNT(*)
FROM EMPLOYEE; COUNT all rows even
duplicates
SELECT COUNT(floor)
FROM EMPLOYEE;
Without DISTINCT, the query would return multiple entries for each role –
as per the original WORKS_ON table
SQL – Queries on a Single Table
The GROUP BY Clause
Groups result table by attribute(s).
GROUP BY clauses group the data by value using a field that MUST therefore have
duplicate values so that at least some groups have multiple records.
An aggregate function can then be applied to each group.
All simple attributes appearing in the SELECT clause MUST appear in the GROUP BY clause
e.g. Get the average salary by floor of all employees
Result Table
SELECT floor, AVG(salary)
floor AVG (salary)
FROM EMPLOYEE
1 22750
GROUP BY floor ; 2 18375
3 17500
EMPLOYEE 4 26980
enum ename salary floor
852341 Smith 15000 1
852358 Jones 19000 3 Get the total salary spent by the company per year
852407 Brown 16000 3
852455 White 25100 2 SELECT YEAR(sdate), SUM(salary)
852491 Adams 30500 1 FROM EMPLOYEE
852514 Doyle 11650 2
GROUP BY YEAR(sdate) ;
852530 Evans 26980 4
SQL – Queries on a Single Table
When GROUP BY clauses are being used:
Can't use a normal attribute in the GROUP BY clause that does not already appear in the
SELECT clause
Don’t use more than one attribute in the GROUP BY clause (unless you intend to use
grouping and subgrouping – not covered in these notes)
The attribute you group by should normally be repeated in the SELECT clause
The HAVING Clause
Can ONLY be used when a GROUP BY clause is present - used to choose between groups.
Can ONLY contain expressions using aggregate functions
e.g. Get the floors having two or more employees Result Table
SELECT floor floor
FROM EMPLOYEE 1
GROUP BY floor 2
HAVING COUNT(*) >= 2 ; 3
HAVING clauses are used to choose between groups and cannot appear without a preceding
GROUP BY clause.
HAVING clauses MUST be constructed as:
Aggregate function compared with a value
count( ), avg( ), etc =, <, >, etc number