Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Lecture 10 of 42 Tuesday, 06 February 2007.

Slides:



Advertisements
Similar presentations
Database System Concepts, 5th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 5: Other Relational.
Advertisements

Chapter 4: Immediate SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined.
E-R Diagram for a Banking Enterprise
SQL Sangeeta Devadiga CS157A, Fall Outline Background Data Definition Basic Structure Set Operation.
©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 Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all account records at the Perryridge branch.
1 CMSC424, Spring 2005 CMSC424: Database Design Lecture 7.
SPRING 2004CENG 3521 E-R Diagram for the Banking Enterprise.
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.
MySQL Tutorial (2) Introduction to Database. Banking Example branch (branch-name, branch-city, assets) customer (customer-name, customer-street, customer-city)
CIS552SQL1 Data Definition Language Insertions Basic Query Structure Set Operations Aggregate Functions Null Values Nested Subqueries Derived Relations.
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.
Chapter 4: Advanced SQL.
©Silberschatz, Korth and Sudarshan6.1Database System Concepts Chapter 6: Integrity and Security Domain Constraints Referential Integrity Assertions Triggers.
Chapter 4: Advanced SQL. 4.2Unite International CollegeDatabase Management Systems Chapter 4: Advanced SQL SQL Data Types and Schemas Integrity Constraints.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
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.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Languages Query-by-Example (QBE) Datalog.
Lecture 6 Structured Query Language SQL Lecture 6 Structured Query Language SQL Instructor: Haya Sammaneh.
3.1 Chapter 3: SQL Schema used in examples p (omit 3.8.2, , 3.11)
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.
-- Introduction to database principles Maoying Wu March 25, 2013 Chapter 4: Advanced SQL (1)
Advanced SQL Murat Kantarcioglu Adapted from Silberchatz et al. slides.
Computing & Information Sciences Kansas State University Monday, 15 Sep 2008CIS 560: Database System Concepts Lecture 8 of 42 Monday, 15 September 2008.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Modification of the Database – Deletion Delete all tuples from the loan relation. delete.
©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.
Computing & Information Sciences Kansas State University Mon, 11 Sep 2006CIS 560: Database System Concepts Lecture 8 of 42 Monday, 11 September 2006 William.
Computing & Information Sciences Kansas State University Friday, 08 Sep 2006CIS 560: Database System Concepts Lecture 7 of 42 Friday, 08 September 2006.
Computing & Information Sciences Kansas State University Monday, 04 Feb 2008CIS 560: Database System Concepts Lecture 6 of 42 Monday, 04 February 2008.
Chapter 3: SQL. 3.2Unite International CollegeDatabase Management Systems Chapter 3: SQL Data Definition Basic Query Structure Set Operations Aggregate.
Computing & Information Sciences Kansas State University Tuesday, 13 Feb 2007CIS 560: Database System Concepts Lecture 12 of 42 Tuesday, 13 February 2007.
Computing & Information Sciences Kansas State University Thursday, 08 Feb 2007CIS 560: Database System Concepts Lecture 11 of 42 Thursday, 08 February.
Computing & Information Sciences Kansas State University Monday, 08 Sep 2008CIS 560: Database System Concepts Lecture 5 of 42 Monday, 08 September 2008.
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING COLLEGE TIRUVANNAMALAI.
©Silberschatz, Korth and Sudarshan5.1Database System Concepts Chapter 5: Other Relational Query Languages Tuple Relational Calculus Domain Relational Calculus.
©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.
Chapter 8: SQL. Data Definition Modification of the Database Basic Query Structure Aggregate Functions.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Computing & Information Sciences Kansas State University Wednesday, 05 Sep 2007CIS 560: Database System Concepts Lecture 6 of 42 Wednesday, 05 September.
Computing & Information Sciences Kansas State University Monday, 17 Sep 2007CIS 560: Database System Concepts Lecture 12 of 42 Monday, 18 February 2008.
Source: Database System Concepts, Silberschatz etc Edited: Wei-Pang Yang, IM.NDHU, Introduction to Database CHAPTER 5 Other Relational Languages.
Chapter 4: SQL Complex Queries Complex Queries Views Views Modification of the Database Modification of the Database Joined Relations Joined Relations.
Database System Concepts, 5th Ed. Bin Mu at Tongji University Chapter 5: Other Relational Languages.
©Silberschatz, Korth and Sudarshan3.1Database System Concepts Extended Relational-Algebra-Operations Generalized Projection Aggregate Functions Outer Join.
Computing & Information Sciences Kansas State University Friday, 08 Sep 2006CIS 560: Database System Concepts Lecture 07 of 42 Tuesday, 30 January 2007.
Computing & Information Sciences Kansas State University Wednesday, 17 Sep 2008CIS 560: Database System Concepts Lecture 9 of 42 Wednesday, 18 September.
MySQL Tutorial Introduction to Database. Introduction of MySQL  MySQL is an SQL (Structured Query Language) based relational database management system.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
Computing & Information Sciences Kansas State University Mon, 10 Sep 2007CIS 560: Database System Concepts Lecture 8 of 42 Monday, 10 September 2007 William.
Computing & Information Sciences Kansas State University Tuesday. 13 Feb 2007CIS 560: Database System Concepts Lecture 13 of 42 Tuesday, 13 February 2007.
©Silberschatz, Korth and Sudarshan4.1Database System Concepts Chapter 4: SQL Basic Structure Set Operations Aggregate Functions Null Values Nested Subqueries.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
IS 230Lecture 7Slide 1 Relational Algebra Lecture 8 Text Book – Chapter.
Chapter 3: Relational Model III Additional Relational Algebra Operations Additional Relational Algebra Operations Views Views.
Database System Concepts, 6 th Ed. ©Silberschatz, Korth and Sudarshan See for conditions on re-usewww.db-book.com Chapter 4: Intermediate.
Query Languages Language in which user requests information from the database. Categories of languages Procedural Non-procedural, or declarative “Pure”
1st Semester, 2017 Sanghyun Park
Relational Model By Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany)
Structured Query Language (SQL)
Chapter 3: SQL.
Basic Structure of SQL The basic structure of an SQL expression consists of three clauses: The select clause corresponds to the projection operation of.
Chapter 3: SQL.
Lecture 4 of 42 Relational Joins Wednesday, 30 January 2008
Introduction to Database
Presentation transcript:

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Lecture 10 of 42 Tuesday, 06 February 2007 William H. Hsu Department of Computing and Information Sciences, KSU KSOL course page: Course web site: Instructor home page: Reading for Next Class: Rest of Chapter 5, Silberschatz et al., 5 th edition JDBC Primer (to be posted on Handouts page) Notes: MP2 Questions, Advanced SQL and Relational Calculus Preliminaries

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Views In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) Consider a person who needs to know a customer’s loan number but has no need to see the loan amount. This person should see a relation described, in SQL, by (select customer_name, loan_number from borrower, loan where borrower.loan_number = loan.loan_number ) A view provides a mechanism to hide certain data from the view of certain users. Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a view.

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Views In some cases, it is not desirable for all users to see the entire logical model (that is, all the actual relations stored in the database.) Consider a person who needs to know a customer’s loan number but has no need to see the loan amount. This person should see a relation described, in SQL, by (select customer_name, loan_number from borrower, loan where borrower.loan_number = loan.loan_number ) A view provides a mechanism to hide certain data from the view of certain users. Any relation that is not of the conceptual model but is made visible to a user as a “virtual relation” is called a view.

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts View Definition A view is defined using the create view statement which has the form create view v as where is any legal SQL expression. The view name is represented by v. Once a view is defined, the view name can be used to refer to the virtual relation that the view generates. View definition is not the same as creating a new relation by evaluating the query expression  Rather, a view definition causes the saving of an expression; the expression is substituted into queries using the view.

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Example Queries A view consisting of branches and their customers Find all customers of the Perryridge branch create view all_customer as (select branch_name, customer_name from depositor, account where depositor.account_number = account.account_number ) union (select branch_name, customer_name from borrower, loan where borrower.loan_number = loan.loan_number ) select customer_name from all_customer where branch_name = ‘Perryridge’

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Views Defined Using Other Views One view may be used in the expression defining another view A view relation v 1 is said to depend directly on a view relation v 2 if v 2 is used in the expression defining v 1 A view relation v 1 is said to depend on view relation v 2 if either v 1 depends directly to v 2 or there is a path of dependencies from v 1 to v 2 A view relation v is said to be recursive if it depends on itself.

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts View Expansion A way to define the meaning of views defined in terms of other views. Let view v 1 be defined by an expression e 1 that may itself contain uses of view relations. View expansion of an expression repeats the following replacement step: repeat Find any view relation v i in e 1 Replace the view relation v i by the expression defining v i until no more view relations are present in e 1 As long as the view definitions are not recursive, this loop will terminate

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Update of a View Create a view of all loan data in the loan relation, hiding the amount attribute create view branch_loan as select branch_name, loan_number from loan Add a new tuple to branch_loan insert into branch_loan values (‘Perryridge’, ‘L-307’) This insertion must be represented by the insertion of the tuple (‘L-307’, ‘Perryridge’, null ) into the loan relation

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Updates Through Views (Cont.) Some updates through views are impossible to translate into updates on the database relations  create view v as select branch_name from account insert into v values (‘L-99’, ‘ Downtown’, ‘23’) Others cannot be translated uniquely  insert into all_customer values (‘ Perryridge’, ‘John’)  Have to choose loan or account, and create a new loan/account number! Most SQL implementations allow updates only on simple views (without aggregates) defined on a single relation

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Joined Relations** Join operations take two relations and return as a result another relation. These additional operations are typically used as subquery expressions in the from clause Join condition – defines which tuples in the two relations match, and what attributes are present in the result of the join. Join type – defines how tuples in each relation that do not match any tuple in the other relation (based on the join condition) are treated.

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Joined Relations – Datasets for Examples Relation loan Relation borrower Note: borrower information missing for L-260 and loan information missing for L-155

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Joined Relations – Examples loan inner join borrower on loan.loan_number = borrower.loan_number loan left outer join borrower on loan.loan_number = borrower.loan_number

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Joined Relations – Examples loan natural inner join borrower loan natural right outer join borrower

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Joined Relations – Examples loan full outer join borrower using (loan_number) Find all customers who have either an account or a loan (but not both) at the bank. select customer_name from (depositor natural full outer join borrower ) where account_number is null or loan_number is null

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts End of Chapter 3

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Figure 3.1: Database Schema branch (branch_name, branch_city, assets) customer (customer_name, customer_street, customer_city) loan (loan_number, branch_name, amount) borrower (customer_name, loan_number) account (account_number, branch_name, balance) depositor (customer_name, account_number)

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Figure 3.3: Tuples inserted into loan and borrower

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Figure 3.4: The loan and borrower relations

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Chapter 4: Advanced SQL SQL Data Types and Schemas Integrity Constraints Authorization Embedded SQL Dynamic SQL Functions and Procedural Constructs** Recursive Queries** Advanced SQL Features**

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Built-in Data Types in SQL date: Dates, containing a (4 digit) year, month and date  Example: date ‘ ’ time: Time of day, in hours, minutes and seconds.  Example: time ‘09:00:30’ time ‘09:00:30.75’ timestamp: date plus time of day  Example: timestamp ‘ :00:30.75’ interval: period of time  Example: interval ‘1’ day  Subtracting a date/time/timestamp value from another gives an interval value  Interval values can be added to date/time/timestamp values

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Build-in Data Types in SQL (Cont.) Can extract values of individual fields from date/time/timestamp  Example: extract (year from r.starttime) Can cast string types to date/time/timestamp  Example: cast as date  Example: cast as time

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts User-Defined Types create type construct in SQL creates user-defined type create type Dollars as numeric (12,2) final create domain construct in SQL-92 creates user-defined domain types create domain person_name char(20) not null Types and domains are similar. Domains can have constraints, such as not null, specified on them.

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts ODBC Code int ODBCexample() { RETCODE error; HENV env; /* environment */ HDBC conn; /* database connection */ SQLAllocEnv(&env); SQLAllocConnect(env, &conn); SQLConnect(conn, "aura.bell-labs.com", SQL_NTS, "avi", SQL_NTS, "avipasswd", SQL_NTS); { …. Do actual work … } SQLDisconnect(conn); SQLFreeConnect(conn); SQLFreeEnv(env); }

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts JDBC Code public static void JDBCexample(String dbid, String userid, String passwd) { try { Class.forName ("oracle.jdbc.driver.OracleDriver"); Connection conn = DriverManager.getConnection( labs.com:2000:bankdb", userid, passwd); Statement stmt = conn.createStatement(); … Do Actual Work …. stmt.close(); conn.close(); } catch (SQLException sqle) { System.out.println("SQLException : " + sqle); }

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Procedural Extensions and Stored Procedures SQL provides a module language  Permits definition of procedures in SQL, with if-then-else statements, for and while loops, etc.  more in Chapter 9 Stored Procedures  Can store procedures in the database  then execute them using the call statement  permit external applications to operate on the database without knowing about internal details These features are covered in Chapter 9 (Object Relational Databases)

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts The Power of Recursion Recursive views make it possible to write queries, such as transitive closure queries, that cannot be written without recursion or iteration.  Intuition: Without recursion, a non-recursive non-iterative program can perform only a fixed number of joins of manager with itself  This can give only a fixed number of levels of managers  Given a program we can construct a database with a greater number of levels of managers on which the program will not work  The next slide shows a manager relation and each step of the iterative process that constructs empl from its recursive definition. The final result is called the fixed point of the recursive view definition. Recursive views are required to be monotonic. That is, if we add tuples to manger the view contains all of the tuples it contained before, plus possibly more

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Example of Fixed-Point Computation

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Chapter 5: Other Relational Languages Tuple Relational Calculus Domain Relational Calculus Query-by-Example (QBE) Datalog

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Tuple Relational Calculus A nonprocedural query language, where each query is of the form {t | P (t ) } It is the set of all tuples t such that predicate P is true for t t is a tuple variable, t [A ] denotes the value of tuple t on attribute A t  r denotes that tuple t is in relation r P is a formula similar to that of the predicate calculus

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Predicate Calculus Formula 1.Set of attributes and constants 2.Set of comparison operators: (e.g., , , , , ,  ) 3.Set of connectives: and (  ), or (v)‚ not (  ) 4.Implication (  ): x  y, if x if true, then y is true x  y  x v y 5.Set of quantifiers:   t  r (Q (t ))  ”there exists” a tuple in t in relation r such that predicate Q (t ) is true   t  r (Q (t ))  Q is true “for all” tuples t in relation r

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Banking Example branch (branch_name, branch_city, assets ) customer (customer_name, customer_street, customer_city ) account (account_number, branch_name, balance ) loan (loan_number, branch_name, amount ) depositor (customer_name, account_number ) borrower (customer_name, loan_number )

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Example Queries Find the loan_number, branch_name, and amount for loans of over $1200 Find the loan number for each loan of an amount greater than $1200 {t |  s  loan (t [loan_number ] = s [loan_number ]  s [amount ]  1200)} Notice that a relation on schema [loan_number ] is implicitly defined by the query {t | t  loan  t [amount ]  1200}

Computing & Information Sciences Kansas State University Tuesday, 06 Feb 2007CIS 560: Database System Concepts Example Queries Find the names of all customers having a loan, an account, or both at the bank {t |  s  borrower ( t [customer_name ] = s [customer_name ])   u  depositor ( t [customer_name ] = u [customer_name] ) Find the names of all customers who have a loan and an account at the bank {t |  s  borrower ( t [customer_name ] = s [customer_name ])   u  depositor ( t [customer_name ] = u [customer_name ])