Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM.

Similar presentations


Presentation on theme: "1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM."— Presentation transcript:

1 1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM

2 2 Introduction to SQL SQL meets ideal database language requirements: –SQL coverage fits into two categories: Data definition Data manipulation –SQL is relatively easy to learn. –ANSI prescribes a standard SQL.

3 3 Data Definition Commands The Database Model –Simple Database -- PRODUCT and VENDOR tables Each product is supplied by only a single vendor. A vendor may supply many products.

4 4 The Tables and Their Components –The VENDOR table contains vendors who are not referenced in the PRODUCT table. PRODUCT is optional to VENDOR. –Existing V_CODE values in the PRODUCT table must have a match in the VENDOR table. –A few products are supplied factory-direct, a few are made in-house, and a few may have been bought in a special warehouse sale. That is, a product is not necessarily supplied by a vendor. VENDOR is optional to PRODUCT.

5 5

6 6 Creating the Database Structure CREATE DATABASE ; –Example: CREATE DATABASE CH3;

7 7 Some Common SQL Data Types NumericNUMBER(L,D) INTEGER SMALLINT DECIMAL(L,D) CharacterCHAR(L) VARCHAR(L) DateDATE Data TypeFormat

8 8 Creating Table Structures CREATE TABLE ( );

9 9 CREATE TABLE VENDOR (V_CODEFCHAR(5)NOT NULLUNIQUE, V_NAMEVCHAR(35)NOT NULL, V_CONTACTVCHAR(15)NOT NULL, V_AREACODEFCHAR(3)NOT NULL, V_PHONEFCHAR(3)NOT NULL, V_STATEFCHAR(2)NOT NULL, V_ORDERFCHAR(1)NOT NULL, PRIMARY KEY (V_CODE));

10 10 CREATE TABLE PRODUCT( P_CODEVCHAR(10)NOT NULLUNIQUE, P_DESCRIPTVCHAR(35)NOT NULL, P_INDATEDATENOT NULL, P_ONHANDSMALLINTNOT NULL, P_MINSMALLINTNOT NULL, P_PRICEDECIMAL(8,2)NOT NULL, P_DISCOUNTDECIMAL(4,1)NOT NULL, V_CODESMALLINT, PRIMARY KEY (P_CODE), FOREIGN KEY (V_CODE) REFERENCES VENDOR ON DELETE RESTRICT ON UPDATE CASCADE);

11 11 SQL Integrity Constraints –Entity Integrity PRIMARY KEY NOT NULL and UNIQUE –Referential Integrity FOREIGN KEY ON DELETE ON UPDATE

12 12 SQL Command Coverage

13 13 Basic Data Management Data Entry INSERT INTO VALUES (attribute 1 value, attribute 2 value, … etc.); INSERT INTO VENDOR VALUES(‘21225, ’Bryson, Inc.’, ’Smithson’, ’615’,’223-3234’, ’TN’, ’Y’); INSERT INTO PRODUCT VALUES(‘11 QER/31’, ’Power painter, 15 psi., 3-nozzle’, ’07/02/1999’, 8.5, 109.99, 0.00, 25595);

14 14 Saving the Table Contents COMMIT ; COMMIT PRODUCT; Listing the Table Contents SELECT * FROM PRODUCT; SELECT P_CODE, P_DESCRIPT, P_INDATE, P_ONHAND, P_MIN, P-PRICE, P_DISCOUNT, V_CODE FROM PRODUCT;

15 15 Making a Correction UPDATE PRODUCT SET P_INDATE = ‘12/11/96’ WHERE P_CODE = ‘13-Q2/P2’; UPDATE PRODUCT SET P_INDATE = ‘12/11/96’, P_PRICE = 15.99, P_MIN=10 WHERE P_CODE = ‘13-Q2/P2’; Restoring the Table Contents ROLLBACK

16 16 Deleting Table Rows DELETE FROM PRODUCT WHERE P_CODE = ‘2238/QPD’; DELETE FROM PRODUCT WHERE P_MIN = 5;

17 17 Queries Partial Listing of Table Contents SELECT FROM WHERE ; SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE V_CODE = 21344;

18 18 Mathematical Operators

19 19 SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE V_CODE <> 21344;

20 20 SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE FROM PRODUCT WHERE P_PRICE <= 10;

21 21 Using Mathematical Operators on Character Attributes SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE FROM PRODUCT WHERE P_CODE < ‘1558-QWI’;

22 22 Using Mathematical Operators on Dates SELECT P_DESCRIPT, P_ONHAND, P_MIN, P_PRICE, P_INDATE FROM PRODUCT WHERE P_INDATE >= ‘08/15/1999’;

23 23 Logical Operators: AND, OR, and NOT SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE V_CODE = 21344 OR V_CODE = 24288;

24 24 SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE P_PRICE ‘07/15/1999’;

25 25 SELECT P_DESCRIPT, P_INDATE, P_PRICE, V_CODE FROM PRODUCT WHERE (P_PRICE ‘07/15/1999’) OR V_CODE = 24288;

26 26 BETWEEN - used to define range limits. IS NULL - used to check whether an attribute value is null LIKE - used to check for similar character strings. IN - used to check whether an attribute value matches a value contained within a (sub)set of listed values. EXISTS - used to check whether an attribute has a value. In effect, EXISTS is the opposite of IS NULL. Special Operators

27 27 BETWEEN is used to define range limits. SELECT * FROM PRODUCT WHERE P_PRICE BETWEEN 50.00 AND 100.00; SELECT * FROM PRODUCT WHERE P_PRICE > 50.00 AND P_PRICE < 100.00;

28 28 IS NULL is used to check whether an attribute value is null. SELECT P_CODE, P_DESCRIPT FROM PRODUCT WHERE P_MIN IS NULL; SELECT P_CODE, P_DESCRIPT FROM PRODUCT WHERE P_INDATE IS NULL;

29 29 LIKE is used to check for similar character strings. SELECT * FROM VENDOR WHERE V_CONTACT LIKE ‘Smith%’; SELECT * FROM VENDOR WHERE V_CONTACT LIKE ‘SMITH%’;

30 30 IN is used to check whether an attribute value matches a value contained within a (sub)set of listed values. SELECT * FROM PRODUCT WHERE V_CODE IN (21344, 24288); EXISTS is used to check whether an attribute has value. DELETE FROM PRODUCT WHERE P_CODE EXISTS; SELECT * FROM PRODUCT WHERE V_CODE EXISTS;

31 31 Advanced Data Management Commands Changing Table Structures ALTER TABLE MODIFY ( ); ALTER TABLE ADD ( );

32 32 Changing a Column’s Data Type ALTER TABLE PRODUCT MODIFY (V_CODE CHAR(5)); Changing Attribute Characteristics ALTER TABLE PRODUCT MODIFY (P_PRICE DECIMAL(9,2)); Adding a New Column to the Table ALTER TABLE PRODUCT ADD (P_SALECODE CHAR(1));

33 33 The Arithmetic Operators

34 34 Deleting a Table from the Database –DROP TABLE ; DROP TABLE PART;


Download ppt "1 Chapter 6 Structured Query Language (SQL) DATABASE MANAGEMENT SYSTEM."

Similar presentations


Ads by Google