You are on page 1of 63

Week 3

Basic Topics in SQL

BC2402 Designing and Developing Databases


Agenda
 Define a database using SQL data definition language

 Write queries using SQL (slide 26)


3.1 Introduction to SQL
Structured Query Language
Previously…
 Use Case
 ERD
 Relational Data Model Use Case

ERD

Relational
Data Model
What is SQL?
 stands for Structured Query Language

 lets you access and manipulate databases

 became a standard of the American National Standards Institute


(ANSI) in 1986, and of the International Organization for
Standardization (ISO) in 1987
What can SQL do?
 create new databases
 create new tables in a database
 create views in a database
 create stored procedures in a database

 insert records in a database


 update records in a database
 delete records from a database

 execute queries against a database


 set permissions on tables, procedures, and views
SQL is a Standard - BUT....
 Although SQL is an ANSI/ISO standard, there are different
versions of the SQL language.

 However, to be compliant with the ANSI standard, they all support


at least the major commands (such as SELECT, UPDATE, DELETE,
INSERT, WHERE) in a similar manner.

Note: Most of the SQL database programs also have their


own proprietary extensions in addition to the SQL standard!
SQL Environment
 Catalog
 A set of schemas that constitute the description of a database

 Schema
 The structure that contains descriptions of objects created by a user (base tables, views,
constraints)

 Data Definition Language (DDL)


 Commands that define a database, including creating, altering, and dropping tables and
establishing constraints

 Data Manipulation Language (DML)


 Commands that maintain and query a database

 Data Control Language (DCL)


 Commands that control a database, including administering privileges and committing data
Data Definition
SQL Database Definition
 Data Definition Language (DDL)

 Major CREATE statements:


 CREATE SCHEMA – defines a portion of the database owned by
a particular user
 CREATE TABLE – defines a table and its columns
 CREATE VIEW – defines a logical (see a part of a table, ie.
those info needed) table from one or more views
Table Creation
 Creates a table with one or more
columns of the specified dataType.
 With NOT NULL, system rejects any
attempt to insert a null in the
column.
 Can specify a DEFAULT value for
the column.
 Primary keys should always be
specified as NOT NULL.
 FOREIGN KEY clause specifies FK
along with the referential action.
ERD For SQL Examples
Database Definition Commands
 See DEMO
Defining attributes and their data types
Primary keys
can never have
NULL values
Non-nullable
specification

Identifying primary key


Non-nullable specifications

Primary key

Some primary keys are composite– composed of multiple


attributes
Controlling the values in attributes

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.

ALTER TABLE PropertyForRent


DROP CONSTRAINT
StaffNotHandlingTooMuch;
ALTER TABLE Client
ADD prefNoRooms PRooms;
Drop Table
DROP TABLE TableName [RESTRICT | CASCADE]

e.g. DROP TABLE PropertyForRent;


 Removes named table and all rows within it.
 With RESTRICT, if any other objects depend for their existence on
continued existence of this table, SQL does not allow request.
 With CASCADE, SQL drops all dependent objects (and objects
dependent on these objects).
Schema Definition
 Control processing/storage efficiency:
 Choice of indexes
 File organizations for base tables
 File organizations for indexes
 Data clustering
 Statistics maintenance

 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

Inserting into a table does not require explicit memberID entry or


field list
Delete Statement
 Removes rows from a table

 Delete certain rows


DELETE FROM SQL_1.member
WHERE memberID = 1;

 Delete all rows [disabled on mySQL by default]


 DELETE FROM SQL_1.member;
Update Statement
 Modifies data in existing rows
UPDATE PRODUCT_T
SET UNIT_PRICE = 775
WHERE PRODUCT_ID = 7;
SQL Statement Processing Order
SELECT Statement
 Used for queries on single or multiple tables
 Clauses of the SELECT statement:
 SELECT
 List the columns (and expressions) that should be returned from the
query
 FROM
 Indicate the table(s) or view(s) from which data will be obtained
 WHERE
 Indicate the conditions under which a row will be included in the result
 GROUP BY
 Indicate categorization of results
 HAVING
 Indicate the conditions under which a category (group) will be included
 ORDER BY
 Sorts the result according to specified criteria
SELECT Statement
SELECT [DISTINCT | ALL]
{* | [columnExpression [AS newName]] [,...] }
FROM TableName [alias] [, ...]
[WHERE condition]
[GROUP BY columnList] [HAVING condition]
[ORDER BY columnList]

Order of the clauses cannot be changed.


Only SELECT and FROM are mandatory.
All Columns, All Rows
List full details of all staff.

SELECT staffNo, fName, lName,


position, sex, DOB, salary,
branchNo
FROM Staff;

Can use * as an abbreviation for ‘all columns’:

SELECT *
FROM Staff;
Specific Columns, All Rows
List the positions of all staff members.

SELECT position
FROM Staff;

Use DISTINCT to eliminate duplicates:

SELECT DISTINCT(position)
FROM Staff;
Calculated Fields
Produce list of monthly salaries for all staff, showing staff number,
first/last name, and salary.

SELECT staffNo, fName, lName, salary/12


FROM Staff;

To name column, use AS clause

SELECT staffNo, fName, lName, salary/12


AS monthlySalary
FROM Staff;
SELECT: Using Alias
 Alias is an alternative column or table name

SELECT CUST.CUSTOMER AS NAME,


CUST.CUSTOMER_ADDRESS
FROM CUSTOMER_V CUST
WHERE NAME = ‘Home
Furnishings’;
SELECT Statement – Aggregates
 ISO standard defines five aggregate functions:

COUNT returns number of values in specified


column.
SUM returns sum of values in specified column.
AVG returns average of values in specified column.
MIN returns smallest value in specified column.
MAX returns largest value in specified column.
Often used to get a sense of a dataset;
provide some basis for investigation into
missingness of data**
SELECT Statement – Aggregates
 Each operates on a single column of a table and returns a single
value.
 COUNT, MIN, and MAX apply to numeric and non-numeric fields, but
SUM and AVG may be used on numeric fields only.
 Apart from COUNT(*), each function eliminates nulls first and
operates only on remaining non-null values.
 COUNT(*) counts all rows of a table, regardless of whether nulls or
duplicate values occur.
 Can use DISTINCT before column name to eliminate duplicates.
 DISTINCT has no effect with MIN/MAX, but may have with
SUM/AVG.
SELECT Statement – Aggregates
 Aggregate functions can be used only in SELECT list and in HAVING
clause.

 If SELECT list includes an aggregate function and there is no GROUP


BY clause, SELECT list cannot reference a column out with an
aggregate function. For example, the following is illegal:

SELECT staffNo, COUNT(salary)


FROM Staff;

This statement can be executed (legal)


under some settings of mySQL, but the
output is confusing
SELECT: Using a Function
 Using the COUNT aggregate function to find totals

SELECT COUNT(*) FROM ORDER_LINE_V


WHERE ORDER_ID = 1004;

Note: with aggregate functions you can’t have single-valued


columns included in the SELECT clause
Example: COUNT(*)
How many female staff are there?

SELECT COUNT(*) AS myCount


FROM staff
WHERE sex = ‘F’;

How many assistants are there?

SELECT COUNT(*) AS myCount


FROM staff
WHERE position = ‘Assistant’;
Example: Use of COUNT and SUM
Find number of Managers and sum of their salaries.

SELECT COUNT(*) AS myCount,


SUM(salary) AS mySum
FROM staff
WHERE position = ‘Manager’;

Find minimum, maximum, and average staff salary.

SELECT MIN(salary) AS myMin,


MAX(salary) AS myMax,
AVG(salary) AS myAvg
FROM staff;
Comparison Search Condition
List all staff with a salary greater than 10,000.

SELECT staffNo, fName, lName, position,


salary
FROM staff
WHERE salary > 10000;
SELECT: Boolean Operators
 AND, OR, and NOT Operators for customizing conditions
in WHERE clause

SELECT PRODUCT_DESCRIPTION, PRODUCT_FINISH,


STANDARD_PRICE
FROM PRODUCT_V
WHERE (PRODUCT_DESCRIPTION LIKE ‘%Desk’
OR PRODUCT_DESCRIPTION LIKE ‘%Table’)
AND STANDARD_PRICE > 300;

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;

 BETWEEN test includes the endpoints of range.


Range Search Condition
 Also a negated version NOT BETWEEN.
 BETWEEN does not add much to SQL’s expressive
power. Could also write:

SELECT *
FROM staff
WHERE salary>=20000 AND salary <= 30000;

 Useful, though, for a range of values.


Set Membership
List all managers and supervisors.
SELECT *
FROM staff
WHERE position IN (‘Manager’, ‘Supervisor’);
Set Membership
 There is a negated version (NOT IN).
 IN does not add much to SQL’s expressive power.
Could have expressed this as:

SELECT *
FROM staff
WHERE position=‘Manager’ OR
position=‘Supervisor’;

 IN is more efficient when set contains many values.


Pattern Matching
Find all staff with an ‘e’ in their last names.

SELECT *
FROM staff
WHERE lname LIKE ‘%e%’;

 SQL has two special pattern matching symbols:


 %: sequence of zero or more characters;
 _ (underscore): any single character.
 LIKE ‘%e%’ means a sequence of characters of any length
containing ‘e’, such as ‘Howe’, ‘Beech’, ‘White’, ‘Lee’.
NULL Search Condition
INSERT INTO staff values
("SL82", "Johnson", "Victor", "", "M", "12-Oct-55", 90000.00,
"B005"),
("XX99", "Jayden", "Choi", NULL, "M", "1-Jan-16", 30000.00,
"B005");

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.

SELECT staffNo, fName, lName, salary


FROM staff
ORDER BY salary DESC;

List staff number, first name, last name and position, arranged in
ascending order of position

SELECT staffNo, fName, lName, position


FROM staff
ORDER BY position;
Multiple Column Ordering

SELECT staffNo, fName, lName,


position, salary
FROM staff
ORDER BY position, salary DESC;
SELECT: Sorting Results with the ORDER BY Clause

 Sort the results first by POSITION, and within a position


by FNAME

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

 Write queries using SQL


 Select
Quiz and Class Exercises
What’s next?
 Using SQL to work on multiple tables

You might also like