E-R Diagram for a Banking Enterprise

Slides:



Advertisements
Similar presentations
Ver 1,12/09/2012Kode :CCs 111,Sistem basis DataFASILKOM Chapter 3: SQL Bambang Irawan Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan.
Advertisements

1 Today’s Class  Relational Model  SQL CS F212 Database Systems.
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Ordering the Display of Tuples List in alphabetic order the names of all customers having.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Outer Join Aggregate Functions.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL Structured Query Language Meizhen Huang. Content (4.1 – 4.4) Background Parts of SQL Basic Structure Set Operations Aggregate Functions.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL Database Management Systems I Alex Coman, Winter 2006.
Chapter 4: SQL Basic Structure Set Operations Aggregate Functions
SQL Neyha Amar CS 157A, Fall Inserting The insert statement is used to add a row of data into a table Strings should be enclosed in single quotes,
©Silberschatz, Korth and Sudarshan1.1Database System Concepts - 5 th Edition, May 23, 2005 Introduction to Database System Concepts.
Slides adapted from A. Silberschatz et al. Database System Concepts, 5th Ed. SQL - part 2 - Database Management Systems I Alex Coman, Winter 2006.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Aandachtspunten Deadlines – alles op tijd inleveren! n Lees goed wat van je verwacht wordt!
MySQL Tutorial (2) Introduction to Database. Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city)
SQL Lecture 3 SQL Lecture 3. 2 SQL SQL Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex.
CIS552SQL1 Data Definition Language Insertions Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations.
SQL I. SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language”
Chapter 3: SQL Data Definition Language Data Definition Language Basic Structure of SQL Basic Structure of SQL Set Operations Set Operations Aggregate.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 3: SQL.
-- Introduction to database principles Maoying Wu March 11, 2013 Chapter 3: Basic SQL.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
SQL. Basic Structure SQL is based on set and relational operations with certain modifications and enhancements A typical SQL query has the form: select.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: Introduction.
3.1 Chapter 3: SQL. 3.2 Chapter 3: SQL Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Null Values It is possible for tuples to have a null value for some of their attributes The.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
Basic Structure Given sets A 1, A 2,..., A n a relation r is a subset of A 1 x A 2 x... x A n Thus a relation is a set of n-tuples (a 1, a 2,..., a n )
Structured Query Language 2 Presented by: Annisa, M.Kom. Source: Database System Concepts 5 th edition.
Relational Model: Examples. Banking Example branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) account.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts (Schema) Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested.
SQL – Introduction  Standard DML/DDL for relational DB’s  DML = “Data Manipulation Language” (queries, updates)  DDL = “Data Definition Language” (create.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
©Silberschatz, Korth and Sudarshan2.1Database System Concepts - 5 th Edition, Oct 5, 2006 Outer Join n An extension of the join operation that avoids loss.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE)
Database System Concepts, 5 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Relational Model.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts, 5 th Edition, Oct 5, 2006 SQL Data Definition Basic Query Structure Set Operations Aggregate.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com ICOM 5016 – Introduction.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
MySQL Tutorial Introduction to Database. Introduction of MySQL  MySQL is an SQL (Structured Query Language) based relational database management system.
ICOM 5016 – Introduction to Database Systems Lecture 6 Dr. Manuel Rodriguez Department of Electrical and Computer Engineering University of Puerto Rico,
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Nested Subqueries in SQL By Kenneth Cheung CS 157A Section 2 Professor Lee.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU 3-1 Introduction to Database CHAPTER 3 SQL Data Definition Basic.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL Bambang.
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
Chapter 5: SQL. Data Definition Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Complex Queries Views Modification.
Database System Concepts ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 3: SQL.
SQL and Relational Algebra
Chapter 3: SQL.
Module 4: SQL.
Chapter 3: SQL.
Structured Query Language (SQL)
Chapter 3: SQL.
Chapter 3: SQL.
Presentation transcript:

E-R Diagram for a Banking Enterprise 1

Rel. Schema for a Banking Enterprise branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-only) account (account-number, branch-name, balance) loan (loan-number, branch-name, amount) depositor (customer-name, account-number) borrower (customer-name, loan-number)

The select Clause - distinct SQL allows duplicates in relations as well as in query results. To force the elimination of duplicates, insert the keyword distinct after select. 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 be removed. select all branch_name from loan; 4

loan_number (amount ≥ 900 AND amount ≤ 1000 (loan)) The where Clause SQL includes a between comparison operator E.g. Find the loan number of those loans with loan amounts between $900 and $1000 (that is, $900 and $1000) First in Relational Algebra loan_number (amount ≥ 900 AND amount ≤ 1000 (loan)) select loan_number from loan where amount between 900 and 1000; 7

The from Clause The from clause lists the relations involved in the query corresponds to the Cartesian product operation of the relational algebra. Find the Cartesian product borrower x loan select  from borrower, loan; Find the name, loan number of all customers having a loan at the Perryridge branch. (Natural Join) customer_name,loan_number (branch_name = ‘Perryridge’ (borrower loan)) select customer_name, borrower.loan_number from borrower, loan where borrower.loan_number = loan.loan_number and branch_name = ‘Perryridge’; 8

The Rename Operation The SQL allows renaming relations and attributes using the as clause: old_name as new_name Find the name and loan number of all customers; rename the column name loan_number as loan_id. select customer_name, borrower.loan_number as loan_id from borrower, loan where borrower.loan_number = loan.loan_number; Find the names of all branches that have greater assets than some branch located in Brooklyn. select distinct T.branch_name from branch T, branch S where T.assets > S.assets and S.branch_city = ‘Brooklyn’; 9

String Operations SQL includes a string-matching operator for comparisons on character strings. Patterns are described using two special characters: percent (%). The % character matches any substring. underscore (_). The _ character matches any character. Find the names of all customers whose street includes the substring “Main”. select customer_name from customer where customer_street like ‘%Main%’; Match the name “Main%” like ‘Main\%’ escape ‘\’ SQL supports a variety of string operations such as concatenation (using “||”) converting from upper to lower case (and vice versa) finding string length, extracting substrings, etc.

Ordering the Display of Tuples List in alphabetic order the names of all customers having a loan in Perryridge branch select distinct customer_name from borrower, loan where borrower loan_number = loan.loan_number and branch_name = ‘Perryridge’ order by customer_name; We may specify desc for descending order or asc for ascending order, for each attribute; ascending order is the default. E.g. order by customer_name desc

Set Operations The set operations union, intersect, and except operate on relations and correspond to the relational algebra operations  Each of the above operations automatically eliminates duplicates; to retain all duplicates use the corresponding multiset versions union all, intersect all and minus all. Suppose a tuple occurs m times in r and n times in s, then, it occurs: m + n times in r union all s min(m,n) times in r intersect all s max(0, m – n) times in r minus all s

Set Operations Find all customers who have a loan, an account, or both: (select customer_name from depositor) union (select customer_name from borrower); Find all customers who have both a loan and an account. (select customer_name from depositor) intersect (select customer_name from borrower); Find all customers who have an account but no loan. (select customer_name from depositor) minus (select customer_name from borrower);

Aggregate Functions These functions operate on the multiset of values of a column of a relation, and return a value avg: average value min: minimum value max: maximum value sum: sum of values count: number of values

Aggregate Functions - Group by Find the number of depositors in the bank. select count (distinct customer_name) from depositor; Find the number of depositors for each branch. select branch_name, count (distinct customer_name) from depositor, account where depositor.account_number = account.account_number group by branch_name; Note: Attributes in select clause outside of aggregate functions must appear in group by list

Aggregate Functions – Having Clause Find the average account balance at the Perryridge branch. select avg (balance) from account where branch_name = ‘Perryridge’; Find the names of all branches where the average account balance is more than $1,200. select branch_name, avg (balance) from account group by branch_name having avg (balance) > 1200; Note: predicates in the having clause are applied after the formation of groups whereas predicates in the where clause are applied before forming groups

Nested Subqueries SQL provides a mechanism for the nesting of subqueries. A subquery is a select-from-where expression that is nested within another query. A common use of subqueries is to perform tests for set membership, set comparisons, and set cardinality.

Example Query Find all customers who have both an account and a loan at the bank. select distinct customer_name from borrower where customer_name in (select customer_name from depositor); Find all customers who have a loan at the bank but do not have an account at the bank select distinct customer_name from borrower where customer_name not in (select customer_name from depositor);

Example Query Find all customers who have both an account and a loan at the Perryridge branch select distinct customer_name from borrower, loan where borrower.loan_number = loan.loan_number and branch_name = “Perryridge” and (branch_name, customer_name) in (select branch_name, customer_name from depositor, account where depositor.account_number = account.account_number); Note: Above query can be written in a much simpler manner. The formulation above is simply to illustrate SQL features.

Set Comparison Find all branches that have greater assets than some branch located in Brooklyn. select distinct T.branch_name from branch as T, branch S where T.assets > S.assets and S.branch_city = ‘Brooklyn’; Same query using > some clause select branch_name from branch where assets > some (select assets from branch where branch_city = ‘Brooklyn’);

Definition of Some Clause F <comp> some r t r s.t. (F <comp> t) Where <comp> can be:  5 6 (read: 5 < some tuple in the relation) (5< some ) = true (5< some ) = false 5 (5 = some ) = true 5 (5  some ) = true (since 0  5) 5 (= some)  in However, ( some)  not in

Definition of all Clause F <comp> all r t r (F <comp> t) 5 6 (5< all ) = false 6 (5< all ) = true 10 4 (5 = all ) = false 5 4 (5  all ) = true (since 5  4 and 5  6) 6 ( all)  not in However, (= all)  in

Example Query Find the names of all branches that have greater assets than all branches located in Brooklyn. select branch_name from branch where assets > all (select assets from branch where branch_city = ‘Brooklyn’);

Test for Empty Relations The exists construct returns the value true if the argument subquery is nonempty. exists r  r  Ø not exists r  r = Ø

Example Query Find all customers who have an account at all branches located in Brooklyn. select distinct S.customer_name from depositor S where not exists ( (select branch_name from branch where branch_city = ‘Brooklyn’) minus (select R.branch_name from depositor T, account R where T.account_number = R.account_number and S.customer_name = T.customer_name)); Note that X – Y = Ø  X Y Note: Cannot write this query using = all and its variants