CO887 – Lecture 8 SQL 2.

Slides:



Advertisements
Similar presentations
Advanced SQL (part 1) CS263 Lecture 7.
Advertisements

DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 8-1 David M. Kroenke’s Chapter Eight: Database Redesign Database Processing:
Fundamentals, Design, and Implementation, 9/e Chapter 8 Database Redesign.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Introduction to Structured Query Language (SQL)
Prentice Hall © COS 346 Day Agenda Questions? Assignment 7 Corrected –4 A’s and 4 B’s Assignment 8 posted –Due April 6 Quiz 2 next class.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Concepts of Database Management Sixth Edition
Introduction to Structured Query Language (SQL)
David M. Kroenke and David J
Oracle Data Definition Language (DDL)
ASP.NET Programming with C# and SQL Server First Edition
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Learningcomputer.com SQL Server 2008 – Entity Relationships in a Database.
Concepts of Database Management, Fifth Edition Chapter 4: The Relational Model 3: Advanced Topics.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
David M. Kroenke and David J. Auer Database Processing Fundamentals, Design, and Implementation Chapter Eight: Database Redesign.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
Logical Database Design and the Rational Model
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
Chapter 5 Database Design
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Relational Database Design
Chapter 6 - Database Implementation and Use
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
David M. Kroenke and David J
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Lecture 2 The Relational Model
Lecture # 13 (After 1st Exam)
Translation of ER-diagram into Relational Schema
COS 346 Day 8.
David M. Kroenke and David J
Chapter 3 The Relational Database Model
STRUCTURED QUERY LANGUAGE
Chapter 8 Working with Databases and MySQL
The Relational Model Relational Data Model
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
SQL DATA CONSTRAINTS.
Oracle Data Definition Language (DDL)
Contents Preface I Introduction Lesson Objectives I-2
Relational Database Design
Chapter 8 Database Redesign
Chapter 11 Managing Databases with SQL Server 2000
IST 318 Database Administration
Indexes and more Table Creation
Integrity 5/5/2019 See scm-intranet.
Manipulating Data Lesson 3.
Database Systems: Design, Implementation, and Management
Presentation transcript:

CO887 – Lecture 8 SQL 2

This Lecture Multi-table queries Nested queries Data definition (creating tables) Using MySQL

Example: Customer / Product / Ordering Table cid name city discount contact c001 TipTop London 10.00 Mr Smith c002 Basic Paris 12.00 M. Parot c003 Allied 8.00 Ms Evans product pid name quantity price p01 comb 11140 0.50 p02 brush 30300 p03 razor 15060 1.00 ordering ordno month quantity cid pid 1011 Jan 1000 c001 p01 1012 Feb c002 p03 1013 Jun c003

Joins: Queries on multiple tables linkage condition(s) must be specified aliases or table names must be used to avoid ambiguity any number of tables can be joined Example: Which customers have ordered product p03? Older syntax using linkage conditions in WHERE clause SELECT DISTINCT name FROM customer c, ordering o WHERE c.cid = o.cid AND pid = ‘p03’; OR the more recent JOIN..ON syntax in the FROM clause FROM customer c JOIN ordering o ON c.cid = o.cid WHERE pid = ‘p03’; Joining data is necessary for when you want to generate reports which represent a composite of rows from multiple tables. There are two ways of doing this. Using a WHERE statement to link a foreign key to a primary key Using a JOIN statement in the from clause. Both are fine, JOIN is more recent, another example of redundancy in SQL.

Joins (2) Conceptually, we can think of this query as consisting of 3 steps: 1) find, in the order table, the rows where pid = ‘p03’ 2) find, in the customer table, the cids that match the cids of the rows returned by step 1 CUSTOMER ( cid, name, city, discount, contact) ORDERING (ordno, month, cid, pid, qty) 3) get the names of the customers returned by step 2, removing duplicates (DISTINCT clause) When faced with more than simple queries, it is sometimes useful to break them down into smaller steps, then using each query to build on each other. With the previous slide we can break the query into 3 steps: Find p03 rows in orders (record set 1) Find cids in the customers table CIDs that match those in record set 1 (to produce record set 2) Get the names of the customers returned in step 2 in record set 2, but remove duplicates (using DISTINCT) to produce record set 3

Joins (3) Retrieve a list of customers, products and order quantities where the quantity is greater than 500. SELECT DISTINCT c.name, p.name, o.qty FROM customer c, product p, ordering o WHERE o.qty > 500 AND c.cid = o.cid AND o.pid = p.pid; CUSTOMER ( cid, name, city, discount, contact) ORDERING (ordno, month, cid, pid, qty) PRODUCT ( pid, name, city, quantity, price) Who can break this down into its logical steps? Find all orders where quantity is greater than 500 -> record set 1 Find all customers where cid is in record set 1 -> record set 2 Find all products where pid is in record set 2 -> record set 3 Select DISTINCT instances of customer, product and qty. What might this look like if we had a Join? SELECT DISTINCT c.name, p.name, o.qty FROM (customer c JOIN ordering o ON c.cid=o.cid) JOIN product p ON o.pid=p.pid WHERE o.qty > 500

Joins (4) SELECT DISTINCT c.name, p.name, o.qty As before, but using the JOIN…ON syntax. Retrieve a list of customers, products and order quantities where the quantity is greater than 500. SELECT DISTINCT c.name, p.name, o.qty FROM (customer c JOIN ordering o ON c.cid=o.cid) JOIN product p ON o.pid=p.pid WHERE o.qty > 500 Now using JOIN, same query. Works slightly differently. We join all records between customer and ordering. Join all products on order, where qty is greater than 500. -> Draw diagram to show this (c) (o) = INTERSECT (c) (c,o) (p)

Quiz Join Ordering and Products using JOIN ON Output order number, order quantity, product name Modify the above query so that it outputs only orders for product ‘razor’ What would the query look like if the join was via a WHERE clause on the linking columns

Nested queries SELECT statements can be nested i.e. they can appear in the WHERE clause Inner- or sub-queries can be used to: represent a constant value represent a ‘constant’ that varies with each row being processed return a list of values for comparison most sub-queries return a single column to the outer query I.e. may look up a fixed value from a separate table to use as part of the wider query i.e., may include conditions on the fixed value look up i.e. may return a sub-set of records for which to compare to

Nested queries List names of cities where customers are based, for those customers who have ordered p03. Conventional JOIN: SELECT DISTINCT city FROM customer c join ordering o on o.cid = c.cid WHERE o.pid = ‘p03’; OR, using IN with subquery to represent a set: SELECT DISTINCT city FROM customer WHERE cid IN ( SELECT cid FROM ordering WHERE pid = ‘p03’); OR, using the EXISTS syntax which evaluates TRUE if the subquery returns one or more rows: FROM customer c WHERE EXISTS ( WHERE cid = c.cid AND pid =‘p03’); Here are some more examples on how to perform a JOIN, but with greater complexity, and some redundancy: IN where a value is identified in the nested result (i.e. compared to a set) EXIST, where at least one row in the nested result matches the value (whether it matches any results) When to use IN or EXISTS? IN -> faster than exists when sub query result is very small EXISTS -> faster than IN when sub query result is very large Why not just use a join ? Other examples: select * from customers c, (SELECT cid from ordering where month IN ('Jan','Feb')) o WHERE c.cid = o.cid;  if we want to use a select statement in the FROM clause

Data definition in SQL top level operation of creating a database depends on the DBMS being used e.g. CREATE DATABASE in MySQL several database objects exist in a database environment such as catalogs, tables, views, rules … these objects are created and destroyed by statements such as CREATE TABLE <table_name> DROP TABLE <table_name> note: DROP removes all references to a table DELETE should be used to remove all rows we concentrate on table definition the create table statement also enables the implementation of integrity constraints that apply What might prevent me from dropping a table if other data is present in my database? Think back to constraints implied in the relational model (remember that SQL is based on the relational model). I.e., DROP TABLE customers;

CREATE TABLE Basic syntax conforms to the ISO standard in all DBMS; subclauses defining integrity constraints may vary Example: To create the customers table, specify the primary key, and apply not null constraints CREATE TABLE customer ( cid CHAR(4), name VARCHAR (30) NOT NULL, city VARCHAR (20), discount DOUBLE, contact VARCHAR(24), PRIMARY KEY (cid) ); This is a simple table definition. Note the reserved words provided by SQL, the primary key, and then column names, data types, and a constraint of ‘NOT NULL’ You define the constraints on the table at time of definition.

Constraints Mandatory data CHECK constraint Specified using the NOT NULL clause in a column specification Primary key columns are, by definition, NOT NULL CHECK constraint used to constrain the domain (data type) values of columns Example: To create a constraint on customer discount discount DOUBLE CHECK (discount <= 15.00) Example: To force column values to be from a specified set college CHAR (1) CHECK (college IN(’D’,’E’,’K’,’R’,’W’)) NOT NULL means a value must be provided when inserting records into your table CHECK is applying a constraint to the data type domain. Example, can apply comparison operators in a CHECK constraint. What should be mandatory for our customer? Could we see any other check constraints on our customer table?

Constraints (2) DEFAULT constraint UNIQUE constraint Specified using the DEFAULT clause in a column specification Example: To create a default value on customer discount discount DOUBLE DEFAULT 0.0 UNIQUE constraint used to ensure unique value in each row for non-primary keys Example: in a car table reg_no CHAR(7) PRIMARY KEY, chassis_no VARCHAR(32) NOT NULL UNIQUE Other constraints include DEFAULT, so if a value for that column is not present, provide a default. This is useful for including a timestamp in your records. To help manage a database you often see fields such as LAST_UPDATED, which is automatically updated with a timestamp when a record changes. The UNIQUE constraint ensures a value has not been previously added to the table CUSTOMER table – examples over ‘UNIQUE’ constraints? (none) CUSTOMER table – examples over ‘DEFAULT’ constraints? (discount 0.0 ?)

Entity Integrity Primary key: a unique identifier for each row Entity integrity rule no primary key (or part thereof) should have a null value CREATE TABLE product ( pid CHAR(3), name VARCHAR(10) NOT NULL, city VARCHAR(10) NOT NULL, quantity INTEGER, price DOUBLE, PRIMARY KEY (pid) ); Primary keys can be composite pid CHAR(3), subprodno CHAR(2), name VARCHAR(10), PRIMARY KEY (prod, subprodno) In fact, if you assign a column to be a primary key, it automatically makes it NOT NULL Primary key values must not be null. Some DBMS will enforce this even though you do not explicitly state this in your SQL. To be safe, always specify the NOT NULL constraint on primary keys. Primary keys can also be composite, as shown here.

Referential Integrity Referential integrity rule ensures integrity of primary and foreign keys used to establish relationships between tables the value of a foreign key must be either the value of an existing primary key or null REFERENCES clause in CREATE TABLE statement Specifies foreign key columns in table and the related (usually primary key) in the ‘parent’ table. CREATE TABLE ordering ( ordno INTEGER AUTO_INCREMENT, month CHAR(3) NOT NULL, cid CHAR(4) NOT NULL, pid CHAR(3) NOT NULL, PRIMARY KEY (ordno), FOREIGN KEY (cid) REFERENCES customers (cid), FOREIGN KEY (pid) REFERENCES products (pid) ); Referential integrity refers to the fact that a foreign key value must exist as a primary key, before being used. To create foreign keys we add another constraint type ‘FOREIGN KEY (…) REFERENCES tablename (…) Note I have also done something different here, can anyone spot what it is? (AUTO_INCREMENT)

Executing SQL against a Database server MySQL: Open source database Access via command line Graphical tools (PHPMyAdmin, Toad for MySQL) For the terminal session we will be accessing a MySQL database over PuTTy Actually implementing your database. We’re using the MySQL Database Management System Several ways we can access this, we’re going to focus on command line first. There are editors out there that are very helpful, and Toad for MySQL is probably the best in my opinion.

MySQL commands CREATE DATABASE <database name>; -> create a database SHOW DATABASES; -> show a list of databases USE <database name>; -> select a database SHOW TABLE; -> show a list of tables DESCRIBE <table name>; -> show table properties DROP TABLE <table name>; -> delete a table SELECT * FROM <table name>; ->execute sql example All commands input into MySQL (including SQL statements) should end in with a semi-colon

Missing topics… SQL is a powerful language, not everything can be covered DBMS often provide extensions to the SQL language, as well as their own functionality Creating your own Database server Check WAMP (Windows), MAMP (Mac) Or on linux: sudo apt-get install mysql

Summary We have covered more advanced SQL techniques There is a lot more to SQL (database specific): Triggers Procedures Altering tables after definition UNION, INTERSECT, EXCEPT (set operators) Views Review: http://www.w3schools.com/sql/