Φροντιστήριο SQL (από το βιβλίο του Date)

Slides:



Advertisements
Similar presentations
Database Languages Chapter 7. The Relational Algebra.
Advertisements

Unit 5 The Network Model  5.1 The Network Model  5.2 IDMS.
BACS 485—Database Management Advanced SQL Overview Advanced DDL, DML, and DCL Commands.
TURKISH STATISTICAL INSTITUTE 1 /34 SQL FUNDEMANTALS (Muscat, Oman)
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
Chapter 3 An Introduction to Relational Databases.
Wei-Pang Yang, Information Management, NDHU More on Normalization Unit 18 More on Normalization ( 表格正規化探討 ) 18-1.
COMP 3715 Spring 05. Working with data in a DBMS Any database system must allow user to  Define data Relations Attributes Constraints  Manipulate data.
Structured Query Language Chapter Three (Excerpts) DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
Domains, Relations & Base RelVars (Ch
Structured Query Language Chapter Three DAVID M. KROENKE’S DATABASE CONCEPTS, 2 nd Edition.
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Unit 2 DB2 and SQL Wei-Pang Yang, Information Management, NDHU Outline of Unit Overview DB2 2.2 Data Definition 2.3 Data Manipulation 2.4.
Introduction Chapter 1. Reference Book  Database Systems Thomas Connolly, Carolyn Begg, Anne Strachan Addison-Wesley 1999 ISBN:
Lecture by Prof.Dr.S.Sridhar 1 DataBase Technology by Dr.S.Sridhar, Ph.D.(JNUD), RACI(Paris, NICE), RMR(USA), RZFM(Germany) DIRECTOR ARUNAI ENGINEERING.
Database Systems Lecture # 8 11 th Feb,2011. The Relational Model of Data The term relation is basically just a mathematical term for a table. DBMS products.
1 SELECT statement. 2 Sample database Supplier Part supplies (0,n) colour s# snamep# pname amount citydob price qualitydate.
Data Manipulation 11 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and other programming.
Relational Algebra II. Semantics - where A WHERE X  Y –  : an operator like =, >, … Result of restriction A by the condition X  Y is A relation –with.
6.8 Case Study: E-R for Supplier-and-Parts Database
1 Session 3 Welcome: To session 3-the 8 th. learning sequence “Relational algebra “ Recap : In the previous learning sequence, we discussed some example.
3-1 Unit 3 The Relational Model. 3-2 Wei-Pang Yang, Information Management, NDHU Outline  3.1 Introduction  3.2 Relational Data Structure  3.3 Relational.
3-1 Unit 3 The Relational Model. 3-2 Wei-Pang Yang, Information Management, NDHU Outline  3.1 Introduction  3.2 Relational Data Structure  3.3 Relational.
4B-1 Wei-Pang Yang, Information Management, NDHU Introduction to Database CHAPTER 4B ( 補 ) DB2 and SQL  Overview  Data Definition  Data Manipulation.
1 Structured Query Language (SQL) CIS*2450 Advanced Programming Concepts.
Logical Database Design ( 補 ) Unit 7 Logical Database Design ( 補 )
Wei-Pang Yang, Information Management, NDHU Normalization Unit 7 Normalization ( 表格正規化 ) 7-1.
SQL (Chapter 2: Simple queries; Chapter 7 and 8: Nested and DML queries) Many of the examples in this document are based on the tables in the next slide.
Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.
Chapter 7 Relational Algebra. Topics in this Chapter Closure Revisited The Original Algebra: Syntax and Semantics What is the Algebra For? Further Points.
Further Normalization I
1-1 Homework 3 Practical Implementation of A Simple Rational Database Management System.
Data Definition After this lecture, you should be able to:
Data Manipulation 21 After this lecture, you should be able to:  Use SQL SELECT statement effectively to retrieve the data from multiple related tables.
Unit 5 The Network Model  5.1 Data Modeling Issues  5.2 The Network Model  5.3 IDMS.
Algebra1 After this lecture, you should be able to:  Understand the differences between SQL (Structured Query Language) and Relational Algebra expressions.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
SQL - miscellaneous D. Christozov / G.Tuparov INF 280 Database Systems: SQL – miscellaneous 1.
Perform DFS from A A B C D E F G. Perform BFS from A A B C D E F G.
An Introduction to SQL For CS Overview of SQL  It is the standard language for relational systems, although imperfect  Supports data definition.
Relational Algebra Prof. Yin-Fu Huang CSIE, NYUST Chapter 7.
Unit 2 DB2 and SQL. 2-2 Wei-Pang Yang, Information Management, NDHU Outline of Unit Overview 2.2 Data Definition 2.3 Data Manipulation 2.4 The System.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Views Prof. Yin-Fu Huang CSIE, NYUST Chapter 10. Advanced Database System Yin-Fu Huang 10.1Introduction Example: Var Good_Supplier View (S Where Status.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
Chapter 4 An Introduction to SQL. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.4-2 Topics in this Chapter SQL: History and Overview The.
Relations with Stored and Inherited Attributes Witold Litwin Dauphine University June 13, 2016.
SQL Exercises. 1) Names of suppliers. Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost)
Structured Query Language IV Asma Ahmad (C. J. Date) Database Systems.
Chapter 4 An Introduction to SQL.
STRUCTURE OF PRESENTATION :
Lecture 3 : Structured Query Language (SQL)
UNIT 11 Query Optimization
Unit 2 DB2 and SQL.
for Computer Professionals
STRUCTURE OF PRESENTATION :
Chapter 8 Relational Calculus.
Introduction to Database
Introduction to Database
CS 405G: Introduction to Database Systems
پايگاه داده ها.
بسم الله الرحمن الرحيم.
Advanced Database System
Unit 7 Normalization (表格正規化).
STRUCTURE OF PRESENTATION :
Question 1: Basic Concepts (45 %)
STRUCTURE OF PRESENTATION :
File and data base concepts
Unit 12 Index in Database 大量資料存取方法之研究 Approaches to Access/Store Large Data 楊維邦 博士 國立東華大學 資訊管理系教授.
Presentation transcript:

Φροντιστήριο SQL (από το βιβλίο του Date) Παράδειγμα σχήματος Suppliers and Parts Εντολές DDL Εντολές DML

Suppliers and Parts database Φροντιστήριο SQL S# SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 S4 Clark S5 Adams Athens S# P# QTY S1 P1 300 P2 200 P3 400 P4 P5 100 P6 S2 S3 S4 Suppliers and Parts database P# PNAME COLOR WEIGHT CITY P1 Nut Red 12 London P2 Bolt Green 17 Paris P3 Screw Blue Rome P4 14 P5 Cam P6 Cog 19

FOREIGN KEY(S#) REFERENCES S ON DELETE CASCADE ON UPDATE CASCADE, Φροντιστήριο SQL CREATE TABLE SP (S# S# NOT NULL, P# P# NOT NULL, QTY QTY NOT NULL, PRIMARY KEY (S#,P#) FOREIGN KEY(S#) REFERENCES S ON DELETE CASCADE ON UPDATE CASCADE, FOREIGN KEY(P#) REFERENCES P CHECK (QTY>0 AND QTY<5001) );

CREATE DOMAIN S# CHAR(5); CREATE DOMAIN P# CHAR(6); Φροντιστήριο SQL CREATE DOMAIN S# CHAR(5); CREATE DOMAIN P# CHAR(6); CREATE DOMAIN QTY NUMERIC(9);

Get color and city for “nonParis” parts with weight greater than ten Φροντιστήριο SQL Get color and city for “nonParis” parts with weight greater than ten SELECT P.COLOR, P.CITY FROM P WHERE P.CITY< >`PARIS` AND P.WEIGHT>10;

Φροντιστήριο SQL For all parts, get the part number and the weight of that part in grams SELECT P.P#, P.WEIGHT * 454 AS GWT FROM P;

Φροντιστήριο SQL Get all combinations of supplier and part information such that the supplier and part in question are collocated SELECT S.S#, S.SNAME, S.STATUS, S.CITY, P.P#, P.PNAME, P.COLOR, P.WEIGHT FROM S, P WHERE S.CITY=P.CITY; ή FROM S JOIN P ON S.CITY=P.CITY;

Φροντιστήριο SQL Get all pairs of supplier numbers such that the two suppliers concerned are collocated SELECT FIRST.S# AS SA, SECOND.S# AS SB FROM S AS FIRST, S AS SECOND WHERE FIRST.CITY=SECOND.CITY AND FIRST.S# < SECOND.S#;

Get the maximum and minimum quantity for part P2 Φροντιστήριο SQL Get the maximum and minimum quantity for part P2 SELECT MAX (SP.QTY) AS MAXQ, MIN (SP.QTY) AS MINQ FROM SP WHERE SP.P# = `P2`;

Φροντιστήριο SQL For each part supplied, get the part number and the total shipment quantity SELECT SP.P#, SUM (SP.QTY) AS TOTQTY FROM SP GROUP BY SP.P# ; ή SELECT P.P#, (SELECT SUM (SP.QTY) WHERE SP.P#=P.P#) AS TOTQTY FROM P;

Get part numbers for all parts supplied by more than one supplier Φροντιστήριο SQL Get part numbers for all parts supplied by more than one supplier SELECT SP.P# FROM SP GROUP BY SP.P# HAVING COUNT(SP.S#) >1;

Get supplier names for suppliers who supply part P2 Φροντιστήριο SQL Get supplier names for suppliers who supply part P2 SELECT DISTINCT S.SNAME FROM S WHERE S.S# IN (SELECT SP.S# FROM SP WHERE SP.P# = `P2`); ή FROM S, SP WHERE S.S# =SP.S# AND SP.P# = `P2`;

Get supplier names for suppliers who supply part P2 Φροντιστήριο SQL Get supplier names for suppliers who supply part P2 SELECT DISTINCT S.SNAME FROM S WHERE EXISTS (SELECT * FROM SP WHERE SP.S#=S.S# AND SP.P# = `P2`);

Get supplier names for suppliers who do not supply part P2 Φροντιστήριο SQL Get supplier names for suppliers who do not supply part P2 SELECT DISTINCT S.SNAME FROM S WHERE NOT EXISTS (SELECT * FROM SP WHERE SP.S#=S.S# AND SP.P# = `P2`); ή WHERE S.S# NOT IN (SELECT SP.S#* WHERE SP.P# = `P2`);

Get supplier names for suppliers who supply at least one red part Φροντιστήριο SQL Get supplier names for suppliers who supply at least one red part SELECT DISTINCT S.SNAME FROM S WHERE S.S# IN (SELECT SP.S# FROM SP WHERE SP.P# IN (SELECT P.P# FROM P WHERE P.COLOR= `Red`));

Get supplier names for suppliers who supply all parts Φροντιστήριο SQL Get supplier names for suppliers who supply all parts SELECT DISTINCT S.SNAME FROM S WHERE NOT EXISTS (SELECT * FROM P FROM SP WHERE SP.S#=S.S# AND SP.P#= P.P#));

Get supplier names for suppliers who supply all parts Φροντιστήριο SQL Get supplier names for suppliers who supply all parts SELECT DISTINCT S.SNAME FROM S WHERE (SELECT COUNT(SP.P#) FROM SP WHERE SP.S#=S.S#) = (SELECT COUNT (P.P#) FROM P);

Φροντιστήριο SQL Get supplier numbers for suppliers with status less that the current maximum status in the S table SELECT S.S# FROM S WHERE S.STATUS < (SELECT MAX(S.STATUS) FROM S);

Φροντιστήριο SQL Get part numbers for parts that either weight more than 16 pounds or are supplied by supplier S2, or both SELECT P.P# FROM P WHERE P.WEIGHT > 16 UNION SELECT SP.P# FROM SP WHERE SP.S#=`S2`);

INTO P(P#,PNAME,COLOR,WEIGHT,CITY) Φροντιστήριο SQL INSERT INTO P(P#,PNAME,COLOR,WEIGHT,CITY) VALUES (`P8`, `Sprocket`, `Pink`, 14, `Nice`); INTO TEMP(S#,CITY) SELECT S.S#, S.CITY FROM S WHERE S.STATUS>15;

SET COLOR=`Yellow`, WEIGHT=P.WEIGHT+5 WHERE P.CITY=`Paris`; Φροντιστήριο SQL UPDATE P SET COLOR=`Yellow`, WEIGHT=P.WEIGHT+5 WHERE P.CITY=`Paris`; SET CITY = (SELECT S.CITY FROM S WHERE S.S#=`S5`) WHERE P.COLOR=`Red`;

DELETE FROM SP WHERE `London` = (SELECT S.CITY FROM S Φροντιστήριο SQL DELETE FROM SP WHERE `London` = (SELECT S.CITY FROM S WHERE S.S#=SP.S#);

Φροντιστήριο SQL For all red and blue parts such that the total quantity supplied is greater than 350 (excluding from the total all shipment for which the quantity is less than or equal to 200), get the part number, the weight in grams, the color, and the maximum quantity supplied of that part

`Max Quantity=` AS TEXT2, MAX(SP.QTY) AS MQY FROM P, SP Φροντιστήριο SQL SELECT P.P#, `Weight in grams =` AS TEXT1, P.WEIGHT * 454 AS GMWT, P COLOR, `Max Quantity=` AS TEXT2, MAX(SP.QTY) AS MQY FROM P, SP WHERE P.P#=SP.P# AND SP.QTY>200 AND (P.COLOR=`Red` OR P.COLOR=`Blue`) GROUP BY P.P#, P.WEIGHT, P.COLOR HAVING SUM(SP.QTY) > 350;

Φροντιστήριο SQL P# TEXT1 GMWT COLOR TEXT2 MQY P1 Weight in grams = 5448 Red Max quantity = 300 P5 Blue 400 P3 7718

Φροντιστήριο SQL Get part names for parts whose weight is greater that that of every blue part SELECT DISTINCT PX.PNAME FROM P AS PX WHERE PX.WEIGHT > ALL (SELECT PY.WEIGHT FROM P AS PY WHERE PY.COLOR=`Blue`);