SQL Simple SQL & Join: Resources: Mostly Froom Elmasri, Navathe and Other Books
SQL Simple SQL & Join: Resources: Mostly Froom Elmasri, Navathe and Other Books
SQL Simple SQL & Join: Resources: Mostly Froom Elmasri, Navathe and Other Books
History of SQL
CREATE TABLE
CREATE TABLE
REFERENTIAL INTEGRITY
OPTIONS
REFERENTIAL INTEGRITY
OPTIONS
CREATE TABLE EMP
( ENAME
VARCHAR(30) NOT NULL,
SSN CHAR(9),
BDATE
DATE,
DNO
INTEGER DEFAULT 1,
SUPERSSN
CHAR(9),
PRIMARY KEY (SSN),
FOREIGN KEY (DNO) REFERENCES DEPT
ON DELETE SET DEFAULT
ON UPDATE CASCADE,
FOREIGN KEY (SUPERSSN) REFERENCES EMP
ON DELETE SET NULL ON UPDATE CASCADE );
DROP TABLE
Example:
DROP TABLE DEPENDENT;
ALTER TABLE
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.
SIMPLE QUERIES
Queries in SQL
Queries in SQL
ORDER BY
The ORDER BY clause is used to sort the tuples in a query
result based on the values of some attribute(s)
Example: Retrieve a list of employees ordered by the
employee's department no, and within each department ordered
alphabetically by employee last name.
SELECT DNO, LNAME, FNAME
FROM
EMPLOYEE
ORDER BY DNO, LNAME
ORDER BY
The default order is in ascending order of values
We can specify the keyword
DESC if we want a descending order;
ASC can be used to explicitly specify ascending order, even
though it is the default
SELECT DNO, LNAME, FNAME
FROM
EMPLOYEE
ORDER BY DNO, LNAME DESC
OR
SELECT DNO, LNAME, FNAME
FROM
EMPLOYEE
ORDER BY 1, 2 DESC
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 first query may have duplicate
SALARY values whereas second one does not have any
duplicate values
SELECT SALARY
FROM
EMPLOYEE
SELECT DISTINCT SALARY
FROM
EMPLOYEE
COMPARISON
Example: Retrieve the birthdate and address of the employee
whose name is 'John B. Smith'.
COMPARISON OPERATORS
SELECT *
FROM EMPLOYEE
WHERE SALARY = 30000 --Equals
SALARY < > 30000 --Not equal (or !=)
SALARY > 30000 --Greater than
SALARY >=30000 --Greater than or equal to
SALARY < 30000 --Less than
SALARY < =30000 --Less than or equal to
AND and OR
Example: Retrieve the names of all employees who either work in department 4 or
female.
SELECT FNAME, LNAME, SEX, DNO
FROM EMPLOYEE
WHERE DNO=4 OR SEX=F
Example: Retrieve the names of all employees who either work in department 4 and
are female.
SELECT FNAME, LNAME, SEX, DNO
FROM EMPLOYEE
WHERE DNO=4 AND SEX=F
IN
Example: Retrieve the names of all employees who work in department 1 or 4.
SELECT FNAME, LNAME, DNO
FROM EMPLOYEE
WHERE DNO IN (1,4)
Equivalent to
SELECT FNAME, LNAME, DNO
FROM EMPLOYEE
WHERE DNO=1 OR DNO=4
BETWEEN
Example: Retrieve first name, last name and salary of employees whose
salary is between 25000 and 40000
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
SUBSTRING COMPARISON
Example: Retrieve all employees whose address is in Houston, Texas. Here,
the value of the ADDRESS attribute must contain the substring 'Houston,TX'.
SELECT
FROM
WHERE
FNAME, LNAME
EMPLOYEE
ADDRESS LIKE '%Houston, TX%
FNAME, LNAME
EMPLOYEE
ADDRESS NOT LIKE '%Houston, TX%'
SUBSTRING COMPARISON
Example: Retrieve all employees who have A as second
character in their name
SELECT FNAME, LNAME
FROM
EMPLOYEE
WHERE LNAME LIKE
_A%