Professional Documents
Culture Documents
Databasetechlecture 9
Databasetechlecture 9
Databasetechlecture 9
Outline
• Introduction to SQL
• Basic structure of SQL
commands
• Data Definition
• Data Manipulation
• Aggregation
Introduction to Introduction to
SQL SQL
• SQL is a transform-oriented • SQL does not contain flow control
language with two major components. commands.
• the DDL for defining the database • must be implemented using a
structure and programming or job-control
• the DML for retrieving and updating language,
data. • or interactively by the decisions of
the user.
Introduction to Introduction to
SQL SQL
• SQL is relatively easy to learn. • An ISO standard exists for SQL,
• SQL is a nonprocedural language, • making it both the formal and de
• you specify what information you facto standard language for
require, relational databases.
• rather than how to get it. • The most popular and widely
implemented is referred to as
SQL2 or SQL/92.
1
8/19/2014
TABLE DEFINITION-
TABLE DEFINITION- example
• The syntax for creating a table • CREATE TABLE Employee
consists of an ordered set of (
attributes and a (possibly empty) RegNo CHARACTER(6) PRIMARY
set of constraints. KEY,
• CREATE TABLE table_name FirstName CHARACTER(20)
(col_name data_type [NULL | NOT NULL,
NOT NULL] [,...]) Surname CHARACTER(20) NOT
NULL,
TABLE DEFINITION-
CONT DROP DEFINITION
Dept CHARACTER (15) REFERENCES • A Table can be deleted from a
Department(DeptName) ON DELETE
database using the command
SET NULL
ON UPDATE CASCADE,
“drop”.
Salary NUMERIC(9) DEFAULT 0, • The following syntax is used.
City CHARACTER(15), – DROP TABLE name [RESTRICT
UNIQUE (Surname,FirstName) | CASCADE ];
)
2
8/19/2014
DROP DEFINITION
CONT: ALTER DEFINITION
• With RESTRICT (default), Table • ALTER (alter domain ..., alter table
must be empty or operation fails. …)
• With CASCADE, SQL drops all • For example the commande:
dependent objects — and objects
ALTER TABLE Department
dependent on these objects
ADD COLUMN NoOfOffices
NUMERIC(4);
Data Manipulation
Data Manipulation
• A query in SQL can consist of up FROM table_name [alias] [, ...]
to six clauses, [WHERE condition]
• but only the first two are [GROUP BY column_list]
mandatory.
• SELECT [DISTINCT | ALL] [HAVING condition]
{* | [column_expression [AS [ORDER BY column_list]
new_name]] [,...] }
3
8/19/2014
4
8/19/2014
• Produce a list of monthly salaries for all • List all staff with a salary greater
employees, showing staff number, first than 10,000.
and last names, and salary details • SELECT staffNo, fName, lName,
position, salary
• SELECT Snumber, Fname, Lname,
FROM Staff
Salary / 12 AS MonthlySalary
WHERE salary > 10000;
FROM Employee;
5
8/19/2014
Alternative JOIN
Constructs
Simple join query
• Find the names of the employees and • SQL provides alternative ways to specify joins:
the cities in which they work. • FROM Employee E JOIN Department D ON
• SELECT Employee.FirstName, E.Dept = D.DeptName
Employee.Surname, Department.City • FROM Employee JOIN Department USING
DeptName
FROM Employee, Department • FROM Employee NATURAL JOIN Department
WHERE Employee.Dept = • In each case, FROM replaces original FROM and
Department.DeptName; WHERE. However, first produces table with two
identical DeptName columns
6
8/19/2014
• To include unmatched rows in result • List branches and properties that are
table, use an Outer join. in
same city along with any unmatched
branches.
• SELECT b.*, p.*
FROM Branch1 b LEFT JOIN
PropertyForRent1 p ON b.bCity =
p.pCity;
Aggregation
Aggregation
• ISO standard defines five – AVG returns average of values
aggregate functions. These are: in a specified column.
– COUNT returns number of – MIN returns smallest value in a
values in a specified column. specified column.
– SUM returns sum of values in a – MAX returns largest value in a
specified column. specified column.
7
8/19/2014
8
8/19/2014
9
8/19/2014
Questions
10