Presentation is loading. Please wait.

Presentation is loading. Please wait.

CO887 – Lecture 8 SQL 2.

Similar presentations


Presentation on theme: "CO887 – Lecture 8 SQL 2."— Presentation transcript:

1 CO887 – Lecture 8 SQL 2

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

3 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

4 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.

5 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

6 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

7 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)

8 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

9 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

10 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

11 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;

12 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.

13 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?

14 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 ?)

15 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.

16 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)

17 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.

18 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

19 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

20 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:


Download ppt "CO887 – Lecture 8 SQL 2."

Similar presentations


Ads by Google