莊裕澤資料庫基本概念 1 Yuh-Jzer Joung 莊 裕 澤 Dept. of Information Management National Taiwan University February, 2002 資料庫管理與資訊科技.

Slides:



Advertisements
Similar presentations
COMPANY schema EMPLOYEE
Advertisements

Basic Queries. 2 Retrieval Queries in SQL SQL has one basic statement for retrieving information from a database; the SELECT statement This is not the.
SQL Query Slides Sharif University Of Technology Database Systems CE 384 Prepared By: Babak Bagheri Hariri
Your Logo Fundamentals of Database Systems Fourth Edition El Masri & Navathe Instructor: Mr. Ahmed Al Astal Chapter 8 (Cont.) SQL-99: Schema Definition,
Displaying Data from Multiple Tables. EMPNO DEPTNO LOC NEW YORK CHICAGO NEW YORK DALLAS
Database technology Lecture 2: Relational databases and SQL
Company Database. CREATE TABLE DEPARMENT ( DNAME VARCHAR(10) NOT NULL, DNUMBER INTEGER NOT NULL, MGRSSN CHAR(9), MGRSTARTDATE CHAR(9), PRIMARY KEY (DNUMBER),
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1.
1 CSE 480: Database Systems Lecture 10: SQL - DML Reference: Read Chapter 4 of the textbook.
METU Department of Computer Eng Ceng 302 Introduction to DBMS SQL-99: Schema Definition, Basic Constraints, and Queries by Pinar Senkul resources: mostly.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: SchemaDefinition, Constraints, and Queries and Views.
Database Systems More SQL Database Design -- More SQL1.
Database Systems Chapter 8 ITM 354. Data Definition, Constraints, and Schema Changes Used in SQL to Create, Drop, and Alter the descriptions of the tables.
More SQL: Complex Queries, Triggers, Views, and Schema Modification 1.
관계 연산자 & SQL. Selection SELECT * FROM r WHERE A=B AND D>5.
SQL The relational DB Standard CS-450 Dr. Ali Obaidi.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Slide 8- 1 CREATE/DROP/ALTER TABLE Data types : char, varchar, decimal, date CREATE TABLE DEPARTMENT.
Al-Imam University Girls Education Center Collage of Computer Science 1 ST Semester, 1432/1433H Chapter 8 Part 4 SQL-99 Schema Definition, Constraints,
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
1 CS 430 Database Theory Winter 2005 Lecture 12: SQL DML - SELECT.
1 CSE 480: Database Systems Lecture 11: SQL. 2 SQL Query SELECT FROM WHERE –In MySQL, FROM and WHERE clauses are optional –Example:
SQL: Schema Definition, Constraints, & Queries and Views.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 Lecture # 11 July 12,2012 More SQL: Complex Queries, Triggers,
Relational Algebra - Chapter (7th ed )
 Employee (fname, minit, lname, ssn, bdate, address, sex, salary, superssn, dno)  Department (dname, dnumber, mgrssn, mgrstartdate) 
Database Management Systems. NESTING OF QUERIES  Some queries require that existing values in the database be retrieved and then used in a comparison.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Chapter 8 SQL- The Relational Database Standard 8-1.
Announcements Written Homework 1 due Friday –If you have fourth edition make sure you do the right problems Program 3 out today, due next Friday Nov 10.
IS 230Lecture 6Slide 1 Lecture 7 Advanced SQL Introduction to Database Systems IS 230 This is the instructor’s notes and student has to read the textbook.
Chapter 3 MORE SQL Copyright © 2004 Pearson Education, Inc.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
SQL Chapter 8. SQL or SEQUEL - (Structured English Query Language) Based on relational algebra Developed in 1970's released in early 1980's Standardized.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Chapter 7 SQL HUANG XUEHUA. AGGREGATE FUNCTIONS Include COUNT, SUM, MAX, MIN, and AVG Query 15: Find the maximum salary, the minimum salary, and the.
Chapter 8 Part 2 SQL-99 Schema Definition, Constraints, Queries, and Views.
Slide 8- 1 THE HAVING-CLAUSE Provides a condition on the summary information Sometimes we want to retrieve the values of these functions for only those.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Introduction to Database Systems
ISC321 Database Systems I Chapter 5: SQL: Advanced Queries, Assertions, Triggers, and Views Fall 2015 Dr. Abdullah Almutairi.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 (SQL Basics) Lecture # 9 July 7,2012.
Copyright © 2004 Pearson Education, Inc. Schema Definition, Basic Constraints, and Queries.
SQL- Updates, Assertions and Views. Data Definition, Constraints, and Schema Changes Used to CREATE, DROP, and ALTER the descriptions of the tables (relations)
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
CS580 Advanced Database Topics Chapter 8 SQL Irena Pevac.
1 Chapter (8) SQL The Relational Database Standard Objectives Presenting the main features of SQL SQL is the Structured Query Language SQL commands SQL.
The SQL Database Grammar
CS580 Advanced Database Topics
SQL: SchemaDefinition, Constraints, and Queries and Views
Chapter 4 Basic SQL.
6/22/2018.
CS580 Advanced Database Topics
Retrieval Queries in SQL(DML)
Subject Name: DATA BASE MANAGEMENT SYSTEM Subject Code: 10cs54
More SQL: Complex Queries, Triggers, Views, and Schema Modification
376a. Database Design Dept. of Computer Science Vassar College
11/9/2018.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Session - 6 Sequence - 2 SQL: The Structured Query Language:
Chapter 4 & 5 Basic SQL More SQL
SQL (more).
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Structured Query Language (3)
SQL-99: Schema Definition, Constraints, and Queries and Views
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
SQL Updating Database Contents Presented by: Dr. Samir Tartir
SQL Grouping, Ordering & Arithmetics Presented by: Dr. Samir Tartir
Presentation transcript:

莊裕澤資料庫基本概念 1 Yuh-Jzer Joung 莊 裕 澤 Dept. of Information Management National Taiwan University February, 2002 資料庫管理與資訊科技

莊裕澤資料庫基本概念 2 課程進 度 3/1 課程介紹、資料庫概論 3/8 資料庫設計與管理 (I) 3/15 資料庫設計與管理 (II) 3/22 關聯式查資料庫詢語言 SQL (I) 3/29 關聯式查資料庫詢語言 SQL (II) 4/12 網路基本介紹 4/19 全球資訊網 4/26 電子商務

莊裕澤資料庫基本概念 3 Data definition in SQL SQL uses table, row and column for relation, tuple and attribute, respectively. SQL commands for data definition: CREATE, ALTER, DROP SQL schema, identified by a schema name, includes an authorization identifier and descriptors for each element

莊裕澤資料庫基本概念 4 Data types and DROP commands Data types in SQL: numeric, character-string, bit-string, date, time, and interval DROP commands: DROP SCHEMA, DROP TABLE Drop behavior options: CASCADE, RESTRICT DROP SCHEMA COMPANY CASCADE; DROP TABLE DEPENDENT CASCADE; RESTRICT OPTIONS:  A schema is dropped only if it has no elements in it.  A table is dropped only if it is not referenced in any constraints.

莊裕澤資料庫基本概念 5 ALTER TABLE COMMAND Possible alter table actions: Adding or dropping a column, changing column definition, adding or dropping table constraints. ALTER TABLE COMPANY.EMPLOYEE ADD JOB VARCHAR(12); ALTER TABLE COMPANY.EMPLOYEE DROP ADDRESS CASCADE; ALTER TABLE COMPANY.DEPARTMENT ALTER MGRSSN DROP DEFAULT; ALTER TABLE COMPANY.EMPLOYEE DROP CONSTRAINTS EMPSUPERFK CASCADE;

莊裕澤資料庫基本概念 6 Queries in SQL SQL allows a table to have two or more tuples that are identical in all their attribute values. Hence, SQL table is not a set of tuples,rather it is a multiset (bag). The basic form of the SELECT statement SELECT FROM WHERE

莊裕澤資料庫基本概念 7 Query example Retrieve the birthdate and address of the employee whose name is ‘John B. Smith’. SELECT BDATE, ADDRESS FROM EMPLOYEE WHERE FNAME=‘John’ AND MINIT= ‘B’ AND LNAME=‘Smith’

莊裕澤資料庫基本概念 8 Ambiguous attribute names and aliasing (renaming) SELECT EMPLOYEE.NAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DEPARTMENT.NAME=‘Research’ AND DEPARTMENT.DNUMBER= EMPLOYEE.DNUMBER Aliasing SELECT E.NAME, E.ADDRESS FROM EMPLOYEE E, DEPARTMENT D WHERE D.NAME=‘Research’ AND D.DNUMBER=E.DNUMBER

莊裕澤資料庫基本概念 9 Unspecified WHERE and use of ‘*’ A missing WHERE indicates no condition on tuple selection. SELECT SSN, DNAME FROM EMPLOYEE, DEPARTMENT ‘*’ stands for all the attributes. SELECT * FROM EMPLOYEE, DEPARTMENT WHERE DNAME=‘Research’ AND DNO=DNUMBER

莊裕澤資料庫基本概念 10 Tables as sets in SQL DISTINCT in SELECT can be used to eliminate duplicate tuple. SELECT SALARY FROM EMPLOYEE SELECT DISTINCT SALARY FROM EMPLOYEE Set operations: UNION, EXCEPT, INTERSECT Duplicate tuples are eliminated from the result by using those set operations.

莊裕澤資料庫基本概念 11 Substring comparisons, arithmetic operators and ordering Retrieve all employees whose address is in Houston, TX. SELECT FNAME, LNAME FROM EMPLOYEE WHERE ADDRESS LIKE ‘%HOUSTON,TX%’ Show the resulting salaries if every employee working on the ‘Project X’ project is given a 10 percent raise. SELECT FNAME, LNAME, 1.1*SALARY FROM EMPLOYEE, WORKS_ON, PROJECT WHERE SSN=ESSN AND PNO=PNUMBER AND PNAME=‘Project X’;

莊裕澤資料庫基本概念 12 Substring comparisons, arithmetic operators and ordering (cont.) Retrieve a list of employees and the projects they are working on, ordered by department and, within each department, ordered alphabetically by last name, first name. SELECT DNAME, LNAME, FNAME, PNAME FROM DEPARTMENT, EMPLOYEE, WORKS_ON, PROJECT WHERE DNUMBER=DNO AND SSN=ESSN AND PNO=PNUMBER ORDER BY DNAME, LNAME, FNAME

莊裕澤資料庫基本概念 13 Set query Make a list of all project numbers for projects that involve an employee whose last name is ‘Smith’, either as a worker or as a manager of the department that controls the project. (SELECT PNUMBER FROM PROJECT,DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN AND LNAME=‘Smith’) UNION (SELECT PNUMBER FROM PROJECT, WORKS_ON, EMPLOYEE WHERE PNUMBER=PNO AND ESSN=SSN AND LNAME=‘Smith’)

莊裕澤資料庫基本概念 14 Set operators Set comparisons: IN, =ANY, =SOME, CONTAINS Other operators can be combined with ANY (SOME): >,>,.

莊裕澤資料庫基本概念 15 Nested queries Retrieve the name of each employee who has a dependent with the same first name and sex as the employee. SELECT E.FNAME, E.LNAME FROM EMPLOYEE E WHERE E.SSN IN (SELECT ESSN FROM DEPENDENT WHERE ESSN=E.SSN AND E.FNAME=DEPENDENT_NAME AND SEX=E.SEX)

莊裕澤資料庫基本概念 16 Nested queries (cont.) In general, a nested query using = or IN can be always be expressed as a single block query. SELECT E.FNAME, E.LNAME FROM EMPLOYEE E, DEPARTMENT D WHERE E.SSN=D.ESSN AND E.SEX=D.SEX AND E.FNAME=D.DEPENDENT_NAME

莊裕澤資料庫基本概念 17 Nested queries (cont.) Retrieve the name of employee whose salary > the salary of the employees in department 5. SELECT FNAME, LNAME FROM EMPLOYEE WHERE SALARY > ALL (SELECT SALARY FROM EMPLOYEE WHERE DNO=5)

莊裕澤資料庫基本概念 18 Nested queries (cont.) Retrieve the name of each employee who works on all the projects controlled by department 5. SELECT FNAME, LNAME FROM EMPLOYEE WHERE ((SELECT PNO FROM WORKS_ON WHERE SSN=ESSN) CONTAINS (SELECT PNUMBER FROM PROJECT WHERE DNUM=5))

莊裕澤資料庫基本概念 19 EXISTS List the names of managers who have at least one dependent. SELECT FNAME, LNAME FROM EMPLOYEE WHERE EXISTS (SELECT * FROM DEPENDENT WHERE SSN=ESSN) AND EXISTS (SELECT * FROM DEPARTMENT WHERE SSN=MGRSSN)

莊裕澤資料庫基本概念 20 NOT EXISTS Retrieve the name of employees who have no dependents. SELECT FNAME, LNAME FROM EMPLOYEE WHERE NOT EXISTS (SELECT * FROM DEPENDENT WHERE SSN=ESSN)

莊裕澤資料庫基本概念 21 Explicit sets and NULLS Retrieve the social security numbers of all employees who work on project number 1, 2, or 3. SELECT DISTINCT ESSN FROM WORKS_ON WHERE PNO IN (1,2,3) Retrieve the names of all employees who do not have supervisors. SELECT FNAME, LNAME FROM EMPLOYEE WHERE SUPERSSN IS NULL

莊裕澤資料庫基本概念 22 Renaming attributes and joined tables Retrieve the last name of each employee and his/her supervisor. SELECT E.LNAME AS EMPLOYLEE_NAME, S.LNAME AS SUPERVISOR_NAME FROM EMPLOYEE AS E, EMPLOYEE AS S WHERE E.SUPERSSN=S.SSN

莊裕澤資料庫基本概念 23 Renaming attributes and joined tables (cont.) Retrieve the name and address of every employee who works for the ‘Research’ department. SELECT FNAME, LNAME, ADDRESS FROM (EMPLOYEE JOIN DEPARTMENT ON DNO=DNUMBER) WHERE DNAME=‘Research’ Natural join, left outer join, right outer join

莊裕澤資料庫基本概念 24 Aggregate functions Aggregate functions: COUNT, SUM, MAX, MIN, AVG Find the sum of the salary of all employees, the maximum salary, the minimum salary, and the average salary. SELECT SUM(SALARY), MAX(SALARY), MIN(SALARY), AVG(SALARY) FROM EMPLOYEE

莊裕澤資料庫基本概念 25 Aggregate functions (cont.) Retrieve the number of employees in the ‘Research’ department SELECT COUNT(*) FROM EMPLOYEE, DEPARTMENT WHERE DNO=DNUMBER AND DNAME=‘Research’

莊裕澤資料庫基本概念 26 Grouping The aggregate functions can be used in SELECT and HAVING. For each department, retrieve the department number, the number of employees in the department, and their average salary. SELECT DNO, COUNT(*), AVG(SALARY) FROM EMPLOYEE GROUP BY DNO

莊裕澤資料庫基本概念 27 Grouping (cont.) For each project on which more than two employees work, retrieve the project number, the project name, and the number of employees who work on the project. SELECT PNUMBER, PNAME, COUNT(*) FROM PROJECT, WORKS_ON WHERE PNUMBER=PNO GROUP BY PNUMBER, PNAME HAVING COUNT(*) > 2

莊裕澤資料庫基本概念 28 SQL statement SELECT FROM [WHERE ] [GROUP BY ] [HAVING ] [ORDER BY ]

莊裕澤資料庫基本概念 29 Update statements in SQL Insert INSERT INTO EMPLOYEE VALUES (‘Richard’,’K’,’Marini’, ‘ ’, ‘30-DEC- 62’, ‘98 Oak St.,TX’,’M’, 37000, ’ ’,4) INSERT INTO EMPLOYEE(FNAME,LNAME,SSN) VALUES (‘Richard’,’Marini’,’ ’)

莊裕澤資料庫基本概念 30 Insert (cont.) Integrity constraints INSERT INTO EMPLOYEE(FNAME,LNAME,DNO) VALUES (‘Richard’,’Hatcher’,2) (rejected; no SSN) CREATE TABLE DEPTS_INFO (DEPT_NAME VARCHAR(15), NO_OF_EMPS INTEGER, TOTAL_SAL INTEGER); INSERT INTO DEPT_INFO (DEPT_NAME,NO_OF_EMPS, TOTAL_SAL) SELECT DNAME, COUNT(*), SUM(SALARY) FROM DEPARTMENT, EMPLOYEE WHERE DNUMBER=DNO GROUP BY DNAME;

莊裕澤資料庫基本概念 31 Delete DELETE - referential integrity DELETE FROM EMPLOYEE WHERE LNAME=‘Brown’ DELETE FROM EMPLOYEE WHERE DNO IN (SELECT DNUMBER FROM DEPARTMENT WHERE DNAME=‘Research’)

莊裕澤資料庫基本概念 32 Update UPDATE UPDATE EMPLOYEE SET SALARY=SALARY*1.1 WHERE DNO IN (SELECT DNUMBER FROM DEPARTMENT WHERE DNAME=‘Research’) (several tuples)

莊裕澤資料庫基本概念 33 Views in SQL View (virtual table): a single table derived from other tables No limitation for querying; some limitations for updating CREATE VIEW WORKS_ON1 AS SELECT FNAME,LNAME, PNAME, HOURS FROM EMPLOYEE, PROJECT, WORKS_ON WHERE SSN=ESSN AND PNO=PNUMBER;

莊裕澤資料庫基本概念 34 Views (cont.) CREATE VIEW DEPT_INFO (DEPT_NAME, NO_OF_EMPS, TOTAL_SAL) AS SELECT DNAME, COUNT(*), SUM(SALARY) FROM DEPARTMENT, EMPLOYEE WHERE DNUMBER=DNO GROUP BY DNAME; DROP VIEW WORKS_ON1; DROP VIEW DEPT_INFO;

莊裕澤資料庫基本概念 35 Updating views An update on a view defined on a single table without any aggregate functions can be mapped to an update on the base table. UPDATE WORKS_ON1 SET PNAME=‘ProductY’ WHERE LNAME=‘SMITH’ AND FNAME=‘John’ AND PNAME=‘ProductX’

莊裕澤資料庫基本概念 36 Updating views (cont.) Possible updates (base tables) (a) UPDATE WORKS_ON1 SET PNO=(SELECT PNUMBER FROM PROJECT WHERE PNAME=‘ProductY’) WHERE ESSN=(SELECT SSN FROM EMPLOYEE WHERE LNAME=‘Smith’ AND FNAME=‘John’) AND PNO=(SELECT PNUMBER FROM PROJECT WHERE PNAME=‘ProductX’) (b) UPDATE PROJECT SET PNAME=‘ProductY’ WHERE PNAME=‘ProductX’

莊裕澤資料庫基本概念 37 Updating views and view implementation Meaningless UPDATE DEPT_INFO SET TOTAL_SAL= WHERE DNAME=‘Research’ A view with a single defining table is updatable if attributes contain primary key or other candidate key of the base table. Views defined on multiple tables using joins are generally not updatable. Views defined using grouping and aggregate functions are not updatable.

莊裕澤資料庫基本概念 38 Assertions The salary of an employee must not be greater than the salary of the manager of the department that the employee works for. CREATE ASSERTION SALARY_CONSTRAINTS CHECK (NOT EXISTS (SELECT * FROM EMPLOYEE E, EMPLOYEE M, DEPARTMENT D WHERE E.SALARY >M.SALARY AND E.DNO=D.DNUMBER AND D.MGRSSN=M.SSN)

莊裕澤資料庫基本概念 39 Triggers A trigger specifies a condition and an action to be taken in case that condition is satisfied. DEFINE TRIGGER SALARY_TRIGGER ON EMPLOYEE E, EMPLOYEE M, DEPARTMENT D: E.SALARY >M.SALARY AND E.DNO=D.DNUMBER AND D.MGRSSN=M.SSN ACTION_PROCEDURE INFORM_MANAGER(D.MGRSSN);