<<

Basic Relational Query Operations • Three basic query operations: Relational Database – Projection: retrieve selected attributes from a Manipulation – Selection (Restriction): retrieve selected from a relation – Join: combine two or more relations Robert C. Nickerson • Note: All relational database query operations ISYS 464 – Spring 2003 take one or more relations as input and produce Topic 08 one relation as output

Copyright (c) 2003 by All rights reserved. 1 Copyright (c) 2003 by All rights reserved. 2 Robert C. Nickerson Not for general distribution. Robert C. Nickerson Not for general distribution.

Student Course Database Basic Relational Database Student (Student Number, Student Name, Major) Course (Course Number, Course Name, Day, Time) Update Operations Student Course (Student Number, Course Number) Student • Three basic relational database update operations: Student Student Major Student Course Number Name Student Course – Add: insert one or more new tuples in a relation 123 Joe IS Number Number 234 Fred IS 123 ISYS 365 – Delete: remove one or more existing tuples from a 345 Mary Acct 123 FIN 350 relation 456 Sue Mgmt 123 MGMT 405 567 Lee Acct 234 ISYS 365 – Change: modify certain data in one or more tuples of a Course 234 FIN 350 Course Course Name Day Time 234 MGMT 405 relation Number 234 MKTG 431 ISYS 365 Adv C++ MW 12:30 345 ISYS 464 • Note: All relational database update operations ISYS 464 Database TTh 9:30 567 ISYS 464 ISYS 565 Data Comm TTh 14:00 567 ISYS 565 take one relation as input and produce a modified IBUS 330 Int Business MW 8:00 567 MGMT 405 FIN 350 Finance MW 14:00 678 ISYS 363 form of that relation as output MGMT 405 Org Behavior MW 15:30 678 MGMT 405 MKTG 431 Marketing TTh 8:00 Copyright (c) 2003 by All rights reserved. 3 Copyright (c) 2003 by All rights reserved. 4 Robert C. Nickerson Not for general distribution. Robert C. Nickerson Not for general distribution.

Projection Selection (Restriction) • The operation of retrieving certain tuples from a relation • The operation of retrieving certain attributes from a relation based on a condition • Can project one or more attributes • Selection can result in one or more tuples, or no tuples if no • Example: Project the Student Name and Major attribute tuples in the relation satisfy the condition from the Student relation • Example: Select all tuples from the Course relation where Input Output the course is taught on TTh Student Input Output Student Student Major Student Major Course Course Course Name Day Time Course Course Name Day Time Number Name Name Number Number 123 Joe IS Joe IS ISYS 365 Adv C++ MW 12:30 ISYS 464 Database TTh 9:30 234 Fred IS Fred IS ISYS 464 Database TTh 9:30 ISYS 565 Data Comm TTh 14:00 345 Mary Acct Mary Acct ISYS 565 Data Comm TTh 14:00 MKTG 431 Marketing TTh 8:00 456 Sue Mgmt Sue Mgmt IBUS 330 Int Business MW 8:00 567 Lee Acct Lee Acct FIN 350 Finance MW 14:00 MGMT 405 Org Behavior MW 15:30 MKTG 431 Marketing TTh 8:00

Copyright (c) 2003 by All rights reserved. 5 Copyright (c) 2003 by All rights reserved. 6 Robert C. Nickerson Not for general distribution. Robert C. Nickerson Not for general distribution.

Copyright (c) 2003 by Robert C. Nickerson. All rights reserved. Not for general distribution. 1 Example: Join Join Input Student Student Course Student Student Major Student Course Number Name Number Number 123 Joe IS 123 ISYS 365 • The operation of combining the tuples of 234 Fred IS 123 FIN 350 345 Mary Acct 123 MGMT 405 two relations based on a condition 456 Sue Mgmt 234 ISYS 365 567 Lee Acct 234 FIN 350 • Example: Join the Student relation and the 234 MGMT 405 234 MKTG 431 Student Course relation based on the 345 ISYS 464 Student Number in the Student relation 567 ISYS 464 567 ISYS 565 equaling the Student Number in the Student 567 MGMT 405 678 ISYS 363 Course relation 678 MGMT 405

Copyright (c) 2003 by All rights reserved. 7 Copyright (c) 2003 by All rights reserved. 8 Robert C. Nickerson Not for general distribution. Robert C. Nickerson Not for general distribution.

Join Use Example: Join • Join is the operation we use to utilize relationships in Output relational : – By joining two relations based on the of one relation Student Student Major Student Course Number Name Number Number equaling the corresponding of another relation, we 123 Joe IS 123 ISYS 365 match related tuples from the two relations 123 Joe IS 123 FIN 350 123 Joe IS 123 MGMT 405 • Join with different relationships: 234 Fred IS 234 ISYS 365 – One-to-one: Each of one relation will match zero or one 234 Fred IS 234 FIN 350 tuples of the other relation 234 Fred IS 234 MGMT 405 234 Fred IS 234 MKTG 431 – One-to-many: Each tuple of the parent relation will match zero, 345 Mary Acct 345 ISYS 464 one, or more tuples of the child relation 567 Lee Acct 567 ISYS 464 – Many-to-many: Requires joining three relations, the two related 567 Lee Acct 567 ISYS 565 567 Lee Acct 567 MGMT 405 relations and the intersection relation. Each tuple of one related relation is matched with related tuples in the other related relation, Unmatched Student tuples and unmatched Student and vice versa, through the intersection relation. Course tuples do not appear in the output. • Note: Join can result in no tuples if condition is not met Copyright (c) 2003 by All rights reserved. 9 Copyright (c) 2003 by All rights reserved. 10 Robert C. Nickerson Not for general distribution. Robert C. Nickerson Not for general distribution.

Join Terminology Inner and Outer Joins • Join condition: the condition that indicates under what circumstances tuples in the two relations are to be combined • Inner : a join in which tuples in the two joined relations • Note: We can have a join involving any of the relational that do not satisfy the join condition are not included in the operators: =, <>, >, >=, <, <= result • Equijoin: a join involving an equal join condition • Note: An inner join is assumed unless otherwise stated • Note: Only with an equijoin are we guaranteed to have two • Example: Previous example was an inner join attributes that are identical in the result of the join • Outer join (full outer join): a join in which tuples in the • Natural join: an equijoin in which one of the duplicate two joined relations that do not satisfy the join condition are attributes is automatically eliminated 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 All rights reserved. 11 Copyright (c) 2003 by All rights reserved. 12 Robert C. Nickerson Not for general distribution. Robert C. Nickerson Not for general distribution.

Copyright (c) 2003 by Robert C. Nickerson. All rights reserved. Not for general distribution. 2 Example: Outer Join Output Left and Right Outer Join Student Student M ajor Student Course Number Name Number Number 123 Joe IS 123 ISYS 365 123 Joe IS 123 FIN 350 • Left outer join: a join in which tuples in the relation 123 Joe IS 123 MGMT 405 234 Fred IS 234 ISYS 365 named first (on the left) that do not satisfy the join 234 Fred IS 234 FIN 350 234 Fred IS 234 MGMT 405 condition are included in the result 234 Fred IS 234 MKTG 431 345 Mary Acct 345 ISYS 464 • Right outer join: a join in which tuples in the 456 Sue Mgmt NULL NULL 567 Lee Acct 567 ISYS 464 relation named second (on the right) that do not 567 Lee Acct 567 ISYS 565 567 Lee Acct 567 MGMT 405 satisfy the join condition are included in the result NULL NULL NULL 678 ISYS 363 NULL NULL NULL 678 MGMT 405

Note NULLs for attributes of unmatched tuples

Copyright (c) 2003 by All rights reserved. 13 Copyright (c) 2003 by All rights reserved. 14 Robert C. Nickerson Not for general distribution. Robert C. Nickerson Not for general distribution.

Example: Left Outer Join Example: Right Outer Join • Example: Left outer join of Student and Student Course relations • Example: Right outer join of Student and Student Course relations based on the Student Number in the Student relation equaling the based on the Student Number in the Student relation equaling the Student Number in the Student Course relation Student Number in the Student Course relation Output Output

Student Student Major Student Course Student Student Major Student Course Number Name Number Number Number Name Number Number 123 Joe IS 123 ISYS 365 123 Joe IS 123 ISYS 365 123 Joe IS 123 FIN 350 123 Joe IS 123 FIN 350 123 Joe IS 123 MGMT 405 123 Joe IS 123 MGMT 405 234 Fred IS 234 ISYS 365 234 Fred IS 234 ISYS 365 234 Fred IS 234 FIN 350 234 Fred IS 234 FIN 350 234 Fred IS 234 MGMT 405 234 Fred IS 234 MGMT 405 234 Fred IS 234 MKTG 431 234 Fred IS 234 MKTG 431 345 Mary Acct 345 ISYS 464 345 Mary Acct 345 ISYS 464 567 Lee Acct 567 ISYS 464 456 Sue Mgmt NULL NULL 567 Lee Acct 567 ISYS 565 567 Lee Acct 567 ISYS 464 567 Lee Acct 567 MGMT 405 567 Lee Acct 567 ISYS 565 NULL NULL NULL 678 ISYS 363 567 Lee Acct 567 MGMT 405 NULL NULL NULL 678 MGMT 405

Copyright (c) 2003 by All rights reserved. 15 Copyright (c) 2003 by All rights reserved. 16 Robert C. Nickerson Not for general distribution. Robert C. Nickerson Not for general distribution.

Relational Database Languages Data Manipulation Language (DML)

• Three main sub-languages in a relational • A language used to query and update the database language data in a database – Data Manipulation Language (DML) – used to • A relational database is manipulated by a query and update the data in a database relational DML – (DDL) – used to • Two main types of relational DMLs: define the characteristics of a database – (DCL) – used to control user access to a database –

Copyright (c) 2003 by All rights reserved. 17 Copyright (c) 2003 by All rights reserved. 18 Robert C. Nickerson Not for general distribution. Robert C. Nickerson Not for general distribution.

Copyright (c) 2003 by Robert C. Nickerson. All rights reserved. Not for general distribution. 3 Relational Algebra Relational Calculus • Non-procedural (like calculus: ∫ x2 dx ) • States what is wanted, not how to obtain it • Procedural (like algebra: x2 + 2x + 3 ) • Query by Example (QBE) is a type of relational calculus • States how to obtain result by giving the sequence of • Example using QBE: What are the names of all students taking ISYS operations 263? • Example using hypothetical relational algebra: 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) Order determined by Student [Student_Name] software • Processing performed left to right through the command QBE is sometimes called a graphical language Copyright (c) 2003 by All rights reserved. 19 Copyright (c) 2003 by All rights reserved. 20 Robert C. Nickerson Not for general distribution. Robert C. Nickerson Not for general distribution.

Hybrid Relational DML • Combines elements of relational algebra and relational Data Definition Language (DDL) calculus • Some procedural and some non-procedural characteristics • A language used to define the characteristics • States what is wanted but may state some sequencing of of a database operations • Example using SQL: What are the names of all students • DDL permits: taking ISYS 263? – Definition of relations in the conceptual of SELECT Student_Name the database FROM Student, Student_Course – Definition of relations in each external view of WHERE Student.Student_Number = Student_Course.Student_Number the database AND Course_Number = 'ISYS 263‘ – Definition of structures used to enhance • SQL is sometimes called a transform-oriented language performance (e.g., indexes)

Copyright (c) 2003 by All rights reserved. 21 Copyright (c) 2003 by All rights reserved. 22 Robert C. Nickerson Not for general distribution. Robert C. Nickerson Not for general distribution.

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 All rights reserved. 23 Robert C. Nickerson Not for general distribution.

Copyright (c) 2003 by Robert C. Nickerson. All rights reserved. Not for general distribution. 4