Lecture 5-7 Basic SQL Annotated
Lecture 5-7 Basic SQL Annotated
2
SQL Data definition and Data types
• CREATE SCHEMA
• Specifies a new database schema by giving it a name
CREATE SCHEMA COMPANY AUTHORIZATION ‘Jsmith’
3
CREATE TABLE
• Specifies a new base relation (or base table) by giving it a name,
and specifying each of its attributes and their data types (INTEGER,
FLOAT, DECIMAL(i,j), CHAR(n), VARCHAR(n)).
Slide 8-4
CREATE TABLE
• CREATE TABLE command for specifying the primary key
attributes, secondary keys, and referential integrity
constraints (foreign keys).
• Key attributes can be specified via the PRIMARY KEY and
UNIQUE phrases
Slide 8-5
DROP TABLE
• Used to remove a relation (base table) and its definition
• The relation can no longer be used in queries, updates, or any
other commands since its description no longer exists
• Example:
Slide 8-6
ALTER TABLE
• Used to add an attribute to one of the base relations
• The new attribute will have NULLs in all the tuples of the
relation right after the command is executed; hence, the NOT
NULL constraint is not allowed for such an attribute
• Example:
• The database users must still enter a value for the new
attribute JOB for each EMPLOYEE tuple. This can be done
using the UPDATE command.
Slide 8-7
Specifying Constraints
• Attribute constraints and attribute defaults
– NOT NULL constraint
– to define a default value for an attribute, append the clause
DEFAULT <value> to an attribute definition
CREATE TABLE DEPT
( …
MGRSSN CHAR(9) NOT NULL DEFAULT ‘888665555’
…);
Slide 8-9
REFERENTIAL INTEGRITY OPTIONS
(continued)
Slide 8-10
Basic Retrieval Queries in SQL
• SQL has one basic statement for retrieving information from a
database; the SELECT statement
• This is not the same as the SELECT operation of the relational
algebra
• Important distinction between SQL and the formal relational
model; SQL allows a table (relation) to have two or more
tuples that are identical in all their attribute values
• Hence, an SQL relation (table) is a multi-set (sometimes
called a bag) of tuples; it is not a set of tuples
• SQL relations can be constrained to be sets by specifying
PRIMARY KEY or UNIQUE attributes, or by using the
DISTINCT option in a query
Slide 8-11
Retrieval Queries in SQL (cont.)
Slide 8-12
Relational Database Schema
Slide 8-13
Populated Database
Slide 8-14
BASIC SQL Queries (cont.)
• Basic SQL queries correspond to using the SELECT, PROJECT, and JOIN
operations of the relational algebra
• All subsequent examples use the COMPANY database
• Example of a simple query on one relation
• Query 0: Retrieve the birthdate and address of the employee whose name
is 'John B. Smith'.
Slide 8-15
Basic SQL Queries (cont.)
• Query 1: Retrieve the name and address of all employees who work for
the 'Research' department.
Slide 8-16
Simple SQL Queries (cont.)
• Query 2: For every project located in 'Stafford', list the project number, the
controlling department number, and the department manager's last name,
address, and birthdate.
• EMPLOYEE.LNAME, DEPARTMENT.DNAME
Slide 8-18
ALIASES
• Some queries need to refer to the same relation twice
• In this case, aliases are given to the relation name
• Query 8: For each employee, retrieve the employee's name, and the
name of his or her immediate supervisor.
Slide 8-19
ALIASES (cont.)
Slide 8-20
UNSPECIFIED WHEREclause
• A missing WHERE-clause indicates no condition; hence, all
tuples of the relations in the FROMclause are selected
• This is equivalent to the condition WHERE TRUE
• Query 9: Retrieve the SSN values for all employees.
Slide 8-21
UNSPECIFIED WHEREclause (cont.)
• Example:
Slide 8-22
USE OF *
Q1C: SELECT *
FROM EMPLOYEE
WHERE DNO=5
Q1D: SELECT *
FROM EMPLOYEE, DEPARTMENT
WHERE DNAME='Research' AND
DNO=DNUMBER
Slide 8-23
USE OF DISTINCT
• SQL does not treat a relation as a set; duplicate tuples can
appear
• To eliminate duplicate tuples in a query result, the keyword
DISTINCT is used
• For example, the result of Q11 may have duplicate SALARY
values whereas Q11A does not have any duplicate values
Slide 8-25
SET OPERATIONS (cont.)
• Query 4: Make a list of all project numbers for projects that involve an
employee whose last name is 'Smith' as a worker or as a manager of the
department that controls the project.
Slide 8-26
SUBSTRING COMPARISON
• The LIKE comparison operator is used to compare
partial strings
• Two reserved characters are used: '%' (or '*' in some
implementations) replaces an arbitrary number of
characters, and '_' replaces a single arbitrary
character
Slide 8-27
SUBSTRING COMPARISON (cont.)
Slide 8-28
SUBSTRING COMPARISON (cont.)
• Query 26: Retrieve all employees who were born during the
1950s.
•
Q26: SELECT FNAME, LNAME
FROM EMPLOYEE
WHERE BDATE LIKE ‘__5_____’
• The LIKE operator allows us to get around the fact that each
value is considered atomic and indivisible; hence, in SQL,
character string attribute values are not atomic
Slide 8-29
ARITHMETIC OPERATIONS
• The standard arithmetic operators '+', ''. '*', and '/' (for
addition, subtraction, multiplication, and division, respectively)
can be applied to numeric values in an SQL query result
• Query 27: Show the effect of giving all employees who work on
the 'ProductX' project a 10% raise.
Slide 8-30
ORDER BY
Slide 8-31
ORDER BY (cont.)
Slide 8-32
Specifying Updates in SQL
• There are three SQL commands to modify the
database; INSERT, DELETE, and UPDATE
Slide 8-33
INSERT
• In its simplest form, it is used to add one or more
tuples to a relation
• Attribute values should be listed in the same order as
the attributes were specified in the CREATE TABLE
command
Slide 8-34
INSERT (cont.)
• Example:
Slide 8-35
INSERT (cont.)
• Important Note: Only the constraints specified in the
DDL commands are automatically enforced by the
DBMS when updates are applied to the database
Slide 8-36
INSERT (cont.)
– Example: Suppose we want to create a temporary table that has the employee
last name, project name, and hours per week for each employee working on a
project.
Slide 8-37
INSERT (cont.)
• Note: The DEPTS_INFO table may not be uptodate if we
change the tuples in either the DEPARTMENT or the
EMPLOYEE relations after issuing U3B. We have to create a
view (see later) to keep such a table up to date.
Slide 8-38
DELETE
• Removes tuples from a relation
• Includes a WHEREclause to select the tuples to be deleted
• Tuples are deleted from only one table at a time (unless
CASCADE is specified on a referential integrity constraint)
• A missing WHEREclause specifies that all tuples in the
relation are to be deleted; the table then becomes an empty
table
• The number of tuples deleted depends on the number of
tuples in the relation that satisfy the WHEREclause
• Referential integrity should be enforced
Slide 8-39
DELETE (cont.)
• Examples:
U4A: DELETE FROM EMPLOYEE
WHERE LNAME='Brown’
Slide 8-40
UPDATE
• Used to modify attribute values of one or more
selected tuples
• A WHEREclause selects the tuples to be modified
• An additional SETclause specifies the attributes to
be modified and their new values
• Each command modifies tuples in the same relation
• Referential integrity should be enforced
Slide 8-41
UPDATE (cont.)
Slide 8-42
UPDATE (cont.)
Slide 8-43
Summary
• We discussed SQL database language. This language and its
variations have been implanted as interfaces to many
commercial relational DBMSs.
• The following features of SQL are discussed:
– Data definition commands for creating tables
– SQL basic data types
– Commands for constraint specification
– Simple retrieval queries and database update commands
44