0% found this document useful (0 votes)
115 views41 pages

CH 5. Structured Query Language (SQL) PDF

This document provides an introduction to database systems and SQL. It discusses what a database system is and the role of a DBMS. It also describes the SQL data definition language used to define and modify database schemas and tables, and the data manipulation language used to query and manipulate data. Specific SQL statements like CREATE TABLE, ALTER TABLE, SELECT, INSERT, UPDATE and DELETE are explained. Additional topics covered include indexes, nested queries, and joined tables.

Uploaded by

Bikila
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
115 views41 pages

CH 5. Structured Query Language (SQL) PDF

This document provides an introduction to database systems and SQL. It discusses what a database system is and the role of a DBMS. It also describes the SQL data definition language used to define and modify database schemas and tables, and the data manipulation language used to query and manipulate data. Specific SQL statements like CREATE TABLE, ALTER TABLE, SELECT, INSERT, UPDATE and DELETE are explained. Additional topics covered include indexes, nested queries, and joined tables.

Uploaded by

Bikila
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 41

INTRODUCTION TO

DATABASE SYSTEMS

PRACTICAL SESSION
Database Systems 1
Introduction to Database Systems
 What Is a Database System?

 It is collection of coherent (related) data.


 It is designed, built and populated to address
a specific situation in real world.

2
…Introduction to Database Systems
 DBMS?
 DBMS is then a general-purpose software
that facilities the processes of
 Defining

 Constructing

 Manipulating, and

 Sharing database.

Eg. Ms spl server ( in different versions ) , Ms access


,Oracle , etc…

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.

 DDL statements are used for schema


definition of a relational database.
7
SQL DATA MANIPULATION LANGUAGE (DML)
 It
is part of the SQL syntax for executing
queries to insert, retrieve, update, or
delete records.
 INSERT INTO - inserts new data into a database
table.
 SELECT - extracts data from a database table.
 UPDATE - updates data in a database table.
 DELETE - deletes data from a database table.

 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>

 Schema is a concept that is used to group


database objects such as tables, constraints,
domains, views and permissions in the same
application.
 The syntax for the command is:
CREATE SCHEMA <schema_name>
AUTHORIZATION <owner> 11
TABLE CREATION AND MODIFICATION
 The SQL statement that is used to specify a new
relation in a database:

CREATE TABLE <table_name> (


<column_name> <data_type>
{column_constraint},
:
<column_name> <data_type>
{column_constraint}
)

12
EXAMPLE
 Creating the database
CREATE DATABASE [HHOffFurn]

 Creating the tables


CREATE TABLE [Emplyees] (
[empId] smallint IDENTITY (1, 1) NOT NULL,
[name] varchar (30) NOT NULL,
[sex] char (1) NOT NULL,
[bDate] datetime NULL,
[address] varchar (50) NULL,
[empDate] datetime NULL,
[position] varchar (20) NULL,
[salary] float NULL
) 13
… TABLE CREATION AND MODIFICATION

 The primary key constraint in a relation is


enforced by using the key word PRIMARY KEY.

 The referential integrity constraint in a


relational database is implemented by the use of
FOREIGN KEY.
 Referential trigged actions:
 ON DELETE {CASCADE | NO ACTION | SET DEFAULT
| SET NULL}
 ON UPDATE {CASCADE | NO ACTION | SET DEFAULT
| SET NULL }

14
… TABLE CREATION AND MODIFICATION
 The ALTER TABLE command allows
modification (adding, changing, or dropping) of a
column or constraint in a table.

ALTER TABLE <table_name>


[ALTER COLUMN <column_name> <new_data_type>]
|
[ADD <column_definition> | <constraint>] |
[DROP <column_name> | < constraint>]

15
… EXAMPLE
 Adding constraints
 Key Constraint
ALTER TABLE [Emplyees] WITH NOCHECK ADD
CONSTRAINT [PK_Emplyees] PRIMARY KEY
CLUSTERED
(
[empId]
)

 Foreign Key Constraint (Relationship)


ALTER TABLE [Teams] ADD
CONSTRAINT [FK_Teams_Projects] FOREIGN KEY
(
[prjId]
) REFERENCES [Projects] (
[prjId]
) 16
… EXAMPLE
 Adding constraints
 Default Constraint
ALTER TABLE Emplyees] ADD
CONSTRAINT [DF_Emplyees_salary] DEFAULT
(
0
) FOR [salary]

 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]

 Example: deleting database


DROP DATABASE [HHOffFurn]

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 ] )

 For a clustered index the data is both stored and


sorted on the index key;
 For a nonclustered index the actual data is not stored
in the index. 19
… INDEX CREATION AND MODIFICATION
 Example:
CREATE NONCLUSTERED INDEX empName
ON Employees (name ASC )

 The DROP command is also used with indexes to


drop an existing database in a table.
 The syntax for the command is:
DROP INDEX <index_name> [,...n ]
 Example:
DROP INDEX empName

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>

 The DISTINCT key word on the SELECT clause:


SELECT DISTINCT <column_list>

 The LIKE key word for test of string pattern:


S LIKE P
S NOT LIKE P
22
… EXAMPLE

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

 The INSERT statement


 Syntax
INSERT INTO <table_name>| <view_name>
[(column_list)] data_values

 There are two ways to specify the data values:


 VALUES (<value_or_expression> [,..n])
 SELECT <subquery>

24
INSERT, UPDATE AND DELETE

 The UPDATE statement


 Syntax
UPDATE <table_name>
SET <column_name> = <value> [,..n]
WHERE <condtion>

 The DELETE statement


 Syntax
DELETE FROM <table_name>|
<view_name>
25
WHERE <condtion>
… EXAMPLE
 Insert
INSERT INTO [HHOffFurn].[dbo].[Emplyees]([name],
[sex], [bDate], [address], [salary])
VALUES(‘AAA’, ‘M’, ‘02-02-1978’, ‘ ’, 750)

 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>

 The columns that can be included in the


HAVING clause are:
 Aggregated column from the FROM clause tables, or
 Unaggregated columns from the GROUP BY list

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>

 Views as the database objects can also be


dropped using the DROP statement as
follows:
DROP VIEW <view_name> 36
EMBEDDED AND DYNAMIC SQL
 Embedded SQL is placed inside a host program,
not built on the fly.
 Static SQL is simply native SQL code that is
handled normally. It doesn't change at runtime
and it is a constant string literal.
 Dynamic SQL is made up on the fly by a
procedure or end user as a string containing SQL
statements that may change at runtime.
 For instance the where clause on a SQL statement
may be dependent on factors not known at compile
time.
 The ANSI Standards have PREPARE and EXECUTE
statements for this. 37
- END -
 Due Date for Assignment
Submission
 Thursday December 19, 2007
11:00AM

 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

 E/R Data Model


 Clean and Neat
 Step of deign

ASSIGNMENT
 Identification of
 Entity set

 Attribute

 Relationship

 It should be Formal
 A4 paper size,
 Readable handwriting,
 …
40
41
ASSIGNMENT

You might also like