7 7 SQL Data Modification 4Spread throughout chapter 7.

12
7 SQL Data Modification Spread throughout chapter 7

Transcript of 7 7 SQL Data Modification 4Spread throughout chapter 7.

Page 1: 7 7 SQL Data Modification 4Spread throughout chapter 7.

77

SQL Data ModificationSQL Data Modification

Spread throughout chapter 7

Page 2: 7 7 SQL Data Modification 4Spread throughout chapter 7.

77

Changing a TableChanging a Table

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

Page 3: 7 7 SQL Data Modification 4Spread throughout chapter 7.

77

Insert New RecordsInsert New Records

Simplest form - single record

INSERT INTO enrollments

VALUES (‘66419’,’2121’,’’,’’);

Page 4: 7 7 SQL Data Modification 4Spread throughout chapter 7.

77

Insert New RecordsInsert New Records

single record - perhaps not all attributes

INSERT INTO enrollments(stdssn , classindex)

VALUES (‘1113’,’66419’); We can insert more than one record at a time

INSERT INTO enrollments(classindex,stdssn)

VALUES (‘66416’,’1111’), (‘66416’,’2222’);

Page 5: 7 7 SQL Data Modification 4Spread throughout chapter 7.

77

InsertingInserting

DBMS should enforce all constraints - including referential integrity, no value for attribute specified as NOT NULL, duplicate primary key values

All DBMSs may not fully enforce - then user and/or programmer must defend against errors

Page 6: 7 7 SQL Data Modification 4Spread throughout chapter 7.

77

Inserting from a QueryInserting from a Query

Since the result of a query is a table, we can actually load data into a (usually temporary) table as a result of a query

First 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);

Page 7: 7 7 SQL Data Modification 4Spread throughout chapter 7.

77

Inserting from a Query (con)Inserting from a Query (con) Then 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;

Page 8: 7 7 SQL Data Modification 4Spread throughout chapter 7.

77

DELETEDELETE

DELETE 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;

Page 9: 7 7 SQL Data Modification 4Spread throughout chapter 7.

77

UPDATEUPDATE UPDATE can modify one or more attributes in one or

more records. WHERE clause selects records to be modified

UPDATE STUDENT

SET HOMETOWN = ‘Vorhees’

WHERE SSN = ‘1113’;

Can do multiple attributes:UPDATE SECTIONS

SET TIME = ‘MW610’, ROOM = ‘BC125’

WHERE INDEX = ‘66420’;

Page 10: 7 7 SQL Data Modification 4Spread throughout chapter 7.

77

UPDATE (con)UPDATE (con) Can modify multiple records:

UPDATE STUDENT

SET YEAR = ‘Sr’

WHERE numcreditsearned > 90;

Can modify using a calculation:UPDATE SECTIONS

SET STOP = STOP + 5

WHERE DEPT = ‘CSC’ AND COURSE = ‘157’;

Page 11: 7 7 SQL Data Modification 4Spread throughout chapter 7.

77

Basic Data ManagementBasic Data Management

Saving the Table ContentsCOMMIT <table names>;

COMMIT PRODUCT;

Restoring the Table ContentsROLLBACK

Page 12: 7 7 SQL Data Modification 4Spread throughout chapter 7.

77

End Updates – and Thus End DMLEnd Updates – and Thus End DML