Database design process

Similar documents
Database Technology. Topic 3: SQL. Olaf Hartig.

Overview Relational data model

Database Technology. Topic 2: Relational Databases and SQL. Olaf Hartig.

Announcement5 SQL5. Create%and%drop%table5. Basic%SFW%query5. Reading%a%table5. TDDD37%% Database%technology% SQL5

SQL STRUCTURED QUERY LANGUAGE

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

Part 1 on Table Function

CIS611 Lab Assignment 1 SS Chung

Session Active Databases (2+3 of 3)

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

COSC344 Database Theory and Applications. σ a= c (P) S. Lecture 4 Relational algebra. π A, P X Q. COSC344 Lecture 4 1

ECE 650 Systems Programming & Engineering. Spring 2018

Advanced Databases. Winter Term 2012/13. Prof. Dr. Dietmar Seipel University of Würzburg. Advanced Databases Winter Term 2012/13

Chapter 8 SQL-99: Schema Definition, Basic Constraints, and Queries

A taxonomy of SQL queries Learning Plan

Some different database system architectures. (a) Shared nothing architecture.

Slides by: Ms. Shree Jaswal

SQL. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

SQL-99: Schema Definition, Basic Constraints, and Queries. Create, drop, alter Features Added in SQL2 and SQL-99

Chapter 8. Joined Relations. Joined Relations. SQL-99: Schema Definition, Basic Constraints, and Queries

Database Technology. Topic 3: SQL. Structured Query Language. Creating Tables SQL DDL. Creating Tables (Example) Modifying Table Definitions

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Query 2: Pnumber Dnum Lname Address Bdate 10 4 Wallace 291 Berry, Bellaire, TX Wallace 291 Berry, Bellaire, TX

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

COSC Assignment 2

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Translation of ER-diagram into Relational Schema. Dr. Sunnie S. Chung CIS430/530

Data Definition Language (DDL)

ECE 650 Systems Programming & Engineering. Spring 2018

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

L130 - DATABASE MANAGEMENT SYSTEMS LAB CYCLE-1 1) Create a table STUDENT with appropriate data types and perform the following queries.

SQL (Structured Query Language) Truong Tuan Anh CSE-HCMUT

Integrity Coded Relational Databases (ICRDB) - Protecting Data Integrity in Clouds

CSIE30600 Database Systems Basic SQL 2. Outline

SQL Introduction. CS 377: Database Systems

Data Manipulation Language (DML)

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Dr. Anis Koubaa. Advanced Databases SE487. Prince Sultan University

Querying a Relational Database COMPANY database For Lab4, you use the Company database that you built in Lab2 and used for Lab3

CS 348 Introduction to Database Management Assignment 2

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Course Notes on Relational Algebra

Practical Project Report

SQL: A COMMERCIAL DATABASE LANGUAGE. Data Change Statements,

Relational Algebra Part I. CS 377: Database Systems

Basic SQL II. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

Chapter 8: Relational Algebra

Structured Query Language (SQL)

DEPARTMENT DNAME DNUMBER MGRNAME MGRSTARTDATE

Relational Calculus: 1

Ref1 for STUDENT RECORD DB: Ref2 for COMPANY DB:

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

More SQL: Complex Queries, Triggers, Views, and Schema Modification

DATABASE CONCEPTS. Dr. Awad Khalil Computer Science & Engineering Department AUC

SQL: Advanced Queries, Assertions, Triggers, and Views. Copyright 2012 Ramez Elmasri and Shamkant B. Navathe

More SQL: Complex Queries, Triggers, Views, and Schema Modification

CS 338 Basic SQL Part II

DBMS LAB SESSION PAVANKUMAR MP

More SQL: Complex Queries, Triggers, Views, and Schema Modification

In Chapters 3 through 6, we presented various aspects

CSC 742 Database Management Systems

Chapter 3. Algorithms for Query Processing and Optimization

RELATIONAL DATA MODEL

Relational Algebra 1

Algorithms for Query Processing and Optimization. 0. Introduction to Query Processing (1)

Chapter 6: RELATIONAL DATA MODEL AND RELATIONAL ALGEBRA

Hand in: the database picture (png or jpeg or ) for question 2, the queries (as SQL statements) for question 4

Guides for Installing MS SQL Server and Creating Your First Database. Please see more guidelines on installing procedure on the class webpage

The Relational Data Model and Relational Database Constraints

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

CS5300 Database Systems

Relational Algebra & Calculus. CS 377: Database Systems

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Advanced Databases (SE487) Prince Sultan University College of Computer and Information Sciences

Chapter 19 Query Optimization

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Relational Model. CS 377: Database Systems

Basic SQL. Basic SQL. Basic SQL

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

SQL- Updates, Asser0ons and Views

Chapter 6 The Relational Algebra and Calculus

Chapter 8: The Relational Algebra and The Relational Calculus

This chapter discusses how to design a relational

PES Institute of Technology Bangalore South Campus (1 K.M before Electronic City,Bangalore ) Department of MCA. Solution Set - Test-II

UNIVERSITY OF TORONTO MIDTERM TEST SUMMER 2017 CSC343H Introduction to Databases Instructor Tamanna Chhabra Duration 120 min No aids allowed

Wentworth Institute of Technology COMP570 Database Applications Fall 2014 Derbinsky. Physical Tuning. Lecture 10. Physical Tuning

Chapter 18 Strategies for Query Processing. We focus this discussion w.r.t RDBMS, however, they are applicable to OODBS.

Course Notes on From Entity-Relationship Schemas to Relational Schemas

The Relational Algebra and Calculus. Copyright 2013 Ramez Elmasri and Shamkant B. Navathe

SQL Queries. COSC 304 Introduction to Database Systems SQL. Example Relations. SQL and Relational Algebra. Example Relation Instances

SQL: A COMMERCIAL DATABASE LANGUAGE. Complex Constraints

Introduction to Query Processing and Query Optimization Techniques. Copyright 2011 Ramez Elmasri and Shamkant Navathe

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

COSC 304 Introduction to Database Systems SQL. Dr. Ramon Lawrence University of British Columbia Okanagan

Key Points. COSC 122 Computer Fluency. Databases. What is a database? Databases in the Real-World DBMS. Database System Approach

Homework #4 1. Suppose that each of the following Update operations is applied directly to the database state shown in Figure 5.6.

Transcription:

Database technology Lecture 2: Relational databases and SQL Jose M. Peña jose.m.pena@liu.se Database design process 1

Relational model concepts... Attributes... EMPLOYEE FNAME M LNAME SSN BDATE ADDRESS S SALARY SUPERSSN DNO Tuples... Ramesh K Narayan 666884444 1962-09-15 M 38000 888665555 5 Joyce A English 453453453 1972-07-31 F 25000 888665555 5 Ahmad V Jabbar 987987987 1969-03-29 M 25000 888665555 4 James E Borg 888665555 1937-11-10 M 55000 null 1 Relation: Set of tuples, i.e. no duplicates are allowed. Database: Collection of relations. EMPLOYEE ( FNAME, M, LNAME, SSN, BDATE, ADDRESS, S, SALARY, SUPERSSN, DNO) Relation schema 3 Relational model concepts Domain String shorter than 30 chars yyyy-mm-dd Character M or F Integer 400 < x < 8000 EMPLOYEE FNAME M LNAME SSN BDATE ADDRESS S SALARY SUPERSSN DNO Ramesh K Narayan 666884444 1962-09-15 M 38000 888665555 5 Joyce Null English 453453453 1972-07-31 F 38000 888665555 5 Ahmad V Jabbar 987987987 1969-03-29 M 25000 888665555 4 James Null Borg 888665555 1937-11-10 M 55000 Null 1 NULL value 4 2

Relational model constraints EMPLOYEE FNAME M LNAME SSN BDATE ADDRESS S SALARY SUPERSSN DNO Ramesh K Narayan 666884444 1962-09-15 M 38000 888665555 5 Joyce Null English 453453453 1972-07-31 F 38000 888665555 5 Ahmad V Jabbar 987987987 1969-03-29 M 25000 888665555 4 James Null Borg 888665555 1937-11-10 M 55000 Null 1 Entity integrity constraint 5 Relational model constraints Foreign keys EMPLOYEE FNAME M LNAME SSN BDATE ADDRESS S SALARY SUPERSSN DNO Ramesh K Narayan 666884444 1962-09-15 M 38000 888665555 5 Joyce A English 453453453 1972-07-31 F 25000 888665555 5 Ahmad V Jabbar 987987987 1969-03-29 M 25000 888665555 4 James E Borg 888665555 1937-11-10 M 55000 Null 1 Referential integrity constraint DEPARTMENT DNAME DNUMBER MGRSSN MGRSTARTDATE Research 5 666884444 1988-05-22 Administration 4 987987987 1995-01-01 Headquarters 1 888665555 1981-06-19 6 3

Relational model constraints (Atomic) domain (or NULL). Key. Entity integrity: A PK cannot take NULL values. Referential integrity: A FK in a relation can only refer to the PK of another relation, and the domains of the FK and PK must coincide, and the FK takes NULL value or values that exist for the PK. 7 SQL relational data model SQL relation table attribute column tuple row Used in many DBMSs. Declarative (what data to get, not how). DDL (Data Definition Language) CREATE, ALTER, DROP Queries SELECT DML (Data Manipulation Language) INSERT, DELETE, UPDATE 8 4

COMPANY schema EMPLOYEE (FNAME, MINIT, LNAME, SSN, BDATE, ADDRESS, SEX, SALARY, SUPERSSN, DNO) DEPT_LOCATIONS (DNUMBER, DLOCATION) DEPARTMENT (DNAME, DNUMBER, MGRSSN, MGRSTARTDATE) WORKS_ON (ESSN, PNO, HOURS) PROJECT (PNAME, PNUMBER, PLOCATION, DNUM) DEPENDENT (ESSN, DEPENDENT-NAME, SEX, BDATE, RELATIONSHIP) 9 Create tables CREATE TABLE <tablename> ( <colname> <datatype> [<constraint>],, [<constraint>], ); Data types: Integer, decimal, number, varchar,char, etc. Constraints: Not null, primary key, foreign key, unique, etc. 10 5

Create tables CREATE TABLE WORKS_ON ( ESSN integer, PNO integer, HOURS decimal(3,1), constraint pk_workson primary key (ESSN, PNO), constraint fk_works_emp FOREIGN KEY (ESSN) references EMPLOYEE(SSN), ); constraint fk_works_proj FOREIGN KEY (PNO) references PROJECT(PNUMBER) 11 Modify tables Change the definition of a table: Add, delete and modify columns and constraints. ALTER TABLE EMPLOYEE ADD JOB VARCHAR(12); ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS CASCADE; ALTER TABLE WORKS_ON DROP FOREIGN KEY fk_works_emp; ALTER TABLE WORKS_ON ADD CONSTRAINT fk_works_emp FOREIGN KEY (ESSN) REFERENCES EMPLOYEE(SSN); Delete a table and its definition DROP TABLE EMPLOYEE; 12 6

Query tables SELECT <attribute-list> FROM <table-list> WHERE <condition>; Attribute list: A 1,, A r Attributes whose values are required. Table list: R 1,, R k Relations to be queried Condition: Boolean expression It identifies the tuples that should be retrieved. It may include comparison operators(=, <>, >, >=, etc.) and/or logical operators (and, or, not). 13 Simple query List the SSN for all employees. SELECT SSN FROM EMPLOYEE; SSN 123456789 333445555 999887777 987654321 666884444 453453453 987987987 888665555 14 7

Use of * List all information about the employees of department 5. SELECT FNAME, MINIT, LNAME,SSN, BDATE, ADDRESS, SEX, SALARY, SUPERSSN, DNO FROM EMPLOYEE WHERE DNO = 5; or SELECT * FROM EMPLOYEE WHERE DNO = 5; Comparison operators {=, <>, >, =>, etc.} 15 Simple query List the last name, birth date and address for all employees whose name is `Alicia J. Zelaya. SELECT LNAME, BDATE, ADDRESS FROM EMPLOYEE WHERE FNAME = Alicia AND MINIT = J AND LNAME = Zelaya ; Logical operators {and, or, not} LNAME BDATE ADDRESS Zelaya 1968-07-19 3321 Castle, Spring, TX 16 8

Pattern matching List the birth date and address for all employees whose last name contains the substring aya. SELECT BDATE, ADDRESS FROM EMPLOYEE WHERE LNAME LIKE %aya% ; LNAME BDATE ADDRESS LIKE comparison operator % replaces 0 or more characters _ replaces a single character Zelaya 1968-07-19 3321 Castle, Spring, TX Narayan 1962-09-15 975 Fire Oak, Humble, TX 17 Tables as sets List all salaries. SELECT SALARY FROM EMPLOYEE; SALARY 30000 40000 25000 43000 38000 25000 25000 55000 18 9

Tables as sets SQL considers a table as a multi-set (bag), i.e. tuples can occur more than once in a table. This is different in a relational model. Why? Removing duplicates is expensive. User may want information about duplicates. Aggregation operators. 19 Example List all salaries. SELECT SALARY FROM EMPLOYEE; SALARY 30000 40000 25000 43000 38000 25000 25000 55000 List all salaries without duplicates. SELECT DISTINCT SALARY FROM EMPLOYEE; SALARY 30000 40000 25000 43000 38000 55000 20 10

Set operations Queries can be combined by set operations: UNION, INTERSECT, EXCEPT (MySQL only supports UNION) Retrieve the first names of all people in the database. SELECT FNAME FROM EMPLOYEE E D UNION SELECT DEPENDENT_NAME FROM DEPENDENT; Which department managers have dependents? Show their SSN. SELECT MGRSSN FROM DEPARTMENT INTERSECT SELECT ESSN FROM DEPENDENT; Duplicate tuples are removed. M DE 21 Ambiguous names: Aliasing What if the same attribute name is used in different relations? No alias Whole name Alias SELECT NAME, NAME FROM EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER; SELECT EMPLOYEE.NAME, DEPARTMENT.NAME FROM EMPLOYEE, DEPARTMENT WHERE EMPLOYEE.DNO=DEPARTMENT.DNUMBER; SELECT E.NAME, D.NAME FROM EMPLOYEE E, DEPARTMENT D WHERE E.DNO=D.DNUMBER; 22 11

Join: Cartesian product List all employees and the names of their departments. EMPLOYEE SELECT LNAME, DNAME FROM EMPLOYEE, DEPARTMENT; LNAME Smith Wong Zelaya Wallace Narayan English Jabbar Borg DNO 5 5 4 4 5 5 4 1 DEPARTMENT DNAME Research Administration headquarters DNUM 5 4 1 LNAME DNAME Smith Research Wong Research Zelaya Research Wallace Research Narayan Research English Research Jabbar Research Borg Research Smith Administration Wong Administration Zelaya Administration Wallace Administration Narayan Administration English Administration Jabbar Administration Borg Administration Smith Headquarters Wong Headquarters Zelaya Headquarters Wallace Headquarters Narayan Headquarters English Headquarters Jabbar Headquarters Borg Headquarters 23 Join: Equijoin List all employees and the names of their departments. SELECT LNAME, DNAME FROM EMPLOYEE, DEPARTMENT WHERE DNO = DNUMBER; Thetajoin {=, <>, >, =>, <=,!=} Equijoin Cartesian product Foreign key in EMPLOYEE Primary key in DEPARTMENT LNAME DNO DNAME DNUMBER Smith 5 Research 5 Wong 5 Research 5 Zelaya 4 Research 5 Wallace 4 Research 5 Narayan 5 Research 5 English 5 Research 5 Jabbar 4 Research 5 Borg 1 Research 5 Smith 5 Administration 4 Wong 5 Administration 4 Zelaya 4 Administration 4 Wallace 4 Administration 4 Narayan 5 Administration 4 English 5 Administration 4 Jabbar 4 Administration 4 Borg 1 Administration 4 Smith 5 Headquarters 1 Wong 5 Headquarters 1 Zelaya 4 Headquarters 1 Wallace 4 Headquarters 1 Narayan 5 Headquarters 1 English 5 Headquarters 1 Jabbar 4 Headquarters 1 Borg 1 Headquarters 1 24 12

Join: Self-join List the last name for all employees together with the last names of their bosses. SELECT E.LNAME Employee, S. LNAME Boss FROM EMPLOYEE E, EMPLOYEE S WHERE E.SUPERSSN = S.SSN; Employee Boss Smith Wong Wong Borg Zelaya Wallace Wallace Borg Narayan Wong English Wong Jabbar Wallace 25 Join: Inner join List the last name for all employees together with the last names of their bosses. SELECT E.LNAME Employee, S.LNAME Boss FROM EMPLOYEE E, EMPLOYEE S WHERE E.SUPERSSN = S.SSN; SELECT E.LNAME Employee, S.LNAME Boss FROM EMPLOYEE E INNER JOIN EMPLOYEE S ON E.SUPERSSN = S.SSN; 26 13

Join: Outer join List the last name for all employees and, if available, show the last names of their bosses. SELECT E.LNAME Employee, S. LNAME Boss FROM EMPLOYEE E LEFT JOIN EMPLOYEE S ON E.SUPERSSN = S.SSN; LEFT JOIN, RIGHT JOIN, FULL JOIN Employee Boss Smith Wong Wong Borg Zelaya Wallace Wallace Borg Narayan Wong English Wong Jabbar Wallace Borg NULL 27 Joins revisited Cartesian product SELECT * FROM a, b; A2 A1 B1 B2 A 100 100 W B null 100 W C 300 100 W D null 100 W A 100 200 X B null 200 X C 300 200 X D null 200 X A 100 null Y B null null Y C 300 null Y D null null Y A 100 null Z B null null Z C 300 null Z D null null Z A A1 A2 100 A null B 300 C null D B B1 B2 100 W 200 X null null Equijoin, natural join, inner join SELECT * from A, B WHERE A1=B1; A2 A1 B1 B2 A 100 100 W Thetajoin SELECT * from A, B WHERE A1>B1; A2 A1 B1 B2 C 300 100 W C 300 200 X Y Z 28 14

Outer joins revisited Right outer join SELECT * FROM A RIGHT JOIN B on A1=B1; A2 A1 B1 B2 A 100 100 W null null 200 X null null null Y null null null Z Left outer join SELECT * FROM A LEFT JOIN B on A1=B1; A2 A1 B1 B2 A 100 100 W C 300 null null B null null null D null null null A B A1 A2 B1 B2 100 A 100 W null B 200 X 300 C null Y null D null Z Full outer join (union of right+left) SELECT * FROM A FULL JOIN b on A1=B1; A2 A1 B1 B2 A 100 100 W null null 200 X null null null Y null null null Z C 300 null null B null null null D null null null 29 Subqueries List all employees that do not have any project assignment with more than 10 hours. SELECT LNAME FROM EMPLOYEE, WORKS_ON WHERE SSN = ESSN AND HOURS <= 10.0; {>, >=, <, <=, <>} + {ANY, SOME, ALL} SELECT LNAME FROM EMPLOYEE WHERE SSN NOT IN (SELECT ESSN FROM WORKS_ON WHERE HOURS > 10.0); Or EXISTS SELECT LNAME FROM EMPLOYEE WHERE NOT EXISTS (SELECT * FROM WORKS_ON WHERE SSN = ESSN AND HOURS > 10.0); 30 15

Extended SELECT syntax SELECT <attribute-list and function-list> FROM <table-list> [ WHERE <condition> ] [ GROUP BY <grouping attribute-list>] [ HAVING <group condition> ] [ ORDER BY <attribute-list> ]; 31 Aggregate functions Built-in functions: AVG(), SUM(), MIN(), MAX(), COUNT() They appear only in SELECT and HAVING clauses. NULL values are not considered in the computations. List the total number of employees. SELECT COUNT(*) FROM EMPLOYEE; AVG() 50 50 100 100 Null 0 75 50 32 16

Grouping Used to apply an aggregate function to subgroups of tuples in a relation. GROUP BY: Grouping attributes. HAVING: Condition that a group has to satisfy. List, for each department with more than two employees, the department number, the number of employees and the average salary. SELECT DNO, COUNT(*), AVG(SALARY) FROM EMPLOYEE GROUP BY DNO HAVING COUNT(*) > 2; DNO COUNT(*) AVG(SALARY) 5 4 33250 4 3 31000 1 1 55000 33 Sort query results Show the department names and their locations in alphabetical order. SELECT DNAME, DLOCATION FROM DEPARTMENT D, DEPT_LOCATIONS DL WHERE D.DNUMBER = DL.DNUMBER ORDER BY DNAME ASC, DLOCATION DESC; DNAME DLOCATION Administration Stafford Headquarters Houston Research Sugarland Research Houston Research Bellaire 34 17

Null values List all employees that do not have a boss. SELECT FNAME, LNAME FROM EMPLOYEE WHERE SUPERSSN IS NULL; SUPERSSN = NULL and SUPERSSN <> NULL will not return any matching tuples, because NULL is incomparable to any value, including another NULL. 35 Insert data INSERT INTO <table> (<attr>, ) VALUES ( <val>, ) ; INSERT INTO <table> (<attr>, ) <subquery> ; Store information about how many hours an employee works for the project 1' into WORKS_ON. INSERT INTO WORKS_ON VALUES (123456789, 1, 32.5); Integrity constraint! Referential integrity constraint! 36 18

Update data UPDATE <table> SET <attr> = <val>, WHERE <condition> ; UPDATE <table> SET (<attr>,.) = ( <subquery> ) WHERE <condition> ; Give all employees in the Research department a 10% raise in salary. Integrity constraint! Referential integrity constraint! UPDATE EMPLOYEE SET SALARY = SALARY*1.1 WHERE DNO IN (SELECT DNUMBER FROM DEPARTMENT WHERE DNAME = Research ); 37 Delete data DELETE FROM <table> WHERE <condition> ; Delete the employees having the last name Borg from the EMPLOYEE table. DELETE FROM EMPLOYEE WHERE LNAME = Borg ; Foreign key EMPLOYEE FNAME M LNAME SSN DEPARTMENT DNAME DNUMBER MGRSSN Ramesh K Narayan 666884444 Research 5 333445555 Joyce A English 453453453 Administration 4 987654321 Ahmad V Jabbar 987987987 Headquarters 1 888665555 James E Borg 888665555 ON DELETE SET NUL / DEFAULT / CASCADE? Referential integrity constraint! 38 19

Views A virtual table derived from another (possibly virtual) tables, i.e. always up-to-date. CREATE VIEW dept_view AS SELECT DNO, COUNT(*), AVG(SALARY) FROM EMPLOYEE GROUP BY DNO; Why? Simplify query commands. Provide data security. Enhance programming productivity. Update problems. 39 20