sql chapter 8. sql or sequel - (structured english query language) based on relational algebra...
TRANSCRIPT
SQL Chapter 8
SQL or SEQUEL - (Structured English Query Language)• Based on relational algebra • Developed in 1970's released in early 1980's• Standardized - SQL-92 (SQL2), SQL-3 current
standard - SQL:1999 (SQL-99), future standard - SQL:2003 (aka SQL:200n)
• High-level DB language used in ORACLE, etc. created at IBM with System R
• SQL provides DDL and DML – DDL - create table, alter table, drop table– DML - Queries in SQL
SQL
• Relation not a set of tuples - a multiset or bag of tuples
• Therefore, 2 or more tuples may be identical • Basic building block of SQL is the Select
Statement
SELECT <attribute list>
FROM <table list >
WHERE <search conditions>
Select Statement
• Select - chooses columns (project operation in relational algebra)
• From - combines tables if > 1 table (join operation |X| in relational algebra)
• Where - chooses rows (select operation in relational algebra) Exs.– Result of a query is a relation– Results may contain duplicate tuples– Can rename a column name using: as
• E.g. select lname as last_name from employee
Queries
• Retrieve the birthdate and address of the employee whose name is 'Smith‘
Select bdate, address From Employee Where lname = 'Smith'• To retrieve all the attribute values of the selected
tuples, a * is used Select * From Employee Where lname = 'Smith'
Queries
• To select all rows and columns of a relation Select * From Employee• To select some of the rows Select * From Employee Where dno = 5• To select specified columns for all rows Select SSN From Employee
Select Clause
Select <attribute list>– Attribute list can be column names, constants,
arithmetic expressions involving columns, etc.– Use an asterisk to list all attributes in a table– To rename an attribute, use the keyword as
Select lname as last_name
From employee
– In Oracle, can also be a select statement
From clause
From <table list>
• Table list can be: – one or more table names– a select statement itself
• How is data combined between more than 1 table?
Combining tuples in where clause
• To retrieve data that is in more than one table can use:– a cartesian product X Fig7.12
Select SSN, dname From Employee, Department
– A join operation |X| Fig7.13• Retrieve the lname, address and department name of
each employee Select SSN, dname From Employee, Department Where dnumber = dno
Combining tuples in where clause
• A cartesian product combines each tuple in one table, with all the tuples in the second table
• A join combines a tuple from the first table with tuple(s) in the second table if the specified (join) condition is satisfied
Relational Algebra
• Select those who work for the research department?
• Similar to select-project-join sequence of relational algebra operations
dname = 'Research' is a selection condition
dnumber = dno is a join condition
Join Conditions
• For every project located in 'Stafford' list the project number, the controlling department number and department manager's last name, address and birthdate.
Select pnumber, dnum, lname, bdate, address From Project, Department, Employee Where dnum = dnumber and mgrssn = ssn and plocation = 'Stafford'
• There are 2 join conditions in the above query
Additional characteristics
• In SQL we can use the same name for 2 or more attributes in different relations. Must qualify the attributes names:
employee.lname
• Use distinct to eliminate duplicate tuples: Select distinct salary From Employee
Additional characteristics
• Aliases are used to rename relations:Select E.lname, D. dnameFrom Employee E, Department DWhere E.dno = D.dnumber
• List all employee names and their supervisor names
Select E.fname, E.lname, S.fname, S.lname From Employee E, Employee S Where E.superssn = S.ssn
Where clause
Where <search conditions>• Search conditions can be:
– Comparison predicate: expr § expr2
where § is <, >, <=, etc.
in, between, like, etc.
expr is constant, col, qual.col,
aexpr op aexpr, fn(aexpr), set_fn(aexpr)
expr2 is expr | select statement• Note: expr can be a select statement!
Expr as a select statement
• You need to be careful using this. Result must be a single value
Select lname, dno
From employee
Where dno = (select dnumber
from department
where dname = ‘Research’)
Using Predicates
• Using where condition - you can nest queries:– In predicate: expr [not] in (select | val {, val})– Quantified predicate: expr § [all | any] (select)– Exists predicate: [not] exists (select)Other where conditions:– Between predicate: expr [not] between expr2 and
expr3– Like predicate: col [not] like 'pattern'– Null predicate: col is [not] null
Predicates
• Predicates evaluate to either T or F. Many of the previous queries can be specified in an alternative form using nesting.
In predicate
• The in predicate tests set membership for a single value at a time.
• In predicate: expr [not] in (select | val {, val})
Select * From Agents
Where city in ('Atlanta', 'Dallas')
In predicate
Rewrite to select employees in research dept.
Select * From Employee Where dno in (Select dnumber From Department where dname = 'Research')
• The outer query selects an Employee tuple if its dno value is in the result of the nested query.
Quantified predicate
• Quantified predicate compares a single value with a set according to the predicate.
• Quantified predicate: expr § [all | any] (select)
Select *
From Employee
Where dno = any (Select Dnumber
From Department
Where dname = 'Research')
Quantified predicate
What does the following query?
Select * From Employee Where salary > all (Select salary From Employee Where sex = 'F')• = any equivalent to in
• not in equivalent to <> all
Exists predicate
• The exists predicate tests if a set of rows is non-empty
• Exists predicate: [not] exists (select) Select * From Employee Where exists (Select * From Department Where dname = 'Research' and dno =
dnumber)
Exists predicate
• Exists is used to check whether the result of the inner query is empty or not. If the result is NOT empty, then the tuple in the outer query is in the result.
• Exists is used to implement difference (‘not in’ used) and intersection.
Exists predicate
• Retrieve all the names of employees who have no dependents.
• Select fname, lname• From Employee• Where not exists (Select *
From Dependent Where ssn = essn)
• All of the Dependent tuples related to one Employee tuple are retrieved. If none exist (not exists is true and the inner query is empty) the Employee tuple is in the result.
Nested queries
• In general we can have several levels of nested queries.
• A reference to an unqualified attribute refers to the relation declared in the inner most nested query.
• An outer query cannot reference an attribute in an inner query (like scope rules in higher level languages).
• A reference to an attribute must be qualified if its name is ambiguous.
Will this work?
Suppose you want the ssn and dname:
Select ssn, dname
from employee
where dno in (select dnumber
from department)
Correlated Nested Queries
• Correlated Nested Queries:• If a condition in the where-clause of a nested
query references an attribute of a relation declared in an outer query, the two queries are said to be correlated.
• The result of a correlated nested query is different for each tuple (or combination of tuples) of the relation in the outer query.
• Which takes longer to execute? a correlated nested query or a non-correlated nested query?
Correlated queries
List the name of employees who have dependents with the same birthday as they do.
Select E.fname, E.lname From Employee E Where E.ssn in (Select essn From Dependent D Where essn = E.ssn and E.bdate = D.bdate)Can this be written as uncorrelated nested?
Single block queries
• An Expression written using = or IN may almost always be expressed as a single block query:
Select E.fname, E.lname From Employee E, Dependent D Where E.ssn = E.essn and
E.bdate = D.bdate
Select statement
• Multiple levels of select nesting are allowed.• Like predicate, Between predicate and Null
predicate• Can apply arithmetic operations to numeric
values in SQL Select fname, lname, 1.1*salary From Employee
Select discount_rate*price From products
Aggregate functions
• Aggregate Functions (set functions, aggregates):
• Include COUNT, SUM, MAX, MIN and AVG
aggr (col)• Find the maximum salary, the minimum salary
and the average salary among all employees.
Select MAX(salary), MIN(salary), AVG(salary) From Employee
Aggregates
• Retrieve the total number of employees in the company
Select COUNT(*) From Employee
• Retrieve the number of employees in the research department.
Select COUNT(*) From Employee, Department Where dno=dnumber and dname='Research'
Aggregates
• Note that:Select COUNT(*) from Employee
Will give you the same result as:Select COUNT(salary)from Employee
Unless there are nulls - not counted for salary
• To count the number of distinct salaries. Select COUNT(distinct salary) From Employee
What does this query do?
SELECT dno, lname, salary FROM employee x WHERE salary >
(SELECT AVG(salary) FROM employeeWHERE x.dno = dno)
What would happen if you delete the qualification “x.”?
Grouping
• We can apply the aggregate functions to subgroups of tuples in a relation.
• Each subgroup of tuples consists of the set of tuples that have the same value for the grouping attribute(s).
• The aggregate is applied to each subgroup independently.
• SQL has a group-by clause for specifying the grouping attributes.
Grouping
• For each department, retrieve the department number, the total number of employees and their average salary.
Select dno, COUNT(*), AVG(salary) From Employee Group By dno• The tuples are divided into groups with the same dno. • COUNT and AVG are then applied to each group.
Grouping
• For each project, retrieve the project number, project name and the number of employees who work on that project.
Select pnumber, pname, COUNT(*) From Project, Works_on Where pnumber=pno Group By pnumber, pname
• In the above query, the joining of the two relations is done first, then the grouping and aggregates are applied.
Oracle group by
• Expressions in the GROUP BY clause can contain any columns of the tables or views in the FROM clause, regardless of whether the columns appear in the SELECT clause.
• However, only grouping attribute(s) and aggregate functions can be listed in the SELECT clause.
Having Clause
• Sometimes we want to retrieve those tuples with certain values for the aggregates.
• The having clause is used to specify a selection condition on a group (rather than individual tuples).
• If a having is specified, you must specify a group by.
Having
• For each project on which more than two employees work, retrieve the project number, project name, and the number of employees who work on that project.
Select pnumber, pname, COUNT(*) From Project, Works_on Where pnumber =pno Group By pnumber, pname Having COUNT(*) > 2
To Access Oracle in EE116
• Log on to the machine, using your STUDENTS account – Login: bama account name – Password: your CWID
• Go to Start, Programs then choose Oracle-OraHome9, Application Development and SQL Plus.
• In SQL Plus – User Name: your CWID – Password: first letter of your last name in lower
case followed by your CWID – Host String: students
Using SQL Plus
• Type in SQL commands interactively• Each SQL statement must have a semicolon ;
at the end • Can also run commands from a file by specifying
@filename • Results of the queries are displayed on the
screen • To write the screen output to a file, select File,
Spool and specify a filename• Use Spool off to stop redirection to the file
Oracle: changes permanent
• inserting tuples into a table using SQL Plus, you must:– type in commit;
• Or
– specify quit;• To make changes permanent
Oracle SQL
• Oracle9i SQL reference can be found here
Subselect formal definition
• Select called Subselect Select expr {, expr}
From tablename [alias] {, tablename [alias]}
[Where search_condition]
[Group By col {, col}]
[Having search_condition]
Order By
• To sort the tuples in a query result based on the values of some attribute:
Order by col_list
• Default is ascending order (asc), but can specify descending order (desc)
Order by
• Retrieve a list of the employees each project (s)he works on, ordered by the employee's department, and within each department order the employees alphabetically by last name.
Select dname, lname, fname, pname From Department, Employee, Works_on, Project Where dnumber=dno and ssn=essn and
pno=pnumber Order By dname, lname
Set operations
• Also available are Set Operations, such as:
• UNION, MINUS and INTERSECT.
Subselect {Union [all] subselect} [Order By col [asc | desc] {, col [asc | desc]}]
Set Operations
• The resulting relations are sets of tuples; duplicate tuples are eliminated.
• Operations apply only to union compatible relations. The two relations must have the same number of attributes and the attributes must be of the same type.
Oracle example
SELECT * FROM
(SELECT ENAME FROM EMP
WHERE JOB = 'CLERK'
UNION
SELECT ENAME
FROM EMP
WHERE JOB = 'ANALYST');
Set operations
• List all project numbers for projects that have an employee whose last name is Smith as a worker or as a manager of the department that controls the project.
(Select pname From Project, Department, Employee Where dnum=dnumber and mgrssn=ssn and
lname='Smith') Union (Select pname From Project, Works_on, Employee Where pnumber=pno and essn=ssn and
lname='Smith')
Example queries
• SQL to list employee name and department name for employees with salary > $25,000.
• SQL to list department name for departments with average salary > $25,000.
• Can you write this query?:– SQL to list employee name and
department name for departments with average salary > $25,000.
Example Queries
• Suppose you have created a tableQtrSales (ID, Q1, Q2, Q3, Q4)
• SQL to compute the total sales for each quarter?
• SQL to compute the total sales for each ID?
Evaluation
• Logical order of evaluation:– Apply Cartesian product to tables, apply
search conditions, then group by and having. – Apply the select clause and order the result
for the display.
• Actual order of evaluation?– More efficient to apply join condition during
Cartesian product (called a join operation)– How can a DBMS implement a join?
DDL – Data Definition in SQL
• Used to CREATE, DROP and ALTER the descriptions of the relations of a database
• CREATE TABLE– Specifies a new base relation by giving it a
name, and specifying each of its attributes and their data types
CREATE TABLE name (col1 datatype, col2 datatype, ..)
• Data types: (ANSI SQL vs. Oracle)
There are differences between SQL and Oracle, but Oracle will convert the SQL types to its own internal types
– int, smallint, integer converted to NUMBER– Character is char(l) or varchar2(l), varchar(l) still works– Float and real converted to number
Constraints
• Constraints are used to specify primary keys, referential integrity constraints, etc.
CONSTRAINT constr_name PRIMARY KEY
CONSTRAINT constr_name REFERENCES
table (col)
• You can also specify NOT NULL for a column
Create table – In line constraint definition Create table Project2 (pname varchar2(9)
CONSTRAINT pk PRIMARY KEY,
pnumber int not null,
plocation varchar2(15),
dnum int CONSTRAINT fk
REFERENCES Department (dnumber),
phead int);
Create Table – out of line constraint definitionCreate table Project2 (pname varchar2(9),
pnumber int not null,
plocation varchar2(15),
dnum int, phead int,
CONSTRAINT pk PRIMAY KEY (pname),
CONSTRAINT fk FOREIGN KEY (dnum)
REFERENCES Department (dnumber));
Oracle Specifics
• When you specify a foreign key constraint out of line, you must specify the FOREIGN KEY keywords and one or more columns. When you specify a foreign key constraint inline, you need only the REFERENCES clause.
Create table
• To create a table with a composite primary key must us out of line definition:
Create table Works_on (essn char(9), pno
int, hours number(4,1),
CONSTRAINT pk2 PRIMARY KEY (essn, pno));
DROP TABLE
• Used to remove a relation and its definition
• The relation can no longer be used in queries, updates or any other commands since its description no longer exists
Drop table dependent;
ACM announces Movie Night
When and Where: Thursday, September 25, 20036:30 pm in room 119 EE
EVERYONE IS INVITED!!!!The Movie will be Office Space
We will have subway plattersNo Admission fee
If you have any questions, go to our newly updated website and post them on the forum: http://www.studentacm.org
ALTER TABLE
• To alter the definition of a table in the following ways:– to add a column– to add an integrity constraint– to redefine a column (datatype, size, default
value) – there are some limits to this– to enable, disable or drop an integrity
constraint or trigger– other changes relate to storage, etc.
Alter is useful when …
– You have two tables that reference each other– Table must be defined before referenced, so
how to define?:• department mgrssn references employee ssn with
mgrssn• Employee dno references department dnumber
– Create employee table without dno– Create department table with reference to
mgrssn– Alter employee and add dno column
Alter table - Oracle
• The table you modify must have been created by you, or you must have the ALTER privilege on the table.
• If used to add an attribute to one of the base relations, the new attribute will have NULLS in all the tuples of the relation after command is executed; hence, NOT NULL constraint is not allowed for such an attribute.
Alter table employee add job varchar(12);
• The database users must still enter a value for the new attribute job for each employee tuple using the update command. Oracle alter
Updates (DML)
• Insert, delete and update– INSERT
Insert into table_name ( [(col1 {, colj})] values (val1 {, valj}) | (col1 {, colj}) subselect )
– add a single tuple– attribute values must be in the same order
as the CREATE table
Insert
Insert into Employee values ('Richard', 'K', 'Marini', '654298343', '30-DEC-52', '98 Oak
Forest, Katy, TX', 'M', 37000, '987654321, 4);
• Use null for null values in ORACLE
Insert
• Alternative form - specify attributes and leave out the attributes that are null
Insert into Employee (fname, lname, ssn) values ('Richard', 'Marini', '654298343');
• Constraints specified in DDL are enforced when updates are applied.
Insert
• To insert multiple tuples from existing table:
Create table Depts_Info (dept_name varchar(10), no_of_emps int, total_sal
int);
Insert into Depts_Info Select dname, count(*), sum(salary)
From Department, Employee Where dnumber = dno Group By dname;
Delete
Delete from table_name [search_condition]
• If include a where clause to select, tuples are deleted from table one at a time
• The number of tuples deleted depends on the where clause
• If no where clause included all tuples are deleted - the table is empty
Delete
Delete From Employee Where lname = 'Brown‘;
Delete From Employee Where ssn = '123456789‘;
Delete from Employee Where dno in (Select dnumber From Department Where dname = 'Research');
Delete from Employee;
Update
• Modifies values of one or more tuples• Where clause used to select tuples• Set clause specified the attribute and value
(new)• Only modifies tuples in one relation at a time
Update <table name>
Set attribute = value {, attribute = value}
Where <search conditions>
Update
Update Project Set plocation = 'Bellaire', dnum = 5 Where pnumber = 10
Update Employee Set salary = salary * 1.1 Where dno in (Select dnumber From department Where dname = 'Research')
Integrity constraints in Oracle
• A NOT NULL constraint prohibits a database value from being null.
• A unique constraint - allows some values to be null. • A primary key constraint combines a NOT NULL
constraint and a unique constraint in a single declaration. • A foreign key constraint requires values in one table to
match values in another table. • A check constraint requires a value in the database to
comply with a specified condition. • A REF constraint lets you further describe the
relationship between a REF column and the object it references.
Violation of Integrity Constraints
• Insert, delete or update can violate a referential integrity constraint
• SQL allows qualified options to be specified for the foreign key:– Set null– Cascade– Set default
• On delete or On update (includes insert)
• Not all options available in Oracle– Set null and Cascade
Oracle
• The ON DELETE clause - If you omit this clause, then Oracle does not allow you to delete referenced key values in the parent table that have dependent rows in the child table.
• Specify CASCADE if you want Oracle to remove all tuples with dependent foreign key values.
• Specify SET NULL if you want Oracle to convert dependent foreign key values to NULL.
Relational Views
• A view - a virtual table that is derived from other tables (vs. base table)
• A view can be used to simplify frequent queries e.g. a join condition• A view does not necessarily exist in physical
form • There is no limit on querying a view (limits on an update to a view)• Views are useful for security and authorization
mechanisms
Create View
• View attribute names are inherited from other tables
• If aggregate functions or the result of arithmetic operations, they must be renamed
• Views can be defined using other views
Create view
CREATE VIEW view_name [(col1 {, col2})]
AS SELECT col1 {, col2}
FROM (table1| view1) {, table2 | view2}
WHERE search_condition
Multiple table views
• To create a view from multiple tables
Create View Works_on1
As Select fname, lname, pname, hours
From Employee, Project, Works_on
Where ssn = essn and pno = pnumber
Create View
Create a view to list for each department: dname, number of employees and total salary paid out
Create View Dept_Info (dept_name, no_of_emps, total_sal)
As Select dname, count(*), sum(salary)
From Department, Employee
Where dnumber = dno
Group By dname;
Views
• Queries on View - same as queries on base tables
• Retrieve the last and first names of all employees who work on ProjectX
Select lname, fname
From Works_on1
Where pname = ‘ProjectX’
• How to represent derived attributes?
Maintain views
• 2 strategies to maintain views:– View is stored as a temporary table for future
queries called view materialization• view is not realized at the time of the view
definition but when specify the query
– Another strategy is to modify the view query into a query on the underlying base table called query modification
• DBMS keeps views up to date – how?
Views
• A view is removed by using the DROP VIEW command.
Drop View Works_on1; Drop View Dept_info;
Updating views
• If specify an update to a view, it updates the corresponding tables.
Create View Emp
As Select fname, lname, ssn, dno
From Employee
Update Emp
Set dno = 1
Where lname = ‘English’
Updating views
• It may not make sense to update some views – why?
Update Dept_info
Set total_sal = 100000
Where dname = ‘Research’
• Cannot always guarantee that a view can be updated
Views
• When would it not make sense?
• General Rule: (true for ORACLE)– View with one defining table is updatable if
the view attributes contain a primary key– Views with more than one table using joins is
not updatable– View with aggregate functions are not
updatable
Evaluation
• Logical order of evaluation:– Apply Cartesian product to tables, apply search
conditions, then group by and having. – Apply the select clause and order the result for the
display.
Select pnumber, pname, COUNT(*) From Project, Works_on Where pnumber =pno Group By pnumber, pname Having COUNT(*) > 2
Order by pname
Order of evaluation
• Actual order of evaluation?– More efficient to apply join condition during
Cartesian product (called a join operation)
– How can a DBMS implement a join?