Chapter 5Structured Query Language          (SQL)
In this chapter, you will learn:          The basic commands and functions of SQL          How SQL is used for data mani...
In this chapter, you will learn:          How SQL is used for data administration (to           create tables, indexes, a...
Introduction to SQL      Ideal database language         Create database and table structures         Perform basic dat...
Introduction to SQL      SQL is the ideal DB language         Data definition language (create tables and          defin...
Good Reasons to Study SQL      Easy to learn      Portable to many database specification, only       minor differences ...
Good Reasons to Study SQL      ANSI standardization effort led to de facto       query standard for relational database  ...
Data Definition Commands   Database model       PRODUCT and VENDORMELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   9
Data Definition Commands       Create database structure            Holds all tables and is a collection of physical fil...
Data Definition Commands           ANSI standard to create database            schema              CREATE SCHEMA AUTHORIZ...
Data Dictionary TableMELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   12
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   13
Creating Table Structure              Data Types                   Number:                      NUMBER(L,D)            ...
Creating Table Structure              Data Types                   Date                        DATE                   ...
Creating Table Structure              Tables store end-user data              May be based on data dictionary entries   ...
   Example: CREATE TABLE VENDOR ( V_CODE      INTEGER                      NOT NULL            UNIQUE V_NAME      VARCHAR...
   Example: CREATE TABLE PRODUCT ( P_CODE      VARCHAR(10) NOT NULL   UNIQUE P_DESCRIPT VARCHAR(35) NOT NULL P_INDATE    ...
Using Domains       Domain is set of permissible values for a        column       Definition requires:          Name   ...
Using Domains       Example:CREATE DOMAIN MARITAL_STATUS AS VARCHAR(8) AS DATA_TYPECHECK (VALUE IN (‘Single’, ‘Married’, ...
Using Domains       Draw domain values from other tables by        including a SELECT statement int eh        CHECK claus...
Using Domains       Delete domain                                  Prevent deleting until no attributes based on the doma...
SQL Integrity Constraints       Adherence to entity integrity and referential        integrity rules is crucial         ...
CREATE TABLE PRODUCT ( P_CODE      VARCHAR(10) NOT NULL   UNIQUE P_DESCRIPT VARCHAR(35) NOT NULL P_INDATE    DATE        N...
Data Manipulation Commands                    Common SQL Commands                                                         ...
Data Entry and Saving                        Enters data into a table       INSERT INTO <table name>       VALUES (attrib...
Data Entry and Saving                 Use form (GUI) to enter dataMELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4)...
Data Entry and Saving     Saves changes to disk                      COMMIT <table names> ;MELJUN CORTES, MBA,MPA,BSCS   ...
Listing Table Contents and               Other Commands         Allows table contents to be listed            SELECT <att...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   30
Listing Table Contents and               Other Commands         UPDATE command makes data entry          corrections     ...
Listing Table Contents and               Other Commands         ROLLBACK command restores database          back to previ...
Listing Table Contents and               Other Commands         DELETE command removes table row            DELETE FROM P...
Queries            Creating partial listings of table contents                               SELECT <column(s)>          ...
Examples   Mathematical operators          SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE          FROM PRODUCT          WH...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   36
Examples   Mathematical operators    SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE    FROM PRODUCT    WHERE V_CODE <> 2134...
Examples   Mathematical operators          SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE          FROM PRODUCT          WHE...
Examples          Mathematical operators on character           attributes        SELECT P_CODE,P_DESCRIPT,P_ONHAND,P_MIN...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   40
Computed Columns        New columns can be created through valid         computations or formulas           Formulas may...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   42
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   43
Operators   Logical: AND, OR,    NOTSELECT *FROM PRODUCTWHERE V_CODE = 21344OR V_CODE = 24288;MELJUN CORTES, MBA,MPA,BSCS...
Operators       Rules of precedence         Conditions within parenthesis executed first         Boolean algebra    SEL...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   46
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   47
Special Operators       BETWEEN - defines limitsSELECT * FROM PRODUCT WHERE P_PRICE BETWEEN 500.00 AND 100.00;       IS ...
Special Operators       LIKE - checks for similar string (wildcard)SELECT V_NAME, V_CONTACT, V_AREACODE, V_PHONEFROM VEND...
Special Operators       IN - checks for value in a set  SELECT * FROM PRODUCT WHERE V_CODE=21344 OR V_CODE=24288;  SELECT...
Special Operators       EXISTS – NOT NULL     DELETE FROM PRODUCT WHERE P_CODE EXISTS;    SELECT FROM PRODUCT WHERE V_COD...
Advanced Data Management             Commands            ALTER - changes table structure            ADD - adds column   ...
Advanced Data Management             Commands       ALTER - changes table structure         ADD - adds column         M...
Advanced Data Management             Commands       Changing a column’s data type:                                       ...
Advanced Data Management             Commands       Adding a column to the table:                                        ...
Advanced Data Management          Commands (con’t.)   Entering data into new    column                                   ...
Advanced Data Management          Commands (con’t.)   Entering data into new    column       UPDATE PRODUCT       SET P_S...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   58
Advanced Data Management          Commands (con’t.)    UPDATE PRODUCT    SET P_SALECODE=‘2’     WHERE P_INDATE<’12/25/2001...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   60
Advanced Data Management          Commands (con’t.)               Arithmetic operators and rules of precedence           ...
Advanced Data Management          Commands (con’t.)               Arithmetic operators and rules of precedence        UPD...
Advanced Data Management          Commands (con’t.)         Copying parts of tables              Copy the contents of se...
Advanced Data Management          Commands (con’t.)         Copying parts of tables                                      ...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   65
Advanced Data Management          Commands (con’t.)         Deleting a table from database            DROP TABLE         ...
Advanced Data Management          Commands (con’t.)         Primary and foreign key designation      ALTER TABLE LINE    ...
Advanced Data Management          Commands (con’t.)         Primary and foreign key designation      ALTER TABLE PRODUCT ...
More complex queries and sql                functions       Ordering listing: list in special order        (ascending ord...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   70
More complex queries and sql                functions       Cascading order sequence        ORDER BY <attribute1 attribut...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   72
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   73
More complex queries and sql                functions       Use order by clause in conjunction with        other SQL comm...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   75
More complex queries and sql            functions   List unique values     SELECT DISTINCT V_CODE        FROM PRODUCT;MEL...
Aggregate                 Function OperationsMELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   77
Aggregate                     Function Operations             COUNT (count raw numbers for a given              column)  ...
Aggregate                     Function Operations             MAX and MIN            SELECT MIN(P_PRICE)            FROM ...
Example Aggregate      Function Operations (con’t.)           SUM             SELECT SUM(P_ONHAND * P_PRICE)             ...
   Grouping data             Creates frequency distributions             Only valid when used with SQL arithmetic funct...
More Complex Queries and        SQL Functions (con’t.)          Virtual tables: creating a view               CREATE VIE...
   Joining database tables          Data are retrieved from more than one table        SELECT PRODUCT.P_DESCRIPT, PRODUC...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   84
Updatable Views    Common operation in production environments is     use of batch routines to update master table     at...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   86
UPDATE PRODUCT, PMSALES      SET PRODUCT.PROD_ QOH=PROD_QOH-PMS-QTY       WHERE PRODUCT.PROD_ID=PMSALES.PROD_ID; This work...
Updatable Views     Restrictions        GROUP BY expressions cannot be used        Cannot use set operators---UNION,   ...
Updatable Views      Create Updatable viewCREATE VIEW MPSVUPD AS (SET PRODUCT.PROD_ID, PRODUCT.PROD_QOH,PMSALES.PMS_QTYFR...
Procedural SQL        SQL is a declarative language, not support         conditional executions of procedures and        ...
Procedural SQL        Solution             Embedded SQL can be called from within procedural              programming la...
Procedural SQL (con’t.)     Procedural SQL allows the use of procedural      code and SQL statements that are stored     ...
Procedural SQL (con’t.)     End users can use procedural SQL (PL/SQL)      to create:        Triggers        Stored pro...
Triggers          Procedural SQL code invoked before or after           data row is selected, inserted, or updated       ...
Triggers          Can enforce particular constraints          Automate critical actions and provide           warnings f...
Triggers (con’t.)          Oracle example      CREATE OR REPLACE TRIGGER      TRG_PRODUCT_REORDER      AFTER INSERT OR UP...
Stored Procedures          Named collection of procedural and SQL           statements stored in database          Invok...
Stored Procedures          Invoked with EXEC      CREATE OR REPLACE PROCEDURE procedure_name      (argument IN/OUT data-t...
Stored Procedures          Example      CREATE OR REPLACE PROCEDURE PROD_REORDER_SET      AS BEGIN         UPDATE PRODUCT...
Stored Procedures          Example      CREATE OR REPLACE PROCEDURE PROD_SALE(CODE IN      VARCHAR2, QTYSOLD IN NUMBER)  ...
PL/SQL Stored Functions         Named group of procedural and SQL          statements that returns a value          CREAT...
PL/SQL Stored Functions         Invoked from within stored procedures or          triggers         Cannot be invoked fro...
Converting an ER model into a            database structure         Require the some rules         Database of artist   ...
Artist Database ERD and                   Tables                                                              Figure 5.55M...
MELJUN CORTES, MBA,MPA,BSCS   MELJUN CORTES ( CSSD04C4) SQL   105
Table Structure and specific              requirements   PAINTER(PTR_NUM, PTR_LASTNAME,PTR_FIRSTNAME,   PTR)INITIAL, PTR_A...
Further action considerations 1.   Delete raw of painter will effect the PAINTER’s table and cerate delete      anomaly. I...
Creates Tables using SQL    CREATE TABLE PAINTER (    PTR_NUM           CHAR(4)                         NOT NULL    UNIQUE...
Creates Tables using SQL    CREATE TABLE GALLERY (    GAL_NUM           CHAR(4)      NOT NULL                   UNIQUE,   ...
Creates Tables using SQL    CREATE TABLE GALLERY (    GAL_NUM           CHAR(4)      NOT NULL                   UNIQUE,   ...
Creates Tables using SQL    CREATE TABLE PAINTING (    PNTG_NUM           CHAR(4)      NOT NULL                  UNIQUE,  ...
General Rules Governing      Relationships Among Tables     SQL statement associated with the combination of     Optional ...
General Rules Governing      Relationships Among Tables                     Figure 5.56: M:N, Both Sides Mandatory        ...
General Rules Governing      Relationships Among Tables                         Figure 5.58: M:N, One Side Optional       ...
General Rules Governing      Relationships Among Tables                  Figure 5.58: M:N, One Side OptionalMELJUN CORTES,...
General Rules Governing      Relationships Among Tables                         Figure 5.58: M:N, One Side OptionalMELJUN ...
General Rules Governing      Relationships Among Tables                      Figure 5.60: 1:M, Both Sides Optional        ...
General Rules Governing         Relationships Among Tables               Figure 5.62: 1:M, One Side Optional, One Side Man...
General Rules Governing         Relationships Among Tables                        Figure 5.64: 1:1, Both Sides Optional   ...
General Rules Governing        Relationships Among Tables           Figure 5.66: Weak Entity, Foreign Key Located in Weak ...
Upcoming SlideShare
Loading in …5
×

MELJUN CORTES Structured Query_language_complete

399
-1

Published on

MELJUN CORTES Structured Query_language_complete

Published in: Education, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
399
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
43
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

MELJUN CORTES Structured Query_language_complete

  1. 1. Chapter 5Structured Query Language (SQL)
  2. 2. In this chapter, you will learn:  The basic commands and functions of SQL  How SQL is used for data manipulation (to add, modify, delete, and retrieve data)  How to use SQL to query a database to extract useful informationMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 2
  3. 3. In this chapter, you will learn:  How SQL is used for data administration (to create tables, indexes, and views)  About more advanced SQL features such as updatable views, stored procedures, and triggersMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 3
  4. 4. Introduction to SQL  Ideal database language  Create database and table structures  Perform basic data management chores (add, delete, and modify)  Perform complex queries to transform data into useful informationMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 4
  5. 5. Introduction to SQL  SQL is the ideal DB language  Data definition language (create tables and define access right to tables)  Data manipulation language (update and manipulate data within database tables)MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 5
  6. 6. Good Reasons to Study SQL  Easy to learn  Portable to many database specification, only minor differences among various SQLs defined in Oracle, SQL server, DB2, Informix, Access, my SQL, …MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 6
  7. 7. Good Reasons to Study SQL  ANSI standardization effort led to de facto query standard for relational database (SQL-99 or SQL3)  Forms basis for present and future DBMS integration efforts  Becomes catalyst in development of distributed databases and database client/server architectureMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 7
  8. 8. Data Definition Commands Database model  PRODUCT and VENDORMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 8
  9. 9. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 9
  10. 10. Data Definition Commands  Create database structure  Holds all tables and is a collection of physical files stored on disk  DBMS automatically creates tables to store metadata  Schema: a group of database objects (tables, indexes and relationships)  Database administrator creates structure or schema  Logical group of tables or logical database  Groups tables by owner  Enforces securityMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 10
  11. 11. Data Definition Commands  ANSI standard to create database schema CREATE SCHEMA AUTHORIZATION <creator> Example: CREATE SCHEMA AUTHORIZATION JONESMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 11
  12. 12. Data Dictionary TableMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 12
  13. 13. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 13
  14. 14. Creating Table Structure  Data Types  Number:  NUMBER(L,D)  INTEGER  SMALLINT  DECIMAL(L,D)  Character:  CHAR(L)  VARCHAR(L)MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 14
  15. 15. Creating Table Structure  Data Types  Date  DATE  Others  TIME  TIME-STAMP  REAL  DOUBLE  FLOAT  ODL  …MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 15
  16. 16. Creating Table Structure  Tables store end-user data  May be based on data dictionary entries CREATE TABLE <table name> ( <attribute1 name and attribute1 characteristics, attribute2 name and attribute2 characteristics, attribute3 name and attribute3 characteristics, primary key designation, foreign key designation and foreign key requirement>);MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 16
  17. 17.  Example: CREATE TABLE VENDOR ( V_CODE INTEGER NOT NULL UNIQUE V_NAME VARCHAR(35) NOT NULL V_CONTACT VARCHAR(15) NOT NULL V_AREACODE CHAR(3) NOT NULL V_PHONE CHAR(8) NOT NULL V_STATE CHAT(2) NOT NULL V_ORDER CHAR(1) NOT NULL PRIMARY KEY(V_CODE) );MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 17
  18. 18.  Example: CREATE TABLE PRODUCT ( P_CODE VARCHAR(10) NOT NULL UNIQUE P_DESCRIPT VARCHAR(35) NOT NULL P_INDATE DATE NOT NULL P_ONHAND SMALLINT NOT NULL P_MIN SMALLINT NOT NULL P_PRICE NUMBER(8,2) NOT NULL P_DISCOUNT NUMBER(5,2) NOT NULL V_CODE INTEGER PRIMARY KEY(P_CODE) FOREIGN KEY(V_CODE) REFERENCES VENDOR ON DELETE RESTRICT ON UPDATE CASCADE); Can not delete vendor table You can change vendor’s codeMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 18
  19. 19. Using Domains  Domain is set of permissible values for a column  Definition requires:  Name  Data type  Default value  Domain constraint or condition CREATE DOMAIN <domain_name> AS DATA_TYPE [DEFAULT <default_value>] [CHECK (<condition>)]MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 19
  20. 20. Using Domains  Example:CREATE DOMAIN MARITAL_STATUS AS VARCHAR(8) AS DATA_TYPECHECK (VALUE IN (‘Single’, ‘Married’, ‘Divorced’, ‘Widowed’));  You can use domain name instead of attribute’s data type, when you define a new tableCREATE TABLE EMPLOYEE (EMP_NUM INTEGER NOT NULL CONSTRAIN EMPKRY KEY (EMP_NUM),EMP_LNAME VARCHAR(15) NOT NULL,EMP_FNAME VARCHAR(15) NOT NULL,EMP_STATUS MARITAL_STATUS NOT NULL);MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 20
  21. 21. Using Domains  Draw domain values from other tables by including a SELECT statement int eh CHECK clauseCREATE DOMAIN DISCOUNTING_RATES AS NUMBER(5,2)CHECK (VALUE IN (SELECT DISCOUNT FROM DISCOUNTBL); DISCOUNT attributes in DISCOUNTBL tableMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 21
  22. 22. Using Domains  Delete domain Prevent deleting until no attributes based on the domainDROP DOMAIN <domain_name> [RESTRICT |CASCADE]DROP DOMAIN MARITAL_STATUS CASCADE; Change data type to the one defined In domain’s definition  Not all database support create domain like OracleMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 22
  23. 23. SQL Integrity Constraints  Adherence to entity integrity and referential integrity rules is crucial  Entity integrity enforced automatically if primary key specified in CREATE TABLE command sequence  Referential integrity can be enforced in specification of FOREIGN KEY  Other specifications to ensure conditions met:  ON DELETE RESTRICT  ON UPDATE CASCADEMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 23
  24. 24. CREATE TABLE PRODUCT ( P_CODE VARCHAR(10) NOT NULL UNIQUE P_DESCRIPT VARCHAR(35) NOT NULL P_INDATE DATE NOT NULL P_ONHAND SMALLINT NOT NULL P_MIN SMALLINT NOT NULL P_PRICE NUMBER(8,2) NOT NULL P_DISCOUNT NUMBER(5,2) NOT NULL V_CODE INTEGER PRIMARY KEY(P_CODE) FOREIGN KEY(V_CODE) REFERENCES VENDOR ON DELETE RESTRICT ON UPDATE CASCADE); Some database does this automatically without explicit specificationsMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 24
  25. 25. Data Manipulation Commands Common SQL Commands Table 5.3MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 25
  26. 26. Data Entry and Saving  Enters data into a table INSERT INTO <table name> VALUES (attribute 1 value, attribute 2 value, … etc.); INSERT INTO VENDOR VALUES (21225, ‘my company’, ‘jun ni’, ‘319’,’232-2194’,’IA’,’Y’);MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 26
  27. 27. Data Entry and Saving  Use form (GUI) to enter dataMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 27
  28. 28. Data Entry and Saving  Saves changes to disk COMMIT <table names> ;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 28
  29. 29. Listing Table Contents and Other Commands  Allows table contents to be listed SELECT <attribute names> FROM <table names>; SELECT * FROM PRODUCT; SELECT P_CODE, P_DESCRIPT, P_INDATE, P_ONANG, P_MIN, P_PRICE, P_DISCOUNT, V_CODE FROM PRODUCT;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 29
  30. 30. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 30
  31. 31. Listing Table Contents and Other Commands  UPDATE command makes data entry corrections UPDATE PRODUCT SET P_INDATE=’01/18/2003’ WHERE P_CODE=’13-Q2/P2’MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 31
  32. 32. Listing Table Contents and Other Commands  ROLLBACK command restores database back to previous condition if COMMIT hasn’t been used ROLLBACK;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 32
  33. 33. Listing Table Contents and Other Commands  DELETE command removes table row DELETE FROM PRODUCT WHERE P_CODE=‘2238/QPD’; DELETE FROM PRODUCT WHERE P_MIN=5; 5 ROWS ARE DELETEDMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 33
  34. 34. Queries  Creating partial listings of table contents SELECT <column(s)> FROM <table name> WHERE <conditions>; Table 5.4 Mathematical OperatorsMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 34
  35. 35. Examples Mathematical operators SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE V_CODE = 21344;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 35
  36. 36. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 36
  37. 37. Examples Mathematical operators SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE V_CODE <> 21344; Not equalMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 37
  38. 38. Examples Mathematical operators SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE FROM PRODUCT WHERE P_PRICE<=10;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 38
  39. 39. Examples  Mathematical operators on character attributes SELECT P_CODE,P_DESCRIPT,P_ONHAND,P_MIN,P_PRICE FROM PRODUCT WHERE P_CODE < ‘1558-QWI’;  Mathematical operators on dates SELECT P_DESCRIPT,P_ONHAND,P_MIN,P_PRICE,PINDATE FROM PRODUCT WHERE P_INDATE >= ‘01/20/2002’;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 39
  40. 40. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 40
  41. 41. Computed Columns  New columns can be created through valid computations or formulas  Formulas may contain mathematical operators  May contain attributes of any tables specified in FROM clause  Alias is alternate name given to table or column in SQL statement SELECT P_DESCRIPT,P_ONHAND,P_PRICE,P_ONHAND*P_PRICE AS TOTVALUE FROM PRODUCT;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 41
  42. 42. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 42
  43. 43. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 43
  44. 44. Operators Logical: AND, OR, NOTSELECT *FROM PRODUCTWHERE V_CODE = 21344OR V_CODE = 24288;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 44
  45. 45. Operators  Rules of precedence  Conditions within parenthesis executed first  Boolean algebra SELECT * P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE (P_PRICE<50 AND P_INDATE>’01/05/2002’) OR V_CODE=24288;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 45
  46. 46. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 46
  47. 47. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 47
  48. 48. Special Operators  BETWEEN - defines limitsSELECT * FROM PRODUCT WHERE P_PRICE BETWEEN 500.00 AND 100.00;  IS NULL - checks for nulls SELECT P_CODE, P_DESCRIPT FROM PRODUCT WHERE P_MIN IS NULL;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 48
  49. 49. Special Operators  LIKE - checks for similar string (wildcard)SELECT V_NAME, V_CONTACT, V_AREACODE, V_PHONEFROM VENDOR WHERE V_CONTACT LIKE ‘Smith%’;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 49
  50. 50. Special Operators  IN - checks for value in a set SELECT * FROM PRODUCT WHERE V_CODE=21344 OR V_CODE=24288; SELECT * FROM PRODUCT WHERE V_CODE IN (21344,24288); MORE EFFICIENTLYMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 50
  51. 51. Special Operators  EXISTS – NOT NULL DELETE FROM PRODUCT WHERE P_CODE EXISTS; SELECT FROM PRODUCT WHERE V_CODE EXISTS; SELECT FROM PRODUCT WHERE V_CODE IS NOT NULL;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 51
  52. 52. Advanced Data Management Commands  ALTER - changes table structure  ADD - adds column  MODIFY - changes column characteristics ALTER TABLE <table name> ADD (<column name> <new column characteristics>); ALTER TABLE <table name> MODIFY (<column name> <new column characteristics>);MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 52
  53. 53. Advanced Data Management Commands  ALTER - changes table structure  ADD - adds column  MODIFY - changes column characteristicsMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 53
  54. 54. Advanced Data Management Commands  Changing a column’s data type: CHANGE FROM ALTER TABLE PRODUCT INTEGER TO MODIFY (V_CODE CHAR(5)); CHAARCTERS  Changing attribute characteristics ALTER TABLE PRODUCT Increase width of P_PRICE MODIFY (P_PRICE DECIMAL(9,2));MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 54
  55. 55. Advanced Data Management Commands  Adding a column to the table: Add column named ALTER TABLE PRODUCT P_SALECODE to ADD ( P_SALECODE CHAR(1)); PRODUCT table  Dropping (delete) a column from the table: ALTER TABLE VENDOR DROP COLUMN V_ORDER;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 55
  56. 56. Advanced Data Management Commands (con’t.) Entering data into new column UPDATE PRODUCT Update entries: SET P_SALECODE=‘2’ WHERE P_CODE=‘1546-qq2’;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 56
  57. 57. Advanced Data Management Commands (con’t.) Entering data into new column UPDATE PRODUCT SET P_SALECODE=‘1’ WHERE P_CODE IN (‘2232/QWE’, ‘2232/QTY’);MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 57
  58. 58. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 58
  59. 59. Advanced Data Management Commands (con’t.) UPDATE PRODUCT SET P_SALECODE=‘2’ WHERE P_INDATE<’12/25/2001’; UPDATE PRODUCT SET P_SALECODE=‘1’ WHERE P_INDATE>’01/16/2002 AND P_INDATE<’01/21/2002’;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 59
  60. 60. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 60
  61. 61. Advanced Data Management Commands (con’t.)  Arithmetic operators and rules of precedence Table 5.5MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 61
  62. 62. Advanced Data Management Commands (con’t.)  Arithmetic operators and rules of precedence UPDATE PRODUCT SET P_ONHAND=P_ONHAND+20 WHERE P_CODE=‘2232/QWE’; UPDATE PRODUCT SET P_PRICE=P_PRICE*1.10 WHERE P_PRICE<50.00;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 62
  63. 63. Advanced Data Management Commands (con’t.)  Copying parts of tables  Copy the contents of selected table columns with data values INSERT INTO <receiving table> <receiving table’s column names> SELECT <column names of the columns to be copied> FROM <contributing table name>;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 63
  64. 64. Advanced Data Management Commands (con’t.)  Copying parts of tables NEW TABLE NAME CREATE TABLE (PART PART_CODE CHAR(8) NOT NULL UNIQUE, PART_DESCRIPT CHAR(35) PART_PRICE DECIMAL(8,2), PRIMARY KEY (PART_CODE)); INSERT INTO PART (PART_CODE,PART_DESCRIPT, PART_PRICE) SELECT P_CODE, P_DESCRIPT, P_PRICE FROM PRODUCT; SELECT * FROM PART;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 64
  65. 65. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 65
  66. 66. Advanced Data Management Commands (con’t.)  Deleting a table from database DROP TABLE PART;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 66
  67. 67. Advanced Data Management Commands (con’t.)  Primary and foreign key designation ALTER TABLE LINE ADD PRIMARY KEY (INV_NUMBER, LINE_NUMBER) ADD FOREIGN KEY (INV_NUMBER) REFERENCES INVOICE ADD FOREIGN KEY (PROD_CODE) REFERENCES PRODUCT;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 67
  68. 68. Advanced Data Management Commands (con’t.)  Primary and foreign key designation ALTER TABLE PRODUCT ADD PRIMARY KEY (P_CODE); ALTER TABLE PRODUCT ADD FOREIGN KEY (V_CODE) REFERENCES VENDOR;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 68
  69. 69. More complex queries and sql functions  Ordering listing: list in special order (ascending order to descending order) ORDER BY <attributes> ascending ORDER BY <attributes> DESC descending SELECT P_CODE, P_DESCRIPT, P_INDATE, P_PRICE FROM PRODUCT ORDER BY P_PRICE;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 69
  70. 70. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 70
  71. 71. More complex queries and sql functions  Cascading order sequence ORDER BY <attribute1 attribute2, …> ORDER BY <attribute1, attribute2, …> DESC SELECT EMP_LNAME, EMP_FNAME, EMP_INITIAL, EMP_AREACODE, EMP_PHONE FROM EMPLOYEE ORDER BY EMP_LNAME, EMP_FNAME, EMP_INITIAL;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 71
  72. 72. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 72
  73. 73. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 73
  74. 74. More complex queries and sql functions  Use order by clause in conjunction with other SQL commands SELECT P_DESCRIPT, V_CODE, P_INDATE, P_PRICE FROM PRODUCT WHERE P_INDATE<’01/21/2002’ AND P_PRICE<=50.00 ORDER BY V_CODE, P_PRICE DESC;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 74
  75. 75. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 75
  76. 76. More complex queries and sql functions List unique values SELECT DISTINCT V_CODE FROM PRODUCT;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 76
  77. 77. Aggregate Function OperationsMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 77
  78. 78. Aggregate Function Operations  COUNT (count raw numbers for a given column) SELECT COUNT(DISTINCT V_CODE) FROM PRODUCT; SELECT COUNT(DISTINCT V_CODE) FROM PRODUCT WHERE P_PRICE <= 10.00;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 78
  79. 79. Aggregate Function Operations  MAX and MIN SELECT MIN(P_PRICE) FROM PRODUCT; SELECT P_CODE, P_DESCRIPT, P_PRICE FROM PRODUCT WHERE P_PRICE = MAX(P_PRICE);MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 79
  80. 80. Example Aggregate Function Operations (con’t.)  SUM SELECT SUM(P_ONHAND * P_PRICE) FROM PRODUCT;  AVG SELECT P_DESCRIPT, P_ONHAND, P_PRICE, V_CODE FROM PRODUCT WHERE P_PRICE > (SELECT AVG(P_PRICE) FROM PRODUCT) ORDER BY P_PRICE DESC;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 80
  81. 81.  Grouping data  Creates frequency distributions  Only valid when used with SQL arithmetic functions SELECT P_SALECODE, MIN(P_PRICE) FROM PRODUCT_2 GROUP BY P_SALECODE;  HAVING clause operates like WHERE for grouping output SELECT V_CODE,COUNT(DISTINCT(P_CODE)),AVG(P_PRICE) FROM PRODUCT_2 GROUP BY V_CODE HAVING AVG(P_PRICE) < 10;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 81
  82. 82. More Complex Queries and SQL Functions (con’t.)  Virtual tables: creating a view  CREATE VIEW command  Creates logical table existing only in virtual memory CREATE VIEW PRODUCT_3 AS SELECT P_DESCRIPT, P_ONHAND, P_PRICE FROM PRODUCT WHERE P_PRICE > 50.00; CREATE INDEX P_CODEX  SQL indexes ON PRODUCT(P_CODE);MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 82
  83. 83.  Joining database tables  Data are retrieved from more than one table SELECT PRODUCT.P_DESCRIPT, PRODUCT.P_PRICE, VENDOR.V_NAME, VENDOR.V_CONTACT, VENDOR.V_AREACODE, VENDOR.V_PHONE FROM PRODUCT, VENDOR WHERE PRODUCT.V_CODE = VENDOR.V_CODE;  Recursive queries joins a table to itself SELECT A.EMP_NUM,A.EMP_LNAME,A.EMP_MGR, B.EMP_LNAME FROM EMP A, EMP B WHERE A.EMP_MGR=B.EMP_NUM ORDER BY A.EMP_MGR  Outer joins can be used when ‘null’ values need to be included in query resultMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 83
  84. 84. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 84
  85. 85. Updatable Views  Common operation in production environments is use of batch routines to update master table attributes using transaction data  Batch update routine pools the transactions into a single “batch” to update a master table field in a single operation.  Overnight batch jobs  Not all views are updatable  Example: master PRODUCT and product monthly sales table (PMSALES)MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 85
  86. 86. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 86
  87. 87. UPDATE PRODUCT, PMSALES SET PRODUCT.PROD_ QOH=PROD_QOH-PMS-QTY WHERE PRODUCT.PROD_ID=PMSALES.PROD_ID; This works for ACCESS not for Oracle. If you use Oracle, you need to create an updatable view. Updatable view is a view that can be used to update attributes in base tables.MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 87
  88. 88. Updatable Views  Restrictions  GROUP BY expressions cannot be used  Cannot use set operators---UNION, INTERSECTION, etc.  Most restrictions based on use of JOINS or group operators in viewsMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 88
  89. 89. Updatable Views  Create Updatable viewCREATE VIEW MPSVUPD AS (SET PRODUCT.PROD_ID, PRODUCT.PROD_QOH,PMSALES.PMS_QTYFROM PRODUCT, MPSALES WHERE PRODUCT.PROD_ID=PMSALES.PROD_ID);  Now you can use UPDATE command to update the view UPDATE PMSVUPD SET PROD_QOH=PROD_QOH-PSM-QTY MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 89
  90. 90. Procedural SQL  SQL is a declarative language, not support conditional executions of procedures and looping executions  SQL shortcomings  Doesn’t support execution of stored procedures based on logical condition  Fails to support looping operations  The way to use logical or procedural statements is to embed SQL into programming languagesMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 90
  91. 91. Procedural SQL  Solution  Embedded SQL can be called from within procedural programming languages.  Using embedded SQL is still the most common approach to maintaining procedural capabilities.  Shared Code is isolated and used by all application programs.  Procedural SQL (PL/SQL) stored within the database, executed by DBMS, and invoked by the end user  Triggers  Stored procedures  PL/SQL functionsMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 91
  92. 92. Procedural SQL (con’t.)  Procedural SQL allows the use of procedural code and SQL statements that are stored within the database.  The procedural code is executed by the DBMS when it is invoked by the end user.MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 92
  93. 93. Procedural SQL (con’t.)  End users can use procedural SQL (PL/SQL) to create:  Triggers  Stored procedures  PL/SQL functions (different from SQL built-in functions)MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 93
  94. 94. Triggers  Procedural SQL code invoked before or after data row is selected, inserted, or updated  Associated with a database table  Table may have multiple triggers  Executed as part of transactionMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 94
  95. 95. Triggers  Can enforce particular constraints  Automate critical actions and provide warnings for remedial action  Can update values, insert records, and call procedures  Add processing powerMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 95
  96. 96. Triggers (con’t.)  Oracle example CREATE OR REPLACE TRIGGER TRG_PRODUCT_REORDER AFTER INSERT OR UPDATE OF P_ONHAND ON PRODUCT BEGIN UPDATE PRODUCT SET P_REORDER=1 WHERE P_ONHAND<=P_MIN END;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 96
  97. 97. Stored Procedures  Named collection of procedural and SQL statements stored in database  Invoked by name  Executed as unit  Invoked with EXEC  Used in multiple-update transaction (entire contents of stored procedure will be transmitted and executed, which reduce network transfer delay.MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 97
  98. 98. Stored Procedures  Invoked with EXEC CREATE OR REPLACE PROCEDURE procedure_name (argument IN/OUT data-type, etc) IS/AS BEGIN DECLARE variable name and data type PL/SQL or SQL statements; END; EXEC store_procedure_name (parameter, parameter, …)MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 98
  99. 99. Stored Procedures  Example CREATE OR REPLACE PROCEDURE PROD_REORDER_SET AS BEGIN UPDATE PRODUCT SET P_REORDER=1 WHERE P_ONHAND <=P_MIN; UPDATE PRODUCT SET P_REORDER=0 WHERE P_ONHAND>_MIN; END; EXEC PROD_REORDER_SET;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 99
  100. 100. Stored Procedures  Example CREATE OR REPLACE PROCEDURE PROD_SALE(CODE IN VARCHAR2, QTYSOLD IN NUMBER) AS BEGIN UPDATE PRODUCT SET P_REORDER=P_ONHAND-QTYSOLD; WHERE P_CODE=CODE; END; EXEC PROD_SALE(‘22232/QTY’,3);MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 100
  101. 101. PL/SQL Stored Functions  Named group of procedural and SQL statements that returns a value CREATE FUNCTION function_name (argument IN data-type, etc) RETURN data-type AS BEGIN PL/SQL statements; RETURN (value); …… END;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 101
  102. 102. PL/SQL Stored Functions  Invoked from within stored procedures or triggers  Cannot be invoked from within SQL statements CREATE FUNCTION function_name (argument IN data-type, etc) RETURN data-type AS BEGIN PL/SQL statements; RETURN (value); …… END;MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 102
  103. 103. Converting an ER model into a database structure  Require the some rules  Database of artist  A painter may have many paintings (1:M)  Each painter painted by one and only one painter (so far it is a 1:M) relationships between PAINTER and PAINTING  A painter must have at least painting (cardinality is (1:N)  A painting might (or might not) be exhibited in a gallery (another Table, which is optional to the paining entity)MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 103
  104. 104. Artist Database ERD and Tables Figure 5.55MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 104
  105. 105. MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 105
  106. 106. Table Structure and specific requirements PAINTER(PTR_NUM, PTR_LASTNAME,PTR_FIRSTNAME, PTR)INITIAL, PTR_AREACODE, PTR_PHONE) GALLERY(GAL_NUM,GAL_OWNER, GAL_AREACODE, GAL_PHONE, GAL_RATE) PAINTING(PNTG_NUM, PNTG_TITLE, PNTG_PRICE, PNTG_NUM, GAL_NUM)MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 106
  107. 107. Further action considerations 1. Delete raw of painter will effect the PAINTER’s table and cerate delete anomaly. Impose a DELETE RESTRICT on PAINTER’s table 3. No problem to add raw in PAINTER”s table 5. Change PRT_NUM (primary key) will affects other tables. Make sure we need to create a trigger to automatically take care the case. Use UPDATE CASCADE (all foreign key references to primary key are updated when the primary key is changed) 7. Delete GAL_NUM may cause problem, unless we impose DELETE RESTRICT on GALLERY table. 9. No problem to add GALEERY raw 11. UPDATE CASCADE is used for GALEERY tableMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 107
  108. 108. Creates Tables using SQL CREATE TABLE PAINTER ( PTR_NUM CHAR(4) NOT NULL UNIQUE, PTR_LASTNAME CHAR(15) NOT NULL, PTR_FIRSTNAME CHAR(15) NOT NULL, PTR_INITIAL CHAR(1), PTR_AREACODE CHAR(3), PTR_PHONE CHAR(8), PRIMARY KEY(PTR_NUM));MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 108
  109. 109. Creates Tables using SQL CREATE TABLE GALLERY ( GAL_NUM CHAR(4) NOT NULL UNIQUE, GAL_OWNER CHAR(35) NOT NULL, GAL_AREACODE CHAR(3), GAL_PHONE CHAR(8) NOT NULL, GAL_RATE NUMBER(4,2), PRIMARY KEY(GAL_NUM));MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 109
  110. 110. Creates Tables using SQL CREATE TABLE GALLERY ( GAL_NUM CHAR(4) NOT NULL UNIQUE, GAL_OWNER CHAR(35) NOT NULL, GAL_AREACODE CHAR(3), GAL_PHONE CHAR(8) NOT NULL, GAL_RATE NUMBER(4,2), PRIMARY KEY(GAL_NUM));MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 110
  111. 111. Creates Tables using SQL CREATE TABLE PAINTING ( PNTG_NUM CHAR(4) NOT NULL UNIQUE, PNTG_TITLE CHAR(35) NOT NULL, PNTG_PRICE NUMBER(9,2), PTR_NUM CHAR(4) NOT NULL, GAL_NUM CHAR(4) PRIMARY KEY(GAL_NUM)); FOREIGN KEY (PTR_NUM) REFERENCES PAINTER ON DELETE RESTRICT ON UPDATE CASCADE, FOREIGN KEY (GAL_NUM) REFERENCES GALLERY ON DELETE RESTRICT ON UPDATE CASCADE,MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 111
  112. 112. General Rules Governing Relationships Among Tables SQL statement associated with the combination of Optional and mandatory Vs. Relationships (N:M, 1:M, 1:1)MELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 112
  113. 113. General Rules Governing Relationships Among Tables Figure 5.56: M:N, Both Sides Mandatory Figure 5.57: M:N, Both Sides OptionalMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 113
  114. 114. General Rules Governing Relationships Among Tables Figure 5.58: M:N, One Side Optional Figure 5.59: 1:M, Both Sides MandatoryMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 114
  115. 115. General Rules Governing Relationships Among Tables Figure 5.58: M:N, One Side OptionalMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 115
  116. 116. General Rules Governing Relationships Among Tables Figure 5.58: M:N, One Side OptionalMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 116 Figure 5.59: 1:M, Both Sides Mandatory
  117. 117. General Rules Governing Relationships Among Tables Figure 5.60: 1:M, Both Sides Optional Figure 5.61: 1:M, Many Side Optional, One SideMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL Mandatory 117
  118. 118. General Rules Governing Relationships Among Tables Figure 5.62: 1:M, One Side Optional, One Side Mandatory Figure 5.63: 1:1, Both Sides MandatoryMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 118
  119. 119. General Rules Governing Relationships Among Tables Figure 5.64: 1:1, Both Sides Optional Figure 5.65:MELJUN CORTES, MBA,MPA,BSCS 1:1, OneCORTES ( CSSD04C4) SQLSide Mandatory MELJUN Side Optional, One 119
  120. 120. General Rules Governing Relationships Among Tables Figure 5.66: Weak Entity, Foreign Key Located in Weak Entity Figure 5.67: Multivalued Attributes (New Table in 1:MMELJUN CORTES, MBA,MPA,BSCS MELJUN CORTES ( CSSD04C4) SQL 120 Relationship, Foreign Key CAR_VIN in the New Table
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×