Mastering Data Manipulation in Oracle8: Insert, Update, Delete
In this chapter, you will delve into the essential SQL commands for data manipulation in Oracle8. You will learn how to insert new rows into tables, update existing records, and delete unwanted data. Detailed examples demonstrate how to handle date values using the TO_DATE function, employ SELECT commands for inserting new data, and execute updates for modifying specific fields. By the end of this section, you will be equipped with the knowledge to efficiently manage data within your Oracle databases.
Mastering Data Manipulation in Oracle8: Insert, Update, Delete
E N D
Presentation Transcript
Chapter 14.Changing Data:Insert, Update, Delete Presented by Victor M. Matos Oracle8 - The Complete Reference. Koch & Loney
Changing Data • In this Chapter you will learn how to • insert new rows into a table • update the values of columns in a row, and • delete rows from a table. Oracle8 - The Complete Reference. Koch & Loney
Sample Data City SampleDate Noon MidNight Precipitation • COMFORT Table SQL> select * from COMFORT; CITY SAMPLEDAT NOON MIDNIGHT PRECIPITATION ------------- --------- --------- --------- ------------- SAN FRANCISCO 21-MAR-93 62.5 42.3 .5 SAN FRANCISCO 22-JUN-93 51.1 71.9 .1 SAN FRANCISCO 23-SEP-93 61.5 .1 SAN FRANCISCO 22-DEC-93 52.6 39.8 2.3 KEENE 21-MAR-93 39.9 -1.2 4.4 KEENE 22-JUN-93 85.1 66.7 1.3 KEENE 23-SEP-93 99.8 82.6 KEENE 22-DEC-93 -7.2 -1.2 3.9 Oracle8 - The Complete Reference. Koch & Loney
Insert • Adding a new row to the COMFORT table insert into COMFORT values ( 'CLEVELAND', TO_DATE('30-JAN-1999', 'DD-MON-YYYY'), 56.7, 43.8, 0); 1 row created CLEVELAND 30-JAN-99 56.7 43.8 0 Oracle default date-format Oracle8 - The Complete Reference. Koch & Loney
Insert • Adding another row to the COMFORT table insert into COMFORT values ( 'CLEVELAND', TO_DATE(’01/31/1999', ’MM/DD/YYYY'), 56.7, 43.8, 0); 1 row created CLEVELAND 31-JAN-99 56.7 43.8 0 Change non-Oracle date values using TO_DATE(…). Indicate current date structure. Oracle8 - The Complete Reference. Koch & Loney
Insert • Adding a time insert into COMFORT values ( 'CLEVELAND', TO_DATE('01/29/1999 1:35', 'MM/DD/YYYY HH24:MI'), 56.7, 43.8, 0); 1 row created CLEVELAND 29-JAN-99 1:10 56.7 43.8 0 Change non-Oracle date values using TO_DATE(…). Indicate current date structure. Oracle8 - The Complete Reference. Koch & Loney
Insert • Giving an explicit list of columns. insert into COMFORT (SampleDate, Precipitation, City, Noon, Midnight) values ( TO_DATE('01/29/1999 1:35', 'MM/DD/YYYY HH24:MI'), NULL, 'CLEVELAND', 56.7, 43.8,); A different sequence of fields NULL means ‘unknown-value’ Oracle8 - The Complete Reference. Koch & Loney
Insert • Using a SELECT command to insert rows. insert into COMFORT (SampleDate, Precipitation, City, Noon, Midnight) select TO_DATE('31-JAN-1999', 'DD-MON-YYYY'), Precipitation, 'CLEVELAND', Noon, Midnight from COMFORT where City = 'KEENE' and SampleDate='22-DEC-93' / KEENE 22-DEC-1993 12:00 -7.2 -1.2 3.9 CLEVELAND 31-JAN-1999 12:00 -7.2 -1.2 3.9 Oracle8 - The Complete Reference. Koch & Loney
DEPARTMENT DNAME DNUMBER MGRSSN MGRSTARTDATE EMPLOYEE FNAME MINIT LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO WORKS_ON ESSN PNO HOURS Insert 1/2 • Use a SELECT command to add all the employees from the ‘RESEARCH’ dept. to the new project ‘X17’. Assign them to 1.5 hours/week. Oracle8 - The Complete Reference. Koch & Loney
333445555 7777 1.5 123456789 7777 1.5 666884444 7777 1.5 453453453 7777 1.5 New records Insert 2/2 • SQL solution insert into WORKS_ON(Essn, Pno, Hours) select Ssn, 'X17', 1.5 from EMPLOYEE where Dno IN ( select Dnumber from DEPARTMENT where Dname LIKE 'RESEARCH%' ) Oracle8 - The Complete Reference. Koch & Loney
Update update COMFORT set Precipitation = .5, MidNight = 73.1 where City = 'KEENE' AND SampleDate = '22-DEC-1993’ KEENE 22-DEC-93 -7.2 73.1 .5 Oracle8 - The Complete Reference. Koch & Loney
Update • Add/Subtract to a field. update COMFORT set Noon = Noon + 10, MidNight = MidNight - 20 where City = 'KEENE' AND SampleDate = '22-DEC-1993’ KEENE 22-DEC-93 -7.2 73.1 .5 KEENE 22-DEC-93 2.8 53.1 .5 Oracle8 - The Complete Reference. Koch & Loney
Update • Use SELECT comand Two attributes at once! update COMFORT set (Noon, MidNight) = (select Humidity, Temperature from WEATHER where City = 'MANCHESTER') where City = 'KEENE' Oracle8 - The Complete Reference. Koch & Loney
Delete • Remove the city of ‘San Francisco’. delete from COMFORT where City = 'SAN FRANCISCO' • Recall the deleted records. RollBack; Oracle8 - The Complete Reference. Koch & Loney
Delete • Remove ALL cities. delete from COMFORT; Table definition is still in the dictionary • Recall the deleted records. RollBack; Oracle8 - The Complete Reference. Koch & Loney