Click here to load reader

Module-04 STRUCTURED QUERY LANGUAGE 4 : Structured Query Language (SQL) Module-04 STRUCTURED QUERY LANGUAGE (SQL) 4.1 Motivation The knowledge SQL is essential for software developer

  • View
    216

  • Download
    0

Embed Size (px)

Text of Module-04 STRUCTURED QUERY LANGUAGE 4 : Structured Query Language (SQL) Module-04 STRUCTURED QUERY...

Module 4 : Structured Query Language (SQL)

Module-04

STRUCTURED QUERY LANGUAGE (SQL)

4.1 Motivation

The knowledge SQL is essential for software developer hence SQL is an important subject

in computer programming around the world.

4.2 Objective

DBMS is the concept for the management of database but SQL is a set of tool or software

For the implementation of all the concept. In SQL we will going to see how each and every

concept of DBMS is implemented with SQL.

4.3 Syllabus

Prerequisites: Syllabus: Duration: Self study:

Relational

model.

Overview of SQL

Data Definition Commands

Set operations

Aggregate function

Null values,

Data Manipulation commands,

Data Control commands

Views in SQL

Complex Retrieval Queries using Group By, Recursive Queries, nested

Queries ;

Referential integrity in SQL. Event

Condition Action (ECA) model

(Triggers) in SQL

Database Programming with JDBC,

Security and authorization in SQL

Functions and Procedures in SQL and

cursors.

10 hours

create the college

library database

using SQL

command then

and use all the

DML, DDL, and

DCL command.

4.4 Definition

Data definition: SQL lets a user define the structure and organization of the stored data

and relationships among the stored data items.

Module 4 : Structured Query Language (SQL)

Data retrieval: SQL allows a user or an application program to retrieve stored data from

the database and use it.

Data manipulation: SQL allows a user or an application program to update the database by

adding new data, removing old data, and modifying previously stored data.

Access control: SQL can be used to restrict a users ability to retrieve, add, and modify

data, protecting stored data against unauthorized access.

Data sharing: SQL is used to coordinate data sharing by concurrent users, ensuring that

they do not interface with one another.

Data integrity: SQL defines integrity constraints in the database, protecting in the database,

protecting it from corruption due to inconsistent updates or system failures.

4.5 Course Outcome and Learning Outcome

CO3:-

Student should be able to retrieve any type of information from a data base by formulating

complex queries in SQL.

CO4:-

Create and populate a RDBMS for a real life application, with constraints and keys, using

SQL.

LO:- Write queries in SQL to retrieve any type of information from a data base.

4.6 Key definition

SQL: SQL is a tool for organizing, managing, and retrieving data stored by a computer

database. SQL is a computer language that we use to interact with the database. In fact,

SQL works with one specific type of database, called a relational data base.

Relational Database: A relational database is a database that can be perceived as a set of

tables and manipulated in accordance with the relational model of data. It contains a set of

objects used to store, manage, and access data. Examples of such objects are tables, views,

indexes, functions, triggers, and packages

4.7 Theory

Commercial database systems require more user-friendly query languages. We will look at

SQL in detail. Although referred to as query languages, SQL contains facilities for

designing and modifying the database.

The relation schemes for the banking example are:

Branch-scheme = (bname, bcity, assets)

Module 4 : Structured Query Language (SQL)

Customer-scheme = (cname, street, ccity)

Depositor-scheme = (cname, account#)

Account-scheme = (bname, account#, balance)

Loan-scheme = (bname, loan#, amount)

Borrower-scheme = (cname, loan#)

Background

1. SQL has become the standard relational database language. It has several parts:

o Data definition language (DDL) - provides commands to

Define relation schemes.

Delete relations.

Create indices.

Modify schemes.

o Interactive data manipulation language (DML) - a query language based on

both relational algebra and tuple relational calculus, plus commands to insert,

delete and modify tuples.

o Embedded data manipulation language - for use within programming

languages like C, PL/1, Cobol, Pascal, etc.

o View Definition - commands for defining views

o Authorization - specifying access rights to relations and views.

o Integrity - a limited form of integrity checking.

o Transaction control - specifying beginning and end of transactions.

4.7.1 Basic Structure

1. Basic structure of an SQL expression consists of select, from and where clauses.

o select clause lists attributes to be copied - corresponds to relational algebra

project.

o from clause corresponds to Cartesian product - lists relations to be used.

o where clause corresponds to selection predicate in relational algebra.

2. Typical query has the form

Select A1,A2..An

from R1,R2Rn

where P

where each Ai represents an attribute, each Ri a relation, and P is a predicate.

Module 4 : Structured Query Language (SQL)

3. This is equivalent to the relational algebra expression

a1,a2..an( p(R1*R2**Rm))

o If the where clause is omitted, the predicate P is true.

o The list of attributes can be replaced with a * to select all.

o SQL forms the Cartesian product of the relations named, performs a selection

using the predicate, then projects the result onto the attributes named.

o The result of an SQL query is a relation.

o SQL may internally convert into more efficient expressions.

The select Clause

The where Clause

The from Clause

The Rename Operation

Tuple Variables

String Operations

Ordering the Display of Tuples

Duplicate Tuples

The select Clause

1. An example: Find the names of all branches in the account relation.

select bname

from account

2. distinct vs. all: elimination or not elimination of duplicates.

Find the names of all branches in the account relation.

select distinct bname

from account

By default, duplicates are not removed. We can state it explicitly using all.

select all bname

from account

3. select * means select all the attributes. Arithmetic operations can also be in the

selection list.

The where Clause

1. The predicates can be more complicated, and can involve

o Logical connectives and, or and not.

http://www.cs.sfu.ca/CC/354/zaiane/material/notes/Chapter4/node4.html#SECTION00421000000000000000http://www.cs.sfu.ca/CC/354/zaiane/material/notes/Chapter4/node5.html#SECTION00422000000000000000http://www.cs.sfu.ca/CC/354/zaiane/material/notes/Chapter4/node6.html#SECTION00423000000000000000http://www.cs.sfu.ca/CC/354/zaiane/material/notes/Chapter4/node7.html#SECTION00424000000000000000http://www.cs.sfu.ca/CC/354/zaiane/material/notes/Chapter4/node8.html#SECTION00425000000000000000http://www.cs.sfu.ca/CC/354/zaiane/material/notes/Chapter4/node9.html#SECTION00426000000000000000http://www.cs.sfu.ca/CC/354/zaiane/material/notes/Chapter4/node10.html#SECTION00427000000000000000http://www.cs.sfu.ca/CC/354/zaiane/material/notes/Chapter4/node11.html#SECTION00428000000000000000

Module 4 : Structured Query Language (SQL)

o Arithmetic expressions on constant or tuple values.

o The between operator for ranges of values.

Example: Find account number of accounts with balances between $90,000 and $100,000.

select account#

from account

where balance between 90000 and 100000

The from Clause

1. The from class by itself defines a Cartesian product of the relations in the clause.

2. SQL does not have a natural join equivalent. However, natural join can be expressed

in terms of a Cartesian product, selection, and projection.

3. For the relational algebra expression

ename,loan#(borrower1 loan)

we can write in SQL,

select distinct cname, borrower.loan#

from borrower, loan

where borrower.loan# = loan.loan#

4. More selections with join: ``Find the names and loan numbers of all customers who

have a loan at the SFU branch,'' we can write in SQL,

select distinct cname, borrower.loan#

from borrower, loan

where borrower.loan# = loan.loan#

and bname=``SFU''

The Rename Operation

1. Rename: a mechanism to rename both relations and attributes.

2. as-clause can appear in both the select and from clauses:

old-name as new-name.

Example.

select distinct cname, borrower.loan# as loan_id

from borrower, loan

where borrower.loan# = loan.loan#

and bname= ``SFU"

Tuple Variables

1. Tuple variables can be used in SQL, and are defined in the from clause:

Search related