Presentation is loading. Please wait.

Presentation is loading. Please wait.

7 7 SQL Data Modification 4Spread throughout chapter 7.

Similar presentations


Presentation on theme: "7 7 SQL Data Modification 4Spread throughout chapter 7."— Presentation transcript:

1 7 7 SQL Data Modification 4Spread throughout chapter 7

2 7 7 Changing a Table 4While there is One SQL statement to obtain info (SELECT), there are 3 for changing a table u INSERT - adds records to a table u DELETE - deletes records from a table u UPDATE - changes records in a table

3 7 7 SQL Command Coverage

4 7 7 Insert New Records 4Simplest form - single record INSERT INTO enrollments VALUES (‘66419’,’2121’,’’,’’);

5 7 7 Insert New Records 4single record - perhaps not all attributes INSERT INTO enrollments(stdssn, classindex) VALUES (‘1113’,’66419’); 4We can insert more than one record at a time INSERT INTO enrollments(classindex,stdssn) VALUES (‘66416’,’1111’), (‘66416’,’2222’);

6 7 7 A Data View and Entry Screen

7 7 7 Inserting 4DBMS should enforce all constraints - including referential integrity, no value for attribute specified as NOT NULL, duplicate primary key values 4All DBMSs may not fully enforce - then user and/or programmer must defend against errors

8 7 7 Basic Data Management 4Data 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);

9 7 7 Inserting from a Query 4Since the result of a query is a table, we can actually load data into a (usually temporary) table as a result of a query 4First need to create table – not covered yet – here’s a simple table creation: CREATE TABLE dept_info (dname CHAR (3), NUM_CLASSES INTEGER, TOTAL_ENROLL INTEGER, MAX_ENROLL INTEGER);

10 7 7 Inserting from a Query (con) 4Then can fill table from query results: INSERT INTO dept_info (dname, NUM_CLASSES, TOTAL_ENROLL, MAX_ENROLL) SELECT DEPT, COUNT(*), SUM (enrollment), MAX (enrollment) FROM SECTIONS GROUP BY DEPT;

11 7 7 4Copying Parts of Tables CREATE TABLE PART PART_CODECHAR(8) NOT NULL UNIQUE, PART_DESCRIPTCHAR(35), PART_PRICEDECIMAL(8,2), PRIMARY KEY(PART_CODE)); INSERT INTO PART (PART_CODE, PART_DESCRIPT, PART_PRICE) SELECT P_CODE, P_DESCRIPT, P_PRICE FROM PRODUCT; Advanced Data Management Commands

12 7 7 The Part Attributes Copied from the PRODUCT Table

13 7 7 DELETE 4DELETE is pretty simple - it removes a set of records (generally that meet some condition) DELETE FROM STUDENT WHERE SSN = ‘1111’; DELETE FROM STUDENT WHERE GPA < 1.5;

14 7 7 Basic Data Management 4Deleting Table Rows DELETE FROM PRODUCT WHERE P_CODE = ‘2238/QPD’; DELETE FROM PRODUCT WHERE P_MIN = 5;

15 7 7 UPDATE 4UPDATE can modify one or more attributes in one or more records. 4WHERE clause selects records to be modified UPDATE STUDENT SET HOMETOWN = ‘Vorhees’ WHERE SSN = ‘1113’; 4Can do multiple attributes: UPDATE SECTIONS SET TIME = ‘MW610’, ROOM = ‘BC125’ WHERE INDEX = ‘66420’;

16 7 7 UPDATE (con) 4Can modify multiple records: UPDATE STUDENT SET YEAR = ‘Sr’ WHERE numcreditsearned > 90; 4Can modify using a calculation: UPDATE SECTIONS SET STOP = STOP + 5 WHERE DEPT = ‘CSC’ AND COURSE = ‘157’;

17 7 7 Basic Data Management 4Making 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’;

18 7 7 UPDATE PRODUCT SET P_SALECODE = ‘1’ WHERE P_CODE IN (‘2232/QWE’, ‘2232/QTY’); No Longer a Figure Selected PRODUCT Table Attributes: Multiple Data Entry Advanced Data Management Commands

19 7 7 UPDATE PRODUCT SET P_SALECODE = ‘2’ WHERE P_INDATE < ‘07/10/1999’; UPDATE PRODUCT SET P_SALECODE = ‘1’ WHERE P_INDATE >= ‘08/15/1999’ AND P_INDATE < ‘08/20/1999’; Advanced Data Management Commands

20 7 7 Selected PRODUCT Table Attributes: Multiple Update Effect Figure 5.18 Advanced Data Management Commands

21 7 7 Basic Data Management 4Saving the Table Contents COMMIT ; COMMIT PRODUCT; 4Restoring the Table Contents ROLLBACK

22 7 7 Figure 5.4 The Contents of the PRODUCT Table

23 7 7 The Arithmetic Operators Table 6.9

24 7 7 End Updates – and Thus End DML


Download ppt "7 7 SQL Data Modification 4Spread throughout chapter 7."

Similar presentations


Ads by Google