Professional Documents
Culture Documents
ERD
Relational
Data Model
What is SQL?
stands for Structured Query Language
Schema
The structure that contains descriptions of objects created by a user (base tables, views,
constraints)
Primary key
Default value
Domain constraint
Data Integrity Controls
Referential integrity–constraint that ensures that foreign key values
of a table must match primary key values of a related table in 1:M
relationships
Restricting:
Deletes of primary records
Updates of primary records
Inserts of dependent records
Identifying foreign keys and establishing relationships
[see demo]
Primary key of
parent table
Foreign key of
dependent table
Ensuring data integrity through updates
Relational integrity is
enforced via the
primary-key to
foreign-key match
Changing and Removing Tables
Add a new column to a table.
Drop a column from a table.
Add a new table constraint.
Drop a table constraint.
Set a default for a column.
Drop a default for a column.
Example – Property For Rent
CREATE TABLE PropertyForRent (
propertyNo PNumber NOT NULL, ….
rooms PRooms NOT NULL DEFAULT
4,
rent PRent NOT
NULL, DEFAULT 600,
ownerNo OwnerNumber NOT NULL,
staffNo StaffNumber
Constraint
StaffNotHandlingTooMuch ….
branchNo BranchNumber NOT
NULL,
PRIMARY KEY (propertyNo),
FOREIGN KEY (staffNo) REFERENCES Staff
ON DELETE SET NULL ON UPDATE
CASCADE ….);
ALTER TABLE
Remove constraint from PropertyForRent that staff are not allowed
to handle more than 100 properties at a time. Add new column to
Client table.
Creating indexes
Speed up random/sequential access to base table data
Example
CREATE INDEX NAME_IDX ON
CUSTOMER_T(CUSTOMER_NAME)
This makes an index for the CUSTOMER_NAME field of the
CUSTOMER_T table
3.2 Data Manipulation
Data Manipulation
Data Manipulation
Adds data to a table
Inserting into a table
INSERT INTO CUSTOMER_T VALUES (001, ‘Contemporary Casuals’,
‘1355 S. Himes Blvd.’, ‘Gainesville’, ‘FL’, 32601);
Inserting a record that has some null attributes requires identifying the
fields that actually get data
INSERT INTO PRODUCT_T (PRODUCT_ID,
PRODUCT_DESCRIPTION,PRODUCT_FINISH, STANDARD_PRICE,
PRODUCT_ON_HAND) VALUES (1, ‘End Table’, ‘Cherry’, 175, 8);
Inserting from another table
INSERT INTO CA_CUSTOMER_T SELECT * FROM CUSTOMER_T
WHERE STATE = ‘CA’;
Creating Tables with Identity Columns, see demo
SELECT *
FROM Staff;
Specific Columns, All Rows
List the positions of all staff members.
SELECT position
FROM Staff;
SELECT DISTINCT(position)
FROM Staff;
Calculated Fields
Produce list of monthly salaries for all staff, showing staff number,
first/last name, and salary.
Note: the LIKE operator allows you to compare strings using wildcards.
For example, the % wildcard in ‘%Desk’ indicates that all strings that
have any number of characters preceding the word “Desk” will be
allowed
Venn Diagram from Previous Query
Compound Comparison Search Condition
List the details of supervisors as well as managers.
SELECT *
FROM staff
WHERE position = ‘Supervisor’ OR position =
‘Manager’;
Range Search Condition
List all staff with a salary between 20,000 and 30,000.
SELECT *
FROM staff
WHERE salary BETWEEN 20000 AND 30000;
SELECT *
FROM staff
WHERE salary>=20000 AND salary <= 30000;
SELECT *
FROM staff
WHERE position=‘Manager’ OR
position=‘Supervisor’;
SELECT *
FROM staff
WHERE lname LIKE ‘%e%’;
SELECT *
FROM staff
WHERE position = “”;
SELECT *
FROM staff
WHERE position IS NULL;
Negated version (IS NOT NULL) can test for non-null values.
Single Column Ordering
List staff number, first name, last name and salary for all staff, arranged in
descending order of salary.
List staff number, first name, last name and position, arranged in
ascending order of position
SELECT *
FROM staff
WHERE POSITION IN (‘Manager’,
‘Supervisor’)
ORDER BY POSITION, FNAME;
Note: the IN operator in this example allows you to include rows whose
POSITION value is either Manager or Supervisor. It is more efficient than
separate OR conditions
SELECT Statement – Grouping
Use GROUP BY clause to get sub-totals.
SELECT and GROUP BY closely integrated: each item in SELECT list must
be single-valued per group, and SELECT clause may only contain:
column names
aggregate functions
constants
expression involving combinations of the above.
All column names in SELECT list must appear in GROUP BY clause unless
name is used only in an aggregate function.
If WHERE is used with GROUP BY, WHERE is applied first, then groups are
formed from remaining rows satisfying predicate.
ISO considers two nulls to be equal for purposes of GROUP BY.
Example: Use of GROUP BY
Find number of staff in each branch and their total salaries.
SELECT branchNo,
COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM staff
GROUP BY branchNo
ORDER BY branchNo;
Restricted Groupings – HAVING clause
HAVING clause is designed for use with GROUP BY to restrict groups that appear
in final result table.
Similar to WHERE, but WHERE filters individual rows whereas HAVING filters groups.
Column names in HAVING clause must also appear in the GROUP BY list or be
contained within an aggregate function.
For each branch with more than 1 member of staff, find number of staff in each branch and sum of
their salaries.
SELECT branchNo,
COUNT(staffNo) AS myCount,
SUM(salary) AS mySum
FROM staff
GROUP BY branchNo
HAVING COUNT(staffNo) > 1
ORDER BY branchNo;
What have we learnt?
Define a database using SQL data definition language
Create