Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Basic Relational Database Query Operations • Three basic relational database query operations: Relational Database Manipulation – Projection: retrieve selected attributes from a relation – Selection (Restriction): retrieve selected tuples from a relation – Join: combine two or more relations Robert C. Nickerson ISYS 464 – Spring 2003 Topic 08 Copyright (c) 2003 by Robert C. Nickerson All rights reserved. Not for general distribution. • Note: All relational database query operations take one or more relations as input and produce one relation as output 1 Student Student N um ber 123 234 345 456 567 – Add: insert one or more new tuples in a relation – Delete: remove one or more existing tuples from a relation – Change: modify certain data in one or more tuples of a relation C ourse N um ber ISY S 365 ISY S 464 ISY S 565 IB U S 330 FIN 350 M G M T 405 M K T G 431 3 • The operation of retrieving certain attributes from a relation • Can project one or more attributes • Example: Project the Student Name and Major attribute from the Student relation Output Input Copyright (c) 2003 by Robert C. Nickerson M ajor IS IS Acct M gmt Acct Student Name Joe Fred M ary Sue Lee Student C ourse M ajor IS IS A cct M gm t A cct C ourse N am e D ay T im e A dv C ++ D atabase D ata C om m Int B usiness Finance O rg B ehavior M arketing MW T Th T Th MW MW MW T Th 12:30 9:30 14:00 8:00 14:00 15:30 8:00 Copyright (c) 2003 by Robert C. Nickerson Student N um ber 123 123 123 234 234 234 234 345 567 567 567 678 678 C ourse N um ber ISY S 365 FIN 350 M G M T 405 ISY S 365 FIN 350 M G M T 405 M K T G 431 ISY S 464 ISY S 464 ISY S 565 M G M T 405 ISY S 363 M G M T 405 All rights reserved. Not for general distribution. 4 Selection (Restriction) Projection Student Name Joe Fred M ary Sue Lee Student N am e Joe Fred M ary Sue L ee C ourse • Note: All relational database update operations take one relation as input and produce a modified form of that relation as output Student Student Number 123 234 345 456 567 2 Student (Student Number, Student Name, Major) Course (Course Number, Course Name, Day, Time) Student Course (Student Number, Course Number) • Three basic relational database update operations: All rights reserved. Not for general distribution. All rights reserved. Not for general distribution. Student Course Database Basic Relational Database Update Operations Copyright (c) 2003 by Robert C. Nickerson Copyright (c) 2003 by Robert C. Nickerson IS IS Acct M gmt Acct Copyright (c) 2003 by Robert C. Nickerson. All rights reserved. Not for general distribution. Output Input Course Course Number ISYS 365 ISYS 464 ISYS 565 IBUS 330 FIN 350 MGMT 405 MKTG 431 M ajor All rights reserved. Not for general distribution. • The operation of retrieving certain tuples from a relation based on a condition • Selection can result in one or more tuples, or no tuples if no tuples in the relation satisfy the condition • Example: Select all tuples from the Course relation where the course is taught on TTh 5 Course Name Day Adv C++ Database Data Comm Int Business Finance Org Behavior Marketing Copyright (c) 2003 by Robert C. Nickerson MW TTh TTh MW MW MW TTh Time 12:30 9:30 14:00 8:00 14:00 15:30 8:00 Course Number ISYS 464 ISYS 565 MKTG 431 All rights reserved. Not for general distribution. Course Name Day Time Database Data Comm Marketing 9:30 14:00 8:00 TTh TTh TTh 6 1 Example: Join Join Input Student Student Number 123 234 345 456 567 • The operation of combining the tuples of two relations based on a condition • Example: Join the Student relation and the Student Course relation based on the Student Number in the Student relation equaling the Student Number in the Student Course relation Copyright (c) 2003 by Robert C. Nickerson All rights reserved. Not for general distribution. 7 M ajo r IS IS IS IS IS IS IS A cct A cct A cct A cct S tud en t N u m ber 123 123 123 234 234 234 234 345 567 567 567 All rights reserved. Not for general distribution. – One-to-one: Each tuple of one relation will match zero or one tuples of the other relation – One-to-many: Each tuple of the parent relation will match zero, one, or more tuples of the child relation – Many-to-many: Requires joining three relations, the two related relations and the intersection relation. Each tuple of one related relation is matched with related tuples in the other related relation, and vice versa, through the intersection relation. • Note: Join can result in no tuples if condition is not met 9 Join Terminology All rights reserved. Not for general distribution. Copyright (c) 2003 by Robert C. Nickerson. All rights reserved. Not for general distribution. 8 • Join with different relationships: • Join condition: the condition that indicates under what circumstances tuples in the two relations are to be combined • Note: We can have a join involving any of the relational operators: =, <>, >, >=, <, <= • Equijoin: a join involving an equal join condition • Note: Only with an equijoin are we guaranteed to have two attributes that are identical in the result of the join • Natural join: an equijoin in which one of the duplicate attributes is automatically eliminated Copyright (c) 2003 by Robert C. Nickerson All rights reserved. Not for general distribution. – By joining two relations based on the primary key of one relation equaling the corresponding foreign key of another relation, we match related tuples from the two relations C o u rse N u m b er IS Y S 3 6 5 F IN 3 5 0 M G M T 405 IS Y S 3 6 5 F IN 3 5 0 M G M T 405 M K T G 43 1 IS Y S 4 6 4 IS Y S 4 6 4 IS Y S 5 6 5 M G M T 405 Unmatched Student tuples and unmatched Student Course tuples do not appear in the output. Copyright (c) 2003 by Robert C. Nickerson IS IS Acct M gm t Acct Student Course Student Course Number Number 123 ISYS 365 123 FIN 350 123 M GM T 405 234 ISYS 365 234 FIN 350 234 M GM T 405 234 M KTG 431 345 ISYS 464 567 ISYS 464 567 ISYS 565 567 M GM T 405 678 ISYS 363 678 M GM T 405 • Join is the operation we use to utilize relationships in relational databases: O u tp u t S tud en t N am e Jo e Jo e Jo e F red F red F red F red M ary L ee L ee L ee Copyright (c) 2003 by Robert C. Nickerson M ajor Join Use Example: Join S tud en t N u m b er 123 123 123 234 234 234 234 345 567 567 567 Student Name Joe Fred M ary Sue Lee 11 Copyright (c) 2003 by Robert C. Nickerson All rights reserved. Not for general distribution. 10 Inner and Outer Joins • Inner join: a join in which tuples in the two joined relations that do not satisfy the join condition are not included in the result • Note: An inner join is assumed unless otherwise stated • Example: Previous example was an inner join • Outer join (full outer join): a join in which tuples in the two joined relations that do not satisfy the join condition are included in the result • Example: Outer join of Student and Student Course relations based on the Student Number in the Student relation equaling the Student Number in the Student Course relation Copyright (c) 2003 by Robert C. Nickerson All rights reserved. Not for general distribution. 12 2 Example: Outer Join Left and Right Outer Join O u tp u t S tu d e n t N um ber 123 123 123 234 234 234 234 345 456 567 567 567 NULL NULL S tu d e n t N am e Jo e Jo e Jo e F re d F re d F re d F re d M ary Sue L ee L ee L ee NULL NULL M a jo r IS IS IS IS IS IS IS A cct M gm t A cct A cct A cct NULL NULL S tu d e n t N um ber 123 123 123 234 234 234 234 345 NULL 567 567 567 678 678 C o u rs e N um ber IS Y S 3 6 5 F IN 3 5 0 M G M T 405 IS Y S 3 6 5 F IN 3 5 0 M G M T 405 M K TG 431 IS Y S 4 6 4 NULL IS Y S 4 6 4 IS Y S 5 6 5 M G M T 405 IS Y S 3 6 3 M G M T 405 • Left outer join: a join in which tuples in the relation named first (on the left) that do not satisfy the join condition are included in the result • Right outer join: a join in which tuples in the relation named second (on the right) that do not satisfy the join condition are included in the result Note NULLs for attributes of unmatched tuples Copyright (c) 2003 by Robert C. Nickerson All rights reserved. Not for general distribution. 13 Copyright (c) 2003 by Robert C. Nickerson All rights reserved. Not for general distribution. Example: Right Outer Join Example: Left Outer Join • Example: Left outer join of Student and Student Course relations based on the Student Number in the Student relation equaling the Student Number in the Student Course relation • Example: Right outer join of Student and Student Course relations based on the Student Number in the Student relation equaling the Student Number in the Student Course relation O u tp u t O u tp u t S tud en t N u m b er 123 123 123 234 234 234 234 345 456 567 567 567 Copyright (c) 2003 by Robert C. Nickerson S tud en t N am e Jo e Jo e Jo e F red F red F red F red M ary Sue L ee L ee L ee M ajo r IS IS IS IS IS IS IS A cct M gm t A cct A cct A cct S tud en t N u m ber 123 123 123 234 234 234 234 345 NULL 567 567 567 C o u rse N u m b er IS Y S 3 6 5 F IN 3 5 0 M G M T 405 IS Y S 3 6 5 F IN 3 5 0 M G M T 405 M K T G 43 1 IS Y S 4 6 4 NULL IS Y S 4 6 4 IS Y S 5 6 5 M G M T 405 All rights reserved. Not for general distribution. S tu d e n t N um ber 123 123 123 234 234 234 234 345 567 567 567 NULL NULL 15 Relational Database Languages – Data Manipulation Language (DML) – used to query and update the data in a database – Data Definition Language (DDL) – used to define the characteristics of a database – Data Control Language (DCL) – used to control user access to a database All rights reserved. Not for general distribution. Copyright (c) 2003 by Robert C. Nickerson. All rights reserved. Not for general distribution. Copyright (c) 2003 by Robert C. Nickerson S tu d e n t N am e Jo e Jo e Jo e F re d F re d F re d F re d M a ry L ee L ee L ee NULL NULL M a jo r IS IS IS IS IS IS IS A cct A cct A cct A cct NULL NULL S tu d e n t N um ber 123 123 123 234 234 234 234 345 567 567 567 678 678 C o u rse N um ber IS Y S 3 6 5 F IN 3 5 0 M G M T 405 IS Y S 3 6 5 F IN 3 5 0 M G M T 405 M K TG 431 IS Y S 4 6 4 IS Y S 4 6 4 IS Y S 5 6 5 M G M T 405 IS Y S 3 6 3 M G M T 405 All rights reserved. Not for general distribution. 16 Data Manipulation Language (DML) • Three main sub-languages in a relational database language Copyright (c) 2003 by Robert C. Nickerson 14 17 • A language used to query and update the data in a database • A relational database is manipulated by a relational DML • Two main types of relational DMLs: – Relational algebra – Relational calculus Copyright (c) 2003 by Robert C. Nickerson All rights reserved. Not for general distribution. 18 3 Relational Calculus Relational Algebra • Procedural (like algebra: x2 + 2x + 3 ) • States how to obtain result by giving the sequence of operations • Example using hypothetical relational algebra: What are the names of all students taking ISYS 263? • • • • Non-procedural (like calculus: ∫ x2 dx ) States what is wanted, not how to obtain it Query by Example (QBE) is a type of relational calculus Example using QBE: What are the names of all students taking ISYS 263? Order of processing is not given Student_Course WHERE Course_Number = 'ISYS 263' JOIN (Student_Course.Student_Number = Student.Student_Number) Student [Student_Name] Order determined by software • Processing performed left to right through the command Copyright (c) 2003 by Robert C. Nickerson All rights reserved. Not for general distribution. 19 Hybrid Relational DML • Combines elements of relational algebra and relational calculus • Some procedural and some non-procedural characteristics • States what is wanted but may state some sequencing of operations • Example using SQL: What are the names of all students taking ISYS 263? SELECT Student_Name FROM Student, Student_Course WHERE Student.Student_Number = Student_Course.Student_Number AND Course_Number = 'ISYS 263‘ • SQL is sometimes called a transform-oriented language Copyright (c) 2003 by Robert C. Nickerson All rights reserved. Not for general distribution. 21 QBE is sometimes called a graphical language Copyright (c) 2003 by Robert C. Nickerson All rights reserved. Not for general distribution. 20 Data Definition Language (DDL) • A language used to define the characteristics of a database • DDL permits: – Definition of relations in the conceptual view of the database – Definition of relations in each external view of the database – Definition of structures used to enhance performance (e.g., indexes) Copyright (c) 2003 by Robert C. Nickerson All rights reserved. Not for general distribution. 22 Data Control Language (DCL) • A language used to control user access to the database • DCL permits: – Giving of privileges to users (e.g., access, update privileges) – Removing privileges from users Copyright (c) 2003 by Robert C. Nickerson All rights reserved. Not for general distribution. Copyright (c) 2003 by Robert C. Nickerson. All rights reserved. Not for general distribution. 23 4