17
MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights Reserved.

MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

Embed Size (px)

Citation preview

Page 1: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

MIS 3053Database Design & Applications

The University of Tulsa

Professor: Akhilesh Bajaj

RM/SQL Lecture 3

© Akhilesh Bajaj, 2000, 2002, 2004. All Rights Reserved.

Page 2: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

Review of Lectures 1 & 2

• Learnt the basics of the relational model

• Learnt the notions of superkeys, candidate keys and primary keys

• Relational algebra- The select operation- The project operation-The assignment operation- The union operation- The set difference operation- The intersect operation- The cartesian product operation- The natural join operation

Page 3: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

Goals for Today

• Practise relational algebra

- Discussion in class (questions from you!)- In-class assignment

• Get started with SQL

Page 4: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

Discussion & Questions from You

Page 5: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

Relational Schema For Example Relational Algebra Queries

Professors (f_id, f_name, f_address, f_specialty, highest_degree)Courses (c_id, c_name, num_credits)Sections (c_id, s_id, time_held, week_days_held, date_began, date_ended, cl_id)Students (st_id, st_name, gender, st_gpa)Grades (st_id, c_id, s_id, grade)Classrooms (cl_id, capacity, location)Teach (f_id, c_id, s_id)

Page 6: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

In-Class Assignment

Example 1• Information on all Professors who have a Ph.D. degree and specialize in “Databases”.

Page 7: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

In-Class Assignment

Example 2• Names and number of credits of all courses being completely taught between “Jun-17-2003” and “Aug-15-2003”.

What if the course was at least partially taught between those dates?

Page 8: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

date_began >= ‘JUN-17-2003’ AND ‘date_ended<= ‘AUG-15-2003’

date_ended >= ‘JUN-17-2003’ AND ‘date_began<= ‘AUG-15-2003’

First Query (only courses completely taught in the period)

Second Query (courses that are at least partially taught in the period)

--------------------------------------------------------(Course spanned whole summer)

--------------------| (Course started before summer but ended in the summer)

(Course started in summer but ended after summer) |----------------------------

|---------------------------------------| (Time line for summer)

JUN 17, 2003 Aug, 15, 2003

Page 9: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

In-Class Assignment

Example 3• Names and faculty_ids of all Professors who are teaching or have taught at least once in classrooms that have a capacity of > 100.

What if we wanted names and faculty ids of professors who haveonly ever taught in classrooms with capacity > 100?

Page 10: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

In-Class Assignment

Example 4• Names and GPAs of all students who have an ‘A’ inMIS3053ASpr2008(c_id=“MIS3053”, s_id = “ASpr2008”)

What if we wanted Names and GPAs of all students who have an ‘A’ or a ‘B’ in MIS3053ASpr2008(c_id=“MIS3053”, s_id = “ASpr2008”)

Page 11: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

In-Class Assignment

Example 5• Names of all Professors who teach the student ”Johnny Jones”.

What if we wanted names of all professors who are only currentlyteaching “Johnny Jones”.

Page 12: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

In-Class Assignment

Example 6• Names of all students and Professors who go to class (or havegone to class) at 8:00 am.

Page 13: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

In-Class Assignment

Example 7• Names of all Professors who have never given a “C” grade.

If we look at the schema, we see that names are not unique. What if we had 5 professors called “Bob”, 3 of whom had given a ‘C’ at least once, but 2 had not. Would “Bob” show up in our query?

Page 14: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

Why is Relational Algebra Important For Us

• The backbone relational database query language.

• The examples so far have hopefully shown how it can be used toask ad-hoc questions of the database.

• The things we learnt here are supported in SQL. E.g., the UNION, the INTERSECT, the cartesian product operations.

• Relational Algebra is a great way to VISUALIZE the queryas it is being run. Knowing relational algebra will allow us to get a better understanding of SQL queries, since they are builtusing relational algebra concepts (and then some more concepts!!)

Page 15: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

Structured Query Language (SQL)

• The standard relational database language

• Used to query the database (hardest to learn!), defining the relational schema, inserting, deleting and modifying data in the database.

• Two standards exist: SQL-89 and SQL-92. Most systems support at least SQL-89.

• SQL is the standard language with which we interact with the relational database system.

• SQL was invented in the early 1970-s. It has been around for around 25 years, and likely to be around for decades ahead.

Page 16: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

Structured Query Language (SQL): Query Part

• To use SQL for querying , we need a defined relational schema.

• SQL allows the use of null values.

• The basic query clause in SQL is the

select <Attributes list>from <tables list>where Condition;

clause.

• Note that the select here is similar to the project in relational algebra. The where here is similar to the select in relational algebra.

Page 17: MIS 3053 Database Design & Applications The University of Tulsa Professor: Akhilesh Bajaj RM/SQL Lecture 3 © Akhilesh Bajaj, 2000, 2002, 2004. All Rights

Structured Query Language (SQL): Query Part

Example:•Information on all Professors who have a Ph.D. degree and specialize in “Databases”.