Title: SQL I
1SQL - I
- Create Tables
- Basic Select
- Select with Join
- Other Select Options
2What is SQL?
- Structured Query Language
- Language for database definition, manipulation,
and control - International standard
- Standalone and embedded usage
- Intergalactic database speak
3CREATE Syntax
- CREATE TABLE table_name (
- Col_name data-type ltoption_argsgt,
- Repeat above for each column
- ltoptional table constraintsgt
- )
- See Figure, page 60 of text.
4MySQL Create Table Example
- CREATE TABLE Student (
- StuID Char(9) NOT NULL,
- PawPrint VarChar(20) NOT NULL,
- LName VarChar(20),
- FName VarChar(20),
- Email VarChar(20) NOT NULL
- Constraint PK_Student
- Primary Key (StuID) )
5Converting ERD to Tables
- Entity Type Rule Each entity type (except
subtypes) becomes a table and inherits PK and
attributes from entity - 1-M Relationship Rule Each 1-M relationship
becomes a foreign key in the table near the
Crows Foot symbol (child entity type). - M-N Relationship Rule Each M-N relationship
becomes a separate table. The PK is the combined
key of the entities participating in the M-N
relationship - Identification Dependency Rule Each identifying
relationship adds a component to the PK of the
weak entity.
6Converting ERD to Tables
- Optional 1-M Relationship Rule Each 1-M
relationship with min cardinality of 0 on the
parent side becomes a new table. The table has
FKs from participating entities with the child
entity PK becoming the PK. - Generalization Hierarchy Rule Each entity in
hierarchy becomes a table. Subtypes inherit PK
from parent. Use CASCADE option for deletions of
referenced rows. - 1-1 Relationship Rule Each 1-1 relationship is
converted into two foreign keys. If the
relationship is optional with respect to one
side, that FK may be dropped.
7From ERD to SQL
- Create Table Pet (
- PID Number NOT NULL Auto_Increment,
- Name VarChar(50) NOT NULL,
- Breed VarChar(50),
- Gender ENUM (M,F),
- Temper ENUM
- (Aggressive,Timid,Semi-Aggressive),
- OID Number,
- Constraint PK_Pet Primary Key (PID),
- Constraint FK_PetToOwner Foreign Key (OID)
References Owner(OID) )
8SQL Statements
9SELECT Statement Overview
- SELECT ltlist of column expressionsgt
- FROM ltlist of tables and join operationsgt
- WHERE ltlist of logical expressions for rowsgt
- GROUP BY ltlist of grouping columnsgt
- HAVING ltlist of logical expressions for groupsgt
- ORDER BY ltlist of sorting specificationsgt
- Expression combination of columns, constants,
operators, and functions
10Complex Select Example from Pet Table
- Select P.Breed, Count(P.PID)
- From Pet P
- Where Gender M AND TemperTimid
- Group By P.Breed
- Having Count(P.PID) gt 5
- Order By P.Breed DESC
11University Database
12First SELECT Examples
- SELECT
- FROM Faculty
- SELECT
- FROM Faculty
- WHERE FacSSN '543210987'
- SELECT FacFirstName, FacLastName, FacSalary
- FROM Faculty
- SELECT FacFirstName, FacLastName, FacSalary
- FROM Faculty
- WHERE FacSalary gt 65000
- AND FacRank 'PROF'
13Using Expressions
- SELECT FacFirstName, FacLastName, FacCity,
- FacSalary1.1 AS IncreasedSalary, FacHireDate
- FROM Faculty
- WHERE year(FacHireDate) gt 1991
- SELECT FacFirstName, FacLastName, FacCity,
- FacSalary1.1 AS IncreasedSalary, FacHireDate
- FROM Faculty
- WHERE
- to_number(to_char(FacHireDate, 'YYYY')) gt 1991
14Inexact Matching
- Match against a pattern
- LIKE operator
- Use meta characters to specify patterns
- Wildcard ( or )
- Any single character (? or _)
- Varies by database
- SELECT
- FROM Offering
- WHERE
- CourseNo LIKE 'IS'
- SELECT
- FROM Offering
- WHERE
- CourseNo LIKE 'IS'
15Using Dates
- Dates are numbers
- Date constants and functions are not standard
- Example 7 (Access)
- SELECT FacFirstName, FacLastName, FacHireDate
- FROM Faculty
- WHERE FacHireDate BETWEEN 1/1/1994
- AND 12/31/1995
- Example 7 (Oracle)
- SELECT FacFirstName, FacLastName, FacHireDate
- FROM Faculty
- WHERE FacHireDate BETWEEN '1-Jan-1994'
- AND '31-Dec-1995'
16Other Single Table Examples
Example 8 Testing for null values SELECT
OfferNo, CourseNo FROM Offering WHERE
FacSSN IS NULL AND OffTerm 'SUMMER' AND
OffYear 2003 Example 9 Mixing AND and OR
SELECT OfferNo, CourseNo, FacSSN FROM Offering
WHERE (OffTerm 'FALL' AND OffYear 2002)
OR (OffTerm 'WINTER' AND OffYear 2003)
17Join Operator
- Most databases have many tables
- Combine tables using the join operator
- Specify matching condition
- Can be any comparison but usually
- PK FK most common join condition
- Relationship diagram useful when combining tables
18Join Example
19Cross Product Style
- List tables in the FROM clause
-
- List join conditions in the WHERE clause
- SELECT OfferNo, CourseNo, FacFirstName,
- FacLastName
- FROM Offering, Faculty
- WHERE OffTerm 'FALL' AND OffYear 2002
- AND FacRank 'ASST' AND CourseNo LIKE 'IS'
- AND Faculty.FacSSN Offering.FacSSN
20Join Operator Style
- Use INNER JOIN and ON keywords
- FROM clause contains join operations
- SELECT OfferNo, CourseNo, FacFirstName,
- FacLastName
- FROM Offering INNER JOIN Faculty
- ON Faculty.FacSSN Offering.FacSSN
- WHERE OffTerm 'FALL' AND OffYear 2002
- AND FacRank 'ASST' AND CourseNo LIKE 'IS'
21Name Qualification
- Ambiguous column reference
- More than one table in the query contains a
column referenced in the query - Ambiguity determined by the query not the
database - Use column name alone if query is not ambiguous
- Qualify with table name if query is ambiguous
22Summarizing Tables
- Row summaries important for decision-making tasks
- Row summary
- Result contains statistical (aggregate) functions
- Conditions involve statistical functions
- SQL keywords
- Aggregate functions in the output list
- GROUP BY summary columns
- HAVING summary conditions
23GROUP BY Examples
- SELECT FacRank, AVG(FacSalary) AS AvgSalary
- FROM Faculty
- GROUP BY FacRank
- SELECT StdMajor, AVG(StdGPA) AS AvgGpa
- FROM Student
- WHERE StdClass IN ('JR', 'SR')
- GROUP BY StdMajor
- HAVING AVG(StdGPA) gt 3.1
24SQL Summarization Rules
- Columns in SELECT and GROUP BY
- SELECT non aggregate and aggregate columns
- GROUP BY list all non aggregate columns
- WHERE versus HAVING
- Row conditions in WHERE
- Group conditions in HAVING
25Summarization and Joins
- Powerful combination
- List join conditions in the WHERE clause
- List the number of students enrolled in each fall
2003 - offering.
- SELECT Offering.OfferNo,
- COUNT() AS NumStudents
- FROM Enrollment, Offering
- WHERE Offering.OfferNo Enrollment.OfferNo
- AND OffYear 2003
- GROUP BY Offering.OfferNo