Still More Operators: Outer Join outer join is an extension of the join operation to deal with missing information –three types: left outer join, right.

Slides:



Advertisements
Similar presentations
MSc IT UFCE8K-15-M Data Management Prakash Chatterjee Room 2Q18
Advertisements

TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
The Relational Model Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
Relational Database Design UNIT II 1. 2 Advantages of Using Database Systems Centralized control of a firm’s data Redundancy can be reduced (avoid keeping.
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
Review Session ER and Relational –ER  Relational –Constraints, Weak Entities, Aggregation, ISA Relational Algebra  Relational Calculus –Selections/Projections/Joins/Division.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity (and Security) Domain Constraints Referential Integrity Assertions Triggers.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 Lecture 11: Basic SQL, Integrity constraints
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL Constraints and Triggers
Cs3431 Constraints Sections 6.1 – 6.5. cs3431 Example CREATE TABLE Student ( sNum int, sName varchar (20), prof int, CONSTRAINT pk PRIMARY KEY (snum),
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
SQL Basic format of the select command select[distinct] target_list fromtuple_variable_list wherequalification [order by target_list_subset]; Simple query.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
10/3/2000SIMS 257: Database Management -- Ray Larson Relational Algebra and Calculus University of California, Berkeley School of Information Management.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 157 Database Systems I SQL Constraints and Triggers.
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
CONSTRAINTS AND UPDATES CHAPTER 3 (6/E) CHAPTER 5 (5/E) 1.
Chapter 6: Integrity Objective Key Constraints (Chapter 2) Cardinality Constraints (Chapter 2) Domain Constraints Referential Integrity Assertions Triggers.
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Chapter 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
Department of Computer Science and Engineering, HKUST Slide 1 Comp 231 Database Management Systems Comp 231 Database Management Systems 6. Integrity Constraints.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
CS 370 Database Systems Lecture 12 Introduction to SQL.
Lecture 7 Integrity & Veracity UFCE8K-15-M: Data Management.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
ICS 321 Fall 2009 SQL: Queries, Constraints, Triggers Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 9/8/20091Lipyeow.
Triggers. Why Triggers ? Suppose a warehouse wishes to maintain a minimum inventory of each item. Number of items kept in items table Items(name, number,...)
SCUHolliday - coen 1788–1 Schedule Today u Modifications, Schemas, Views. u Read Sections (except and 6.6.6) Next u Constraints. u Read.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
CMPT 258 Database Systems SQL Queries (Chapter 5).
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL: The Query Language Part 1 R &G - Chapter 5 The important thing is not to stop questioning. Albert Einstein.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity Constraints Domain Constraints Referential Integrity Assertions Triggers.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Chapter 6: Integrity (and Security)
SQL – Part 2.
Integrity Constraints
CS 405G: Introduction to Database Systems
SQL: The Query Language Part 1
SQL-1 Week 8-9.
Presentation transcript:

Still More Operators: Outer Join outer join is an extension of the join operation to deal with missing information –three types: left outer join, right outer join, and full outer join –left outer join computes the natural join, then takes all tuples (rows) in the left relation that did not match on the join attribute and includes them in the result, with all attributes of the right relation padded with null values –right outer join is the same, except non-matching tuples in the right relation are included in the result padded with null values –full outer join includes all non-matching tuples of both relations appropriately padded –see examples in text, p

Still More Operators division –R/S: given R(A,B) and S(B), then a given tuple t is in R/S if for all s in S there exists an r in R such that r.B=s.B and t.A=r.A. –So tuple t with attribute t.A is in the result if and only if R contained tuples (t.A, B 1 ), (t.A, B 2 ), (t.A, B 3 ), … for every possible value B i contained in S. –Note that S must be defined on a subset of the attributes of R for the operation to be meaningful.

A Short Interlude: Integrity the preceding slides covered chapter three up to section 3.3 before attacking chapter 4 (SQL), we’re going to make a brief excursion up to chapter 6, touching sections Integrity constraints attempt to enforce data consistency and prevent accidental damage to the database during updates We’ve already seen two forms of integrity constraints: –key declarations (stipulating that certain attributes form a candidate key for a given entity set) –mapping form of a relationship (one-one, one-many, many-many)

Integrity Constraints Domain Constraints –simplest form of integrity constraint –type declarations are one such domain constraint (e.g., integer, floating point, double-precision, fixed length character string). –domains can be further restricted (e.g., check clause in SQL can ensure that hourly wages are  4.00 dollars) –easily tested whenever a new data item is entered into the database –extensions like date or currency can be easily supported on a strongly typed programming language –Null values can be useful for values to be filled in later, but some attributes may need to be specified as “not Null” (e.g., primary keys cannot have a null value)

Integrity Constraints (2) Key Constraints –keys must have unique values –primary key -- a candidate key declared primary –unique key -- a candidate key –foreign key -- a set of attributes that are a primary key for some other relations foreign keys are an important concept because we need to treat foreign keys differently from other attributes (for example, protecting their uniqueness and insuring referential integrity) even though they aren’t a primary key in the current relation

Referential Integrity –We often want to be able to ensure that an attribute value in a tuple of a relation appears in at least one tuple of another relation. For example: »EMP(eno, ename, salary) »DEPT(dno, dname, floor) »WORKS-IN(eno, dno, hours) –note that eno is a foreign key in WORKS-IN –We want the following to be true:  eno (WORKS-IN)   eno (EMP) (every eno is a real employee)  dno (WORKS-IN)   dno (DEPT) (every dno is a real department) –SQL allows the declaration of domain/key/referential integrity constraints with the clause check in its DDL

Referential Integrity: SQL DDL Example Create table customer (cust-namechar(20) not null, cust-streetchar(30), cust-citychar(30), primary key(cust-name)) Create table branch (branch-namechar(15) not null, branch-citychar(30), assetsnumber, primary key(branch-name), check(assets  0)) Create table account (account-nochar(10) not null, branch-namechar(15), balancenumber, primary key(account-no), foreign key (branch-name) references branch, check(balance  0)) Create table depositor (cust-namechar(20) not null, account-nochar(10) not null, primary key(cust-name), foreign key (cust-name) references customer, foreign key (account-no) references account)

Referential Integrity and Database Modifications Database modifications may violate referential integrity Insertion: inserting a value into the referencing relation that is not in the referenced relation Deletion: deleting the last example of a given value in the referenced relation and leaving that value in the referencing one –proper handling may lead to cascading deletions Update to the referencing relation (constraints as Insertion) Update to the referenced relation (constraints as Deletion)

Assertions An assertion is an arbitrary expression that the database must always satisfy –e.g., student GPA > 2.8, or sum(all-charges) < credit-line –Domain constraints and referential integrity constraints are special forms of assertion that are easy to test –SQL supports assertions as follows: create assertion check –When an assertion is made the system checks it for validity. If it is validated, every future modification of the database is checked against the assertion and allowed only if it is not violated. –This can be very expensive if assertions are complex or numerous

Triggers A trigger is a statement that the system executes automatically as a side effect of an update to the database. A trigger has two parts: –condition under which it is executed –actions to be taken if it is executed Example: instead of having an assertion “balance  0” for a checking account, use a trigger on negative balances that sets the balance to zero and creates a new loan for the amount of the overdraft Triggers make the system reactive Triggers are also called active rules Like Assertions, Triggers can be very expensive.

Trigger Example define trigger overdraft on update of account T (if new T.balance < 0 then (insert into borrow values (T.branch.name, T.account-number, T-customer-name, - new T.balance) update deposit S set S.balance = 0 where S.account-number = T.account-number)) (note: SQL syntax given here is slightly different from that in the text, p235)

SQL ( Structured Query Language) (Astrahan, Gray, Lindsay, Selinger, …) Most common and influential commercial query language; well established as the industry standard query language for relational databases Developed (as “Sequel”) at the IBM Research Lab in San Jose in the early 70s Four basic commands –select –insert –delete –update Result of each query is a relation

SQL Example selecte.name fromemp e wheree.age > 30; e is a tuple variable ranging over the emp relation a tuple variable followed by a “.” and an attribute is an indexed tuple variable and specifies the corresponding attribute of the tuple, very similarly to in many programming languages what follows the keyword select is the target list what follows from is the tuple variable list and consists of a list of relations and variable names what follows where is the qualification clause; an arbitrary boolean expression

SQL Basic format of the select command select[distinct] target_list fromtuple_variable_list wherequalification [order by target_list_subset]; Semantics –evaluate qualification: select the subset of the cartesian product of the ranges of the tuple variables that satisfy the qualification –evaluate target list: eliminate columns that are not in the target list –prepare the result as a relation with columns according to the target list –if distinct is used, eliminate duplicate tuples –if order by is used, sort the result accordingly

SQL: some example queries We will give a number of simple query examples using the following relational schema: sailors(sid, sname, rating) boats(bid, bname, colour) reserve(sid, bid, date) (1) Find the names of sailors who have reserved boat #2 select s.sname from sailors s, reserve r where s.sid=r.sid and r.bid=2

SQL: example queries (2) (2) Find the names of sailors who have reserved a red boat select s.sname from sailors s, reserve r, boats b where s.sid=r.sid and r.bid=b.bid and b.colour=“red” (3) Find the colours of all boats reserved by Pat selectb.colour fromsailors s, reserve r, boats b wheres.sname=“Pat” and s.sid=r.sid and r.bid=b.bid

SQL: example queries (3) (4) Find the names of sailors who have reserved at least one boat select s.sname from sailors s, reserve r where s.sid=r.sid (5) Find the names of sailors who have reserved a red or a green boat selects.sname fromsailors s, reserve r, boats b wheres.sid=r.sid and r.bid=b.bid and (b.colour=“red” or b.colour=“green”)

SQL: example queries (4) (6) Find the names of sailors who have reserved a red and a green boat selects.sname fromsailors s, reserve r, boats b, reserve r2, boats b2 wheres.sid=r.sid and r.bid=b.bid and b.colour=“red” and s.sid=r2.sid and r2.bid=b2.bid and b2.colour=“green”) Note: in the above query if sailor Pat has reserved one green boat and two red ones, the name Pat will appear twice in the results. To avoid that, use the keyword distinct in the select line, as in: select distinct s.sname