Top Banner
Database Management System Lecture 10 Lecture 10 DDL and DDL and Tuple Tuple calculus calculus
16

L10_PPT_IVSem

Jul 07, 2018

Download

Documents

Rohit Tiwari
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 1/16

Database Management System

Lecture 10Lecture 10DDL andDDL and TupleTuple calculuscalculus

Page 2: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 2/16

Data Definition LanguageData Definition Language

• The schema for each relation, including

 Allows the specification of:

attribute types.• Integrity constraints

 • u or za on n orma on or eac re a on.

• Non-standard SQL extensions also allows ecification of 

 – The set of indices to be maintained foreach relations.

 – The physical storage structure of eachrelation on disk.

Slide No:L1-2

Page 3: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 3/16

Create Table ConstructCreate Table Construct

•table command:

create table r (A 1 D 1, A 2 D 2, ..., A n D n ,- 1 ,

...,(integrity-constraintk))

  –  r  s e name o e re a on

 – each A i  is an attribute name in the schemaof relation r 

 –  D i  is the data type of attribute A i Example:

 crea e a e ranc (branch_name  char(15),branch_city char(30),

Slide No:L1-3

asse s  n eger

Page 4: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 4/16

Domain Types in SQL Domain Types in SQL 

•   -.   ,specified length n.

• varchar(n). Variable length character strings, with- .

• int. Integer (a finite subset of the integers that ismachine-dependent).

 .   -of the integer domain type).

• numeric(p,d). Fixed point number, with user-specified,

point.

• real, double precision. Floating point and double-

  , -dependent precision.

• float(n). Floating point number, with user-specified

Slide No:L1-4

  .

• More are covered in Chapter 4.

Page 5: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 5/16

Integrity Constraints on TablesIntegrity Constraints on Tables

 • not nu

• primary key (A 1, ..., A n )

xamp e: ec are ranc _name as e pr mary ey or ranc

.create table branch

branch name char 15 , _ 

branch_city char(30) not null,

assets integer,

primary key (branch_name))

primary key declaration on an attribute automatically ensures

not null in SQL-92 onwards, needs to be explicitly stated in

-

Slide No:L1-5

Page 6: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 6/16

Basic Insertion and Deletion of TuplesBasic Insertion and Deletion of Tuples

• Newly created table is empty

• Add a new tuple to account 

insert into account values ('A-9732', 'Perryridge', 1200)

 – Insertion fails if any integrity constraint is

violated

• Delete all tuples from account 

delete from account 

Note: Will see later how to delete selected tuples

Slide No:L1-6

Page 7: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 7/16

Drop and Alter Table ConstructsDrop and Alter Table Constructs

• he drop table command deletes all informationabout the dropped relation from the database.

• The alter table command is used to add

alter table r add A D where A is the name of the attribute to be addedto relation r and D is the domain of A.

 – All tuples in the relation are assigned null asthe value for the new attribute.

• The alter table command can also be used to

drop attributes of a relation:

 a ter ta e r  rop Awhere A is the name of an attribute of relation r 

 

Slide No:L1-7

 –databases

Page 8: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 8/16

Basic Query StructureBasic Query Structure

• A t ical S L uer has the form:

select A 1, A 2, ..., A n 

 , , ..., m 

where P 

  –  A i represents an attr ute

 –  R i

represents a relation

 –    .

• This query is equivalent to the relational algebraexpression. ))((

21,,, 21   mP A A A  r r r 

n×××∏   K

K

  σ  

 

Slide No:L1-8

  .

Page 9: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 9/16

The select ClauseThe select Clause

• he select clause list the attributes desired in the resultof a query

 – corresponds to the projection operation of the

• Example: find the names of all branches in the loan relation:

 se ect ranc _name 

from loan 

• In the relational algebra, the query would be:

branch_name (loan )

• NOTE: SQL names are case insensitive (i.e., you may use- - .

 – E.g. Branch_Name   ≡  BRANCH_NAME   ≡  branch_name 

 – Some people use upper case wherever we use bold

Slide No:L1-9

ont.

Page 10: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 10/16

The select Clause (Cont.)The select Clause (Cont.)

 results.

• To force the elimination of duplicates, insert theeywor st nct a ter se ect.

• Find the names of all branches in the loan relations,and remove duplicates

select distinct branch_name 

from loan 

• The keyword all specifies that duplicates not beremoved.

select all branch_name 

from loan 

Slide No:L1-10

Page 11: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 11/16

The select Clause (Cont.)The select Clause (Cont.)

• An asterisk in the select clause denotes “allattributes”

select *from loan 

 expressions involving the operation, +, –, ∗, and

/, and operating on constants or attributes oftuples.

• E.g.:

 _ , _ ,amount ∗ 100from loan 

Slide No:L1-11

Page 12: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 12/16

The where ClauseThe where Clause

 result must satisfy

 – Corresponds to the selection predicate of the

relational algebra.• To find all loan number for loans made at the

erryr ge ranc w oan amoun s grea erthan $1200.

select loan_number from loan 

where branch_name = ' Perryridge' and

• Comparison results can be combined using thelogical connectives and, or, and not.

Slide No:L1-12

Page 13: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 13/16

The from ClauseThe from Clause

 query

 – Corresponds to the Cartesian product operation of

the relational algebra.• Find the Cartesian product borrower X loan 

 select ∗

from borrower, loan 

  ,having a loan at the Perryridge branch.

select customer_name, borrower.loan_number, amount 

  ,

where borrower.loan_number = loan.loan_number and

branch_name = 'Perryridge'

Slide No:L1-13

Page 14: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 14/16

The Rename OperationThe Rename Operation

• SQL allows renaming relations and attributesusing the as clause:

old-name as new-name • E.g. Find the name, loan number and loan

amoun o a cus omers; rename e co umn

name loan_number as loan_id.

select customer_name, borrower.loan_number as

loan_id, amount 

  ,

where borrower.loan_number = loan.loan_number 

Slide No:L1-14

Page 15: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 15/16

Tuple VariablesTuple Variables

• uple variables are defined in the from clause via theuse of the as clause.

• Find the customer names and their loan numbers and.

select customer_name, T.loan_number, S.amount from borrower as T, loan as S 

 

Find the names of all branches that have greater assets than

some branch located in Brooklyn.

w ere . oan_num er = . oan_num er 

select distinct T.branch_name 

from branch as T, branch as S 

where T.assets > S.assets and S.branch_city = ' Brooklyn'

eywor as s op ona an may e om eborrower as T ≡ borrower T 

  Some database such as Oracle require as to be omitted

Slide No:L1-15

Page 16: L10_PPT_IVSem

8/18/2019 L10_PPT_IVSem

http://slidepdf.com/reader/full/l10pptivsem 16/16

Example InstancesExample Instances   s i d b i d d a y

5 8 1 0 3 1 1 / 1 2 / 9 6

s id s n a m e ra tin g a g e

2 2 d u s tin 7 4 5 .0S1

• We will use these

instances of theSailors and Reserves

u e r .

5 8 ru s ty 1 0 3 5 .0

re at ons n ourexamples.

• If the key for the

s id s n a m e r a tin g a g e

2 8 y u p p y 9 3 5 .0

eserves re at oncontained only theattributes sid and

3 1 l u b b e r 8 5 5 .54 4 g u p p y 5 3 5 .0

5 8 r u s t 1 0 3 5 .0

,semantics differ?

Slide No:L2-1