Upload
chiquita-kent
View
33
Download
3
Embed Size (px)
DESCRIPTION
SQL Data Modification. Spread throughout chapter 7. Changing 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. Insert New Records. - PowerPoint PPT Presentation
Citation preview
77
SQL Data ModificationSQL Data Modification
Spread 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
77
Insert New RecordsInsert New Records
Simplest form - single record
INSERT INTO enrollments
VALUES (‘66419’,’2121’,’’,’’);
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’);
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
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);
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;
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;
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’;
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’;
77
Basic Data ManagementBasic Data Management
Saving the Table ContentsCOMMIT <table names>;
COMMIT PRODUCT;
Restoring the Table ContentsROLLBACK
77
End Updates – and Thus End DMLEnd Updates – and Thus End DML