Bazy danych Ćwiczenia z SQL W ćwiczeniach wykorzystano przyk adowy schemat bazy danych dostarczany z Personal Oracle 8 Definicję schematu i dane tabel zawiera plik bdemobld sql (c) 2001 Katedra Informatyki i Automatyki PRz 1/5
Diagram związków encji pokazany ni ej modeluje schemat bazy danych firmy handlowej Gwiazdkami zaznaczono atrybuty kluczowe encji Związkom odpowiadają klucze obce o nazwach identycznych z nazwami atrybutów kluczowych Wyjątkiem jest SALESPERSON ID - referencja do atrybutu kluczowego tabeli EMPLOYEE oraz MANAGER ID - referencja do atrybutu kluczowego tabeli EMPLOYEE LOCATION LOCATION ID* REGIONAL GROUP DEPARTMENT DEPARTMENT ID* NAME JOB JOB ID* FUNCTION manager id EMPLOYEE EMPLOYEE ID* LAST NAME FIRST NAME MIDDLE INITIAL HIRE DATE SALARY COMMISSION MANAGER ID (FK) salesperson id SALES ORDER ORDER ID* ORDER DATE SHIP DATE TOTAL ITEM ITEM ID* ACTUAL PRICE QUANTITY TOTAL CUSTOMER CUSTOMER ID* NAME ADDRESS CITY STATE ZIP CODE AREA CODE PHONE NUMBER SALESPERSON ID (FK) CREDIT LIMIT COMMENTS PRODUCT PRODUCT ID* DESCRIPTION PRICE LIST PRICE* MIN PRICE START DATE END DATE Wykonać następujące ćwiczenia 1 Wyświetlić nazwiska i etaty pracowników wydzia ów ʼRESEARCHʼ lub ʼSALESʼ sortowane wed ug nazwisk i wed ug wydzia ów 2 Wyświetlić nazwiska i etaty analityków (ʼANALYSTʼ) zatrudnionych w miastach ʼNEW YORKʼ lub ʼBOSTONʼ (c) 2001 Katedra Informatyki i Automatyki PRz 2/5
3 Podać polecenie, które wyświetli na konsoli SQL następujące dane: Pracownik nazwisko pracuje na etacie etat od data zatrudnienia 4 Wyświetlić nazwisko, nazwę wydzia u i kwotę sprzeda y za poprzedni miesiąc pracowników zatrudnionych na etacie ʼSALESPERSONʼ, których sprzeda przekroczy a 1280 USD eby ustalić poprzedni miesiąc sprawdzić odpowiednim poleceniem kiedy zosta o z o one ostatnie zamówienie Nie mo na u yć EMPLOYEE SALARY poniewa atrybut ten zawiera kwotę sprzeda y w aktualnym miesiącu 5 Dla ka dego klienta wyświetlić jego nazwę i sumę w USD na jaką z o y dotychczas zamówienia Zapytanie powinno te wyświetlić klientów, którzy jeszcze nic nie kupili 6 Wyświetlić liczbę zamówień oraz średnią wartość jednego zamówienia przyjętego przez pracowników wydzia ów ʼSALESʼ (sprzeda ) w poszczególnych miastach Wynik powinien zawierać następujące informacje: Miasto, liczba przyjętych zamówień, średnia wartość zamówienia 7 Wyświetlić nazwy wszystkich klientów, daty z o enia zamówień, ilość i nazwę ka dego towaru, który zamówili Zapytanie powinno pokazać tak e klientów, którzy jeszcze nic nie kupili, zamówienia, które nie mają adnych pozycji i towary, których jeszcze nikt nie kupi Dodatkowe informacje o schemacie Tabela lub kolumna EMPLOYEE ʼGeneral information on all corporate employees ʼ; EMPLOYEE EMPLOYEE ID ʼEmployee Number: Unique 4-digit identification number assigned to every employee ʼ; EMPLOYEE LAST NAME ʼLast name of employee Stored in all caps ʼ; EMPLOYEE FIRST NAME ʼFirst name of employee Stored in all caps ʼ; EMPLOYEE MIDDLE INITIAL ʼMiddle initial (one character only) Stored in all caps ʼ; EMPLOYEE JOB ID ʼJob identification number See JOB for descriptions ʼ; EMPLOYEE MANAGER ID ʼManager: The employee number of this employeeʼʼs manager If employee has no manager, value should be NULL ʼ; EMPLOYEE HIRE DATE ʼDate the employee was HIRED, not necessarily the day the employee STARTED ʼ; EMPLOYEE SALARY ʼMonthly salary for the employee in U S dollars Salary grade information can be found in the table SALARY GRADE ʼ; EMPLOYEE COMMISSION ʼCommission (in U S dollars) earned last month This field should be NULL for non-commissioned employees ʼ; EMPLOYEE DEPARTMENT ID ʼEmployeeʼʼs department number Further department information is available in the table DEPT ʼ; JOB ʼAll valid job categories for personnel information ʼ; JOB JOB ID ʼUnique 3-digit code for job function and title ʼ; JOB FUNCTION ʼGeneric function class of job ʼ; (c) 2001 Katedra Informatyki i Automatyki PRz 3/5
Tabela lub kolumna DEPARTMENT DEPARTMENT DEPARTMENT ID DEPARTMENT NAME DEPARTMENT LOCATION ID LOCATION LOCATION LOCATION ID LOCATION REGIONAL GROUP PRODUCT ʼDepartment information for all U S operations ʼ; ʼDepartment number: Unique 2-digit department number assigned to each department ʼ; ʼDepartment name: Only the first 14 characters of a department name can be stored in the database Storing names in all caps is recommended but not required ʼ; ʼDepartment location: The code for where the departmentʼʼs central office is located (LOCATION)ʼ; ʼLocation information for all U S departments and warehouses ʼ; ʼCentral location corporate location code ʼ; ʼMetropolitan center closest to location ʼ; ʼProducts in catalog Pricing information can be found in table PRICE ʼ; PRODUCT PRODUCT ID ʼUnique 6 digit number assigned to all products Number generated by sequence PRODUCT ID ʼ; PRODUCT DESCRIPTION ʼFull product name (up to 30 characters) Storing product names in all caps is recommended but not required ʼ; PRICE ʼPrices (both standard and minimum) of products Database tracks both effective dates and expiration dates for prices ʼ; PRICE PRODUCT ID ʼProduct number to which price applies Product name found in table PRICE ʼ; PRICE LIST PRICE ʼUndiscounted price (in U S dollars) ʼ; PRICE MIN PRICE ʼLowest price (in U S dollars) that can be offered ʼ; PRICE START DATE ʼDate which standard and minimum prices go into effect ʼ; PRICE END DATE ʼDate which standard and minimum price expire This value can be left NULL ʼ; SALES ORDER ʼSales order for a customer Items in the order can be found in the table ITEM ʼ; SALES ORDER ORDER ID ʼUnique 4 digit number assigned to all orders Number generated by sequence ORDER ID ʼ; SALES ORDER ORDER DATE ʼDate sales order logged (not necessarily when order was fulfilled )ʼ; SALES ORDER CUSTOMER ID ʼCustomer ordering the goods Further information can be found in the table CUSTOMER ʼ; SALES ORDER SHIP DATE ʼDate order fulfilled and invoice sent (not necessarily date payment received) ʼ; SALES ORDER TOTAL ʼTotal of all items totals in the order (i e the value of TOTAL for order N should contain: SELECT SUM(TOTAL) FROM ITEM WHERE ORDER ID=N)ʼ; (c) 2001 Katedra Informatyki i Automatyki PRz 4/5
Tabela lub kolumna CUSTOMER CUSTOMER CUSTOMER ID CUSTOMER NAME CUSTOMER ADDRESS CUSTOMER CITY CUSTOMER STATE CUSTOMER ZIP CODE CUSTOMER AREA CODE CUSTOMER PHONE NUMBER CUSTOMER SALESPERSON ID CUSTOMER CREDIT LIMIT CUSTOMER COMMENTS ITEM ITEM ORDER ID ITEM ITEM ID ITEM PRODUCT ID ITEM ACTUAL PRICE ITEM QUANTITY ʼInformation on all U S customers and prospective customers ʼ; ʼUnique 6 digit number assigned to all customers Number generated by the sequence CUSTOMER ID ʼ; ʼFull store name of every customer (up to 45 characters) Storing names in all caps is recommended but not required ʼ; ʼStreet address of customer CITY, STATE, and ZIP CODE make up the rest of the address ʼ; ʼCity name for address of customer Storing city names in all caps is recommended, but not required ʼ; ʼTwo letter abbreviation for state name for address of customer Must be entered in all caps ʼ; ʼZip code of customer Supports both 5 and 9 digit zip codes Dashes should be omitted from 9 digit zips ʼ; ʼArea code of phone number for customer ʼ; ʼPhone number of customer This field does not supports letters and spaces for mnemonics (7 numeric digits only) ʼ; ʼEmployee number of sales representative handling customerʼʼs account Employee information can be found in the EMPLOYEE table ʼ; ʼCredit limit of customer or prospective customer in U S dollars ʼ; ʼField can be used flexibly Suggestions for use include lists of action items or special considerations when dealing with the particular customer ʼ; ʼItems listed in sales orders Product information can be found in table PRODUCT Sales order information can be found in table SALES ORDER ʼ; ʼSales order in which this item appears Sales order information can be found in table SALES ORDER ʼ; ʼNumber of item within the sales order ʼ; ʼNumber of product (described in PRODUCT) that pertains to the line item ʼ; ʼPrice to be paid by the customer in U S dollars The standard price is PRICE STANDARD PRICE, but in some instances, discounts are given to as low as PRICE MINIMUM PRICE ʼ; ʼThe amount of product to be ordered in this line item ʼ; ITEM TOTAL ʼThe total price (in U S dollars) of this line item TOTAL = ACTUAL PRICE * QUANTITY ʼ; (c) 2001 Katedra Informatyki i Automatyki PRz 5/5