CH 5. Structured Query Language (SQL) PDF
CH 5. Structured Query Language (SQL) PDF
DATABASE SYSTEMS
PRACTICAL SESSION
Database Systems 1
Introduction to Database Systems
What Is a Database System?
2
…Introduction to Database Systems
DBMS?
DBMS is then a general-purpose software
that facilities the processes of
Defining
Constructing
Manipulating, and
Sharing database.
3
STRUCTURED
QUERY LANGUAGE
Database Systems
General objectives
After completion of the practical sessions , you
will be able to design and implement a simple
database systems using Ms spl server 2005
INTRODUCTION
Structured Query Language (SQL) is a query
language that is standardized for RDBMS.
SQL Statements (commonly referred to as
'queries') are run to retrieve or modify the
requested information from the database
SQL supports:
Data Definition Language (DDL), and
Data Manipulation Language (DML)
6
SQL DATA DEFINITION LANGUAGE (DDL)
Partof the SQL language that permits
database tables and constraints to be
created, modified or deleted.
CREATE TABLE - creates a new database table.
ALTER TABLE - alters (changes) a database table.
DROP TABLE - deletes a database table.
CREATE INDEX - creates an index (search key).
DROP INDEX - deletes an index.
The
four most common commands are also
known as SQL CRUD statements.
Create, Read, Update, Delete 8
SCHEMA
DEFINITION IN SQL
SCHEMA DEFINITION IN SQL
Terminologies
Table – Relation
Column – Attribute
Row – Tuple
10
SCHEMA CREATION AND MODIFICATION
SQL statement that is used to create a new
database and the corresponding files for
storing the database:
CREATE DATABASE <database_name>
12
EXAMPLE
Creating the database
CREATE DATABASE [HHOffFurn]
14
… TABLE CREATION AND MODIFICATION
The ALTER TABLE command allows
modification (adding, changing, or dropping) of a
column or constraint in a table.
15
… EXAMPLE
Adding constraints
Key Constraint
ALTER TABLE [Emplyees] WITH NOCHECK ADD
CONSTRAINT [PK_Emplyees] PRIMARY KEY
CLUSTERED
(
[empId]
)
Unique Constraint
ALTER TABLE [Resources] ADD
CONSTRAINT [UQ_Resources_stockNo] UNIQUE
NONCLUSTERED
(
[stockNo], [regDate]
)
17
DROP COMMAND
The DROP command is used to drop an exiting
table, database or schema. The syntax for the
command is:
DROP TABLE [table name]
DROP DATABASE [database name]
DROP SCHEMA [schema name]
18
INDEX CREATION AND MODIFICATION
Indexes are the heart of fast data access that
provides fast data access.
An index for a table is managed by an external
table which consists of the search key (index
attribute) and a pointer to the location of the
data as columns.
Syntax
CREATE [CLUSTERED | NONCLUSTERED] INDEX
<index_name>
ON {<table> | <view> } ( <column> [ ASC | DESC ] [ ,...n ] )
20
SIMPLE QUERY
CONSTRUCTS AND
SYNTAX
SELECT-FROM-WHERE STATEMENT
The SELECT-FROM-WHERE statement is data
reading query
Simplest Syntax
SELECT <column_list>
FROM <table_list>
WHERE <condition>
Select
All Data
SELECT [empId], [name], [sex], [bDate], [address],
[empDate], [position], [salary]
FROM [Emplyees]
{SELECT * FROM [Emplyees]}
Single Data
SELECT [empId], [name], [sex], [bDate], [address],
[empDate], [position], [salary]
FROM [Emplyees]
WHERE [empId] = 1 23
INSERT, UPDATE AND DELETE
24
INSERT, UPDATE AND DELETE
Update
UPDATE [Emplyees] SET [name]=‘BBB', [empDate]='1-1-
2001'
WHERE [empID] = 1
Delete
DELETE FROM [Emplyees]
WHERE [empID] = 1 26
NESTED SUBQUERIES
AND COMPLEX
QUERIES
SUBQUERIES
Subqueries can be used in different ways:
In the WHERE clause to form nested
queries,
In set operations such as UNION, EXCEPT,
…, and
In the FROM clause as constant tables
28
NESTED QUERIES
SQL SELECT statements can be contained in
the WHERE clause of another SQL
statement to form Nested queries.
The SELECT statement that contains the
nested query is said to be the outer query.
Subqueries in a nested SQL statement can
produce scalar value (constant) or table.
TEST Operators in subquery:
IN
ALL
ANY
EXISTS 29
SET OPERATION OF QUERIES
The set operations union, intersection and
difference (UNION, INTERSECT and
EXCEPT) can be used in SQL statements
that will consist of two subqueries as the
operations are binary.
Syntax
(Subquery 1)
UNION | INTERSECT | EXCEPT |
(Subquery 2)
The set operators do not include duplicate in
the resulting table. If all the resulting rows
are to be retrieved the ALL operator is used
with the set operators to prevent duplicate 30
elimination.
JOINED TABLES
Reading data from more than one table with the
use of joined tables in the FROM clause.
Cross Product
<left_table> CROSS JOIN <right_table>
Natural Join
<left_table> NATURAL JOIN <right_table>
Theta Join
<left_table> JOIN <right_table> ON<condition>
Outer Join
<left_table> [RIGHT | LEFT | FULL] OUTER
{NATURAL} JOIN <right_table> {ON<condition>}
31
ORDERING QUERY RESULTS
To specify the order in which the resulting table
is organized, the ORDER BY clause is used in the
statement.
Syntax:
SELECT <column_list>
FROM <table_list>
WHERE <condition>
ORDER BY {<order_column> [ASC | DESC]}[,..n]
32
AGGREGATE FUNCTION IN SQL
SQL allows grouping of resulting rows in a
query so that aggregate functions can be
applied to make analysis and summary.
The aggregate functions supported by the
SQL statement are:
Summation
SUM (<column_name>)
Average
AVG (<column_name>)
Minimum
MIN (<column_name>)
Maximum
MAX (<column_name>)
Count
COUNT (<column_name> | *) 33
GROUPING IN SQL STATEMENT
The GROUP BY clause is used after the WHERE
clause to group the result of the SQL statement.
Syntax:
GROUP BY <group_column>[,..n]
HAVING <condition>
34
SUMMARY OF SQL STATEMENT
In
summary the general form the
SELECT statement is as follows
SELECT select_list
FROM table_source
[WHERE search_condition]
[GROUP BY group_by_expression]
[HAVING search_condition]
[ORDER BY order_expression [ASC | DESC]
]
35
VIEWS
View in the context of SQL is a virtual
table that is derived from one or more
tables in an alternative way.
Syntax
CREATE VIEW <view_name>
AS <select_statement>
Practice Group
ANNOUNCEMENT
Group 1 (section 1)
Tuesday December 17, 2007
Afternoon [2:00-11:00 PM]
Group 2 (section 2)
Wednesday December 18, 2007
Morning [9:00AM-12:00 PM]
39
Requirement Analysis
Detail problem description
ASSIGNMENT
Identification of
Entity set
Attribute
Relationship
It should be Formal
A4 paper size,
Readable handwriting,
…
40
41
ASSIGNMENT