DBMS Languages. Data Definition Language (DDL) Used to define the conceptual and internal schemas Includes constraint definition language (CDL) for describing.

Slides:



Advertisements
Similar presentations
SQL: The Query Language Part 2
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Introduction to Database Systems 1 SQL: The Query Language Relation Model : Topic 4.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 Lecture 11: Basic SQL, Integrity constraints
SQL: Queries, Constraints, Triggers
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
Database Management Systems 1 Raghu Ramakrishnan SQL: Queries, Programming, Triggers Chpt 5.
SQL: The Query Language Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY courtesy of Joe Hellerstein and etc for some slides.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5.
CS 405G: Introduction to Database Systems
M ATH IN SQL. 222 A GGREGATION O PERATORS Operators on sets of tuples. Significant extension of relational algebra. SUM ( [DISTINCT] A): the sum of all.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
SQL 2 – The Sequel R&G, Chapter 5 Lecture 10. Administrivia Homework 2 assignment now available –Due a week from Sunday Midterm exam will be evening of.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 90 Database Systems I SQL Queries.
FALL 2004CENG 351 File Structures and Data Management1 SQL: Structured Query Language Chapter 5.
Introduction to Structured Query Language (SQL)
Rutgers University SQL: Queries, Constraints, Triggers 198:541 Rutgers University.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
1 SQL: Structured Query Language Chapter 5. 2 SQL and Relational Calculus relationalcalculusAlthough relational algebra is useful in the analysis of query.
Introduction to Structured Query Language (SQL)
CSC343 – Introduction to Databases - A. Vaisman1 SQL: Queries, Programming, Triggers.
INFS614 - Lecture week 9 1 More on SQL Lecture Week 9 INFS 614, Fall 2008.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
The Relational Model These slides are based on the slides of your text book.
1 IT420: Database Management and Organization SQL - Data Manipulation Language 27 January 2006 Adina Crăiniceanu
Introduction to Databases Chapter 7: Data Access and Manipulation.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
CSC 411/511: DBMS Design Dr. Nan WangCSC411_L6_SQL(1) 1 SQL: Queries, Constraints, Triggers Chapter 5 – Part 1.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Unit 5/COMP3300/ SQL: Queries, Programming, Triggers Chapter 5.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
SQL: Queries, Programming, Triggers. Example Instances We will use these instances of the Sailors and Reserves relations in our examples. If the key for.
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.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Example Instances R1 S1 S2  We will use these instances of the Sailors and Reserves relations in our.
Introduction to SQL ; Christoph F. Eick & R. Ramakrishnan and J. Gehrke 1 Using SQL as a Query Language COSC 6340.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Constraints, Triggers Chapter 5.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
CMPT 258 Database Systems SQL Queries (Chapter 5).
1 SQL: Queries, Constraints, Triggers Chapter 5. 2 Overview: Features of SQL  Data definition language: used to create, destroy, and modify tables and.
1 SQL: Structured Query Language (‘Sequel’) Chapter 5.
1 SQL: The Query Language. 2 Example Instances R1 S1 S2 v We will use these instances of the Sailors and Reserves relations in our examples. v If the.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Basic SQL Queries.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture 8 Introduction to SQL Instructor: Chen Li.
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Basic SQL Queries Go over example queries, like 10 > ALL.
COP Introduction to Database Structures
01/31/11 SQL Examples Edited by John Shieh CS3754 Classnote #10.
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.
SQL The Query Language R & G - Chapter 5
SQL: Advanced Options, Updates and Views Lecturer: Dr Pavle Mogin
Lecture#7: Fun with SQL (Part 2)
Database Applications (15-415) SQL-Part II Lecture 9, February 04, 2018 Mohammad Hammoud.
The Relational Model Relational Data Model
CS 405G: Introduction to Database Systems
SQL: Structured Query Language
SQL: Queries, Programming, Triggers
CSC 453 Database Systems Lecture
SQL: The Query Language (Part III)
Presentation transcript:

DBMS Languages

Data Definition Language (DDL) Used to define the conceptual and internal schemas Includes constraint definition language (CDL) for describing conditions that database instances must satisfy Includes storage definition language (SDL) to influence layout of physical schema (some DBMSs) CREATE, ATER, DROP Data Manipulation Language (DML) Used to describe operations on the instances of a database Procedural DML (how) vs. declarative DML (what) SELECT, INSERT, UPDATE, DELETE Note, SQL includes a DML and a DDL in one! Host Language General-purpose programming language which lets users embed DML commands (data sublanguage) into their code

DML Commands: SELECT To find all 21 years old students, we can write: FROM -> predefined word used to specify the set from where you want to read the informations (ex. the table) WHERE -> adds a filter that will be applied to the set specified in the FROM clause SELECT -> selects the fields from the new formed set that the user wants to be returned (use * if you want to return all the fields from the set) To find just names and addresses, replace the first line: SELECT * FROM Students S WHERE S.age = 21 SELECT S.name, S.

Querying Multiple Relations What does the following query compute? Given the following instances of Students and Enrolled SELECT S.name, E.cid FROM Students S, Enrolled E WHERE S.sid=E.sid AND E.grade=10 Students sid name agegr Enrolled sidcid grade 1234Alg Alg DB DB29 S.nameE.cid JohnDB1 SmithAlg1 We get: Using two tables in the FROM clause, creates a cartesian product between the two tables -> Each row in the first table is paired with all the rows in the second table The resulting table will be stored into the memory and the filters from the WHERE clause will be applied on it After the filters are applied, from the resulting table, only the columns specified in the SELECT clause will be returned

Semantics of a Query A conceptual evaluation method for the previous query: 1. FROM clause: Compute cross-product of Students and Enrolled 2. WHERE clause: Check conditions, discard tuples that fail 3. SELECT clause: Delete unwanted fields Remember, this is conceptual. Actual evaluation will be much more efficient, but must produce the same answers.

Find students with at least one grade All the students are stored in the table Students The table Enrolled specifies every studentid with a grade registered at a courseid For a student to have at least one grade => its studentid should appear in the Enrolled table Questions: Would adding DISTINCT to this query make a difference? What is the effect of replacing S.sid by S.sname in the SELECT clause? Would adding DISTINCT to this variant of the query make a difference?

Expressions and Strings The working table will be the one specified by the FROM clause: Students table The filter from the WHERE clause will remove all of the rows from the table, that do not have in the field name, a name that starts and ends with the letter B, and it contains at least three letters (B + one arbitrary character + 0 or more characters + B) In the SELECT clause there are three fields returned:: first filed is the column age (int) from the table students, the second column is an user created column, age1, which will be created from substracting 5 from the age the third column will be another user created column, age2,which will be created from multiplying the age column with 2 Illustrates use of arithmetic expressions and string pattern matching: Find triples (of ages of students and two fields defined by expressions) for students whose names begin and end with B and contain at least three characters. AS and = are two ways to name fields in result. LIKE is used for string matching. `_’ stands for any one character and `%’ stands for 0 or more arbitrary characters.

Find sid of students with grades at courses with 4 or 5 credits  Both queries above produce the same results

Find sid of students with grades at courses with 4 or 5 credits (cont)  In the FROM clause the cross product will be created from the two tables Enrolled and Courses  In the WHERE clause only the rows thathave the same cid from both tables, will remain  To find the student ids that have grades only at courses with 4 and 5 credits, in the WHERE clause there is another filter added, that only selects the rows where the field credits is either 4 OR 5

Find sid of students with grades at courses with 4 or 5 credits (cont)  The first query will only return the student ids that are enrolled in courses with 4 credits  The second query will only return the student ids that are enrolled in courses with 5 credits  The UNION clause will take both returned sets from both queries, apply DISTINCT on the resulting set and return it as final  If applying UNION ALL instead of UNION, the resulting set will also contain duplicates => UNION ALL does not apply DISTINCT on the resulting set  The difference in execution speed comes from the fact UNION requires internal temporary table with index (to skip duplicate rows) while UNION ALL will create table without such index.  When applying UNION or UNION ALL on two or more sets, all returned sets should have the same number of fields and the every field should have the same name and datatype

Nested Queries

Join Queries

Outer Queries

Outer Queries (cont.)

Full Outer Join

Join Queries INNER JOIN FULL OUTER JOIN LEFT OUTER JOIN

Join Queries There are mainly three types of JOIN Inner: fetches data, that are present in both tables Only JOIN means INNER JOIN Outer: are of three types LEFT OUTER - - fetches data present only in left table & matching condition RIGHT OUTER - - fetches data present only in right table & matching condition FULL OUTER - - fetches data present any or both table (LEFT or RIGHT or FULL) OUTER JOIN can be written without writing "OUTER” Cross Join: joins everything to everything

Null Values Field values in a tuple are sometimes unknown (e.g., a rating has not been assigned) or inapplicable. SQL provides a special value null for such situations. The presence of null complicates many issues. E.g.: Special operators needed to check if value is/is not null. Is rating>8 true or false when rating is equal to null ? What about AND, OR and NOT connectives? We need a 3-valued logic (true, false and unknown ). Meaning of constructs must be defined carefully. (e.g., WHERE clause eliminates rows that don’t evaluate to true.) New operators (in particular outer joins ) possible/needed.

Aggregate Operators

GROUP BY and HAVING So far, we’ve applied aggregate operators to all (qualifying) tuples. Sometimes, we want to apply them to each of several groups of tuples. Consider: Find the age of the youngest student for each group. In general, we don’t know how many groups exist Suppose we know that rating values go from 110 to 119, we can write 10 queries that look like this (!):

GROUP BY/HAVING - Example Find the age of the youngest student with age  20 for each group with at least 2 such students Only S.gr and S.age are mentioned in the SELECT, GROUP BY or HAVING clauses; other attributes `unnecessary’. 2nd column of result is unnamed. (Use AS to name it.)

Find the number of enrolled students and the grade average for each course with 6 credits

sid name agegr Students sidcid grade 1234Alg Alg Alg Alg Alg Alg DB DB DB DB DB DB DB DB DB17 Enrolled

24 sid name agegr Students sidcid grade 1234Alg Alg DB DB DB17 Enrolled SELECTC.cid, COUNT(*)AS scount, AVG(grade)AS average FROM Students S, Enrolled E, Courses C WHERE S.sid=E.sid AND E.cid=C.cid AND C.credits=6 GROUP BY C.cid 6Databases2DB2 6Databases1DB1 7Algoritmics 1Alg1 6Databases1DB1 Algoritmics 1 cname 7Alg1 credits cid Courses

SELECTC.cid, COUNT(*)AS scount, AVG(grade)AS average FROM Students S, Enrolled E, Courses C WHERE S.sid=E.sid AND E.cid=C.cid AND C.credits=6 GROUP BY C.cid sid name agegr sidcid grade 1234DB DB DB17 cid cnamecredits DB1Databases16 DB2Databases26 DB1Databases16 DB2 scount DB1 cidaverage HAVING MAX(grade) = 10

Exercises (Use AdventureWorks2008R2): Download AdventureWorks2008R2_Database.zip from Use T-SQL: CREATE DATABASE AdventureWorks2008R2 ON (FILENAME = 'M:\Data\AdventureWorks2008R2_Data.mdf'), (FILENAME = 'L:\Tlogs\AdventureWorks2008R2_Log.ldf') FOR ATTACH; Or, attach the AdventureWorks database Unzip the database (mdf) file and log (ldf) file. From Microsoft SQL Server Management Studio, connect to a SQL Server instance. Right click Databases. Click Attach. Click the Add button. Locate the AdventureWorks database mdf file. For instance, AdventureWorks2008R2_Data.mdf. Click the OK button on the Locate Database Files dialog window. Click the OK button on the Attach Databases dialog window to attach the database.

Exercises (Use AdventureWorks2008R2): Return the products that have a product line of ‘R’ and are manufactured in les than 4 days (Use Production.Product table) Return the total sales and the discounts for each product (use Production.Product and Sales.SalesOrderDetail tables) Return the products where the product model is a ‘Classic Vest’ (use Production.Product and Production.ProductModel tables and inner queries with EXISTS or IN statements) Return the total of each sale (Use Sales.SalesOrderDetail and GROUP BY statement) Return the average price and the sum of year-to-date sales, group by the product id and the special offer id (use Sales.SalesOrderDetail table) Group the rows in SalesOrderDetail table by the id of the product and eliminate the products whose average order quantities are five or less (use HAVING statement) Group the SalesOrderDetail table by the id of the product and include only those groups of products that have orders totaling more than $ and whose average order quantities are less than 3 (use HAVING statement) Return the product models for which the maximum list price is more than twice the average for the model (Use Production.Product table and GROUP BY and HAVING (with inner query) statements)

CTE – Common Tables Expressions CTE provides alternative syntax for mastering nested queries, and also can be used for writing recursive queries – it is a temporary named result set that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. SQL Server supports two types of CTEs—recursive and nonrecursive. CTEs are defined by adding a WITH clause directly before your SELECT, INSERT, UPDATE or DELETE statement. The WITH clause can include one or more CTEs, separated by commas After you define your WITH clause with the necessary CTEs, you can then reference those CTEs as you would any other table. After you’ve run your statement, the CTE result set is not available to other statements. The structure: ;WITH cte_name(col_a, col_b, …, col_z) AS ( --query definition ), cte_name2(col_a, col_b, …, col_z) AS (--query definition ) SELECT col_a, col_b, …, col_z FROM cte_name JOIN cte_name2

CTE – Common Tables Expressions - Nonrecursive A nonrecursive CTE is one that does not reference itself within the CTE. Example : returns the total sales for each sales person (total sales grouped by salesperson ID) ;WITH cteTotalSales (SalesPersonID, NetSales) AS ( SELECT SalesPersonID, ROUND(SUM(SubTotal),2) FROM Sales.SalesOrderHeader WHERE SalesPersonID IS NOT NULL GROUP BY SalesPersonID ) SELECT sp.FirstName + ‘ ’ + sp.LastName AS FullName,sp.City + ‘,’ + StateProvinceNames AS Location,ts.NetSales FROM Sales.vSalesPerson AS sp INNER JOIN cteTotalSales AS ts ON sp.BussinessEntityID = ts.SalesPersonID ORDER BY ts.NetSales DESC

CTE – Common Tables Expressions - Recursive A recursive CTE is one that references itself within that CTE. The recursive CTE is useful when working with hierarchical data because the CTE continues to execute until the query returns the entire hierarchy. Note that a CTE created incorrectly could enter an infinite loop. To prevent this, you can include the MAXRECURSION hint in the OPTION clause of the primary SELECT, INSERT, UPDATE, DELETE A recursive CTE query must contain at least two members (statements), connected by the UNION ALL, UNION, INTERSECT, or EXCEPT operator -> All anchor members must precede the recursive members, and only the recursive members can reference the CTE itself. In addition, all members must return the same number of columns with corresponding data types.

CTE – Common Tables Expressions – Recursive (Example) WITH cteReports (EmpID, FirstName, LastName, MgrID, EmpLevel) AS ( SELECT EmployeeID, FirstName, LastName, ManagerID, 1 FROM Employees WHERE ManagerID IS NULL UNION ALL SELECT e.EmployeeID, e.FirstName, e.LastName, e.ManagerID,r.EmpLevel + 1 FROM Employees e INNER JOIN cteReports r ON e.ManagerID = r.EmpID ) SELECT FirstName + ' ' + LastName AS FullName, EmpLevel, (SELECT FirstName + ' ' + LastName FROM Employees WHERE EmployeeID = cteReports.MgrID) AS Manager FROM cteReports ORDER BY EmpLevel, MgrID ORDER BY ts.NetSales DESC

CTE Exercises (Use AdventureWorks2008R2): For each sales representative (SalesPersonID), find the total number of sales orders per year (table Sales.SalesOrderHeader – SalesPersonID, SalesOrderID, OderDate) Find all the managers and all the employees that are reporting to them. The number of levels that are returned is limited to only two. Basically, only return the employees that are reporting directly to the manager. (Create an Employees table – id, employees info and managerid that will be null in case of a manager)

Adding, Deleting and Updating Tuples INSERT INTO Students (sid, name, , age, gr) VALUES (53688, ‘Smith’, 18, 311) DELETE FROM Students S WHERE S.name = ‘Smith’ Can modify the columns values using: UPDATE Students S SET S.age=S.age+1 WHERE S.sid = Can delete all tuples satisfying some condition