Presentation is loading. Please wait.

Presentation is loading. Please wait.

Table maintenance revisited (again) Please use speaker notes for additional information!

Similar presentations


Presentation on theme: "Table maintenance revisited (again) Please use speaker notes for additional information!"— Presentation transcript:

1 Table maintenance revisited (again) Please use speaker notes for additional information!

2 SQL> SELECT * FROM maintain; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 23-JUN-99 456 Blocks 10 12-JUN-99 TY 8 789 Radio 24.99 06-JUL-99 TY 20 678 Warrior 15.99 09-JUN-99 TY 14 890 Calculator 20.75 07-JUL-99 TY 17 7 rows selected. SQL> CREATE TABLE maintain00 2 AS 3 SELECT * FROM maintain 4 WHERE idno < 300; Table created. SQL> SELECT * from maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 SQL> DESC maintain00; Name Null? Type ------------------------------- -------- ---- IDNO VARCHAR2(3) ITEMNAME VARCHAR2(12) PRICE NUMBER(6,2) PURCHASED DATE DEPT CHAR(2) COST NUMBER(6,2) Create copy The notes use a table called maintain. In working with it, I had added multiple records. I decided to create a new table with the same format and the first two records with similar data for this presentation. To do that, I first created a table with the same format and data as the previous table except that I excluded all records with an indo that was not < 300. This got me the two records for Teddy Bear and Dump Truck. Next I described the table. It has the same description as maintain since it was created from maintain.

3 SQL> INSERT INTO maintain00 2 VALUES ('345','Baby Doll', 12, sysdate, NULL, NULL); 1 row created. SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 SQL> DESC maintain00; Name Null? Type ------------------------------- -------- ---- IDNO VARCHAR2(3) ITEMNAME VARCHAR2(12) PRICE NUMBER(6,2) PURCHASED DATE DEPT CHAR(2) COST NUMBER(6,2) Insert This is an example of the INSERT we have already seen. Note that all 6 columns receive data even though two are set up as null.

4 Insert with user entry SQL> INSERT INTO maintain00 2 VALUES ('&userid', '&username', &userprice, '&userpurchase', '&userdept', &usercost); Enter value for userid: 456 Enter value for username: Blocks Enter value for userprice: 10 Enter value for userpurchase: 12-JUN-00 Enter value for userdept: TY Enter value for usercost: 8 old 2: VALUES ('&userid', '&username', &userprice, '&userpurchase', '&userdept', &usercost) new 2: VALUES ('456', 'Blocks', 10, '12-JUN-00', 'TY', 8) 1 row created. SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 In this example, all of the values have been set up as variables for user input. When the statement is executed, the user is prompted to enter the information. The information is used to create the record that is entered into the table named maintain00.

5 SQL> edit Insert Edit takes you into the editor. The code was then saved on my floppy disk as insertmain00.sql

6 Insert SQL> @a:\insertmain00.sql Enter value for userid: 478 Enter value for username: Football Enter value for userprice: 14.98 Enter value for userpurchase: 10-JUN-00 Enter value for userdept: TY Enter value for usercost: 11 old 2: VALUES ('&userid', '&username', &userprice, '&userpurchase', '&userdept', &usercost) new 2: VALUES ('478', 'Football', 14.98, '10-JUN-00', 'TY', 11) 1 row created. Enter value for userid: 488 Enter value for username: Jump Rope Enter value for userprice: 5.99 Enter value for userpurchase: 12-JUN-00 Enter value for userdept: TY Enter value for usercost: 5 old 2: VALUES ('&userid', '&username', &userprice, '&userpurchase', '&userdept', &usercost) new 2: VALUES ('488', 'Jump Rope', 5.99, '12-JUN-00', 'TY', 5) 1 row created. SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 488 Jump Rope 5.99 12-JUN-00 TY 5 SQL> @ a:\insertmain00.sql The saved file insertmain00.sql was executed twice using @. The user was prompted for data and the rows/records were added to the table.

7 SQL> CREATE TABLE movemain00 2 (itemnum VARCHAR2(3), 3 itemname VARCHAR2(12), 4 itemcost NUMBER(6,2), 5 itemprice NUMBER(6,2)); Table created. SQL> DESC maintain00; Name Null? Type ------------------------------- -------- ---- IDNO VARCHAR2(3) ITEMNAME VARCHAR2(12) PRICE NUMBER(6,2) PURCHASED DATE DEPT CHAR(2) COST NUMBER(6,2) SQL> DESC movemain00; Name Null? Type ------------------------------- -------- ---- ITEMNUM VARCHAR2(3) ITEMNAME VARCHAR2(12) ITEMCOST NUMBER(6,2) ITEMPRICE NUMBER(6,2) Create table NOTE: maintain00 has 6 columns while the new table movemain00 only has 4. The types are similar for idno and itemnum, itemname and itemname, price and itemprice, cost and itemcost. There is no equivalent for purchased or dept and the order of price and cost have been reversed.

8 Insert SQL> INSERT INTO movemain00 2 SELECT idno, itemname, cost, price 3 FROM maintain00 4 WHERE price > 15; 2 rows created. SQL> SELECT * FROM movemain00; ITE ITEMNAME ITEMCOST ITEMPRICE --- ------------ --------- --------- 123 Teddy Bear 20 20 234 Dump Truck 14.36 15.95 SQL> DESC maintain00; Name Null? Type ------------------------------- -------- ---- IDNO VARCHAR2(3) ITEMNAME VARCHAR2(12) PRICE NUMBER(6,2) PURCHASED DATE DEPT CHAR(2) COST NUMBER(6,2) SQL> DESC movemain00; Name Null? Type ------------------------------- -------- ---- ITEMNUM VARCHAR2(3) ITEMNAME VARCHAR2(12) ITEMCOST NUMBER(6,2) ITEMPRICE NUMBER(6,2) SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 488 Jump Rope 5.99 12-JUN-00 TY 5 Note that the information is inserted into movemain00 in the order that the columns are listed in the SELECT. #1 #2 #4 #3

9 Insert SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 488 Jump Rope 5.99 12-JUN-00 TY 5 SQL> SELECT * FROM movemain00; ITE ITEMNAME ITEMCOST ITEMPRICE --- ------------ --------- --------- 123 Teddy Bear 20 20 234 Dump Truck 14.36 15.95 SQL> INSERT INTO movemain00 (itemnum, itemname, itemprice) 2 SELECT idno, itemname, price 3 FROM maintain00 4 WHERE idno = '456'; 1 row created. SQL> SELECT * FROM movemain00; ITE ITEMNAME ITEMCOST ITEMPRICE --- ------------ --------- --------- 123 Teddy Bear 20 20 234 Dump Truck 14.36 15.95 456 Blocks 10 Three columns on movemain00 are listed to be filled. The select lists the three fields from maintain00 that contains the data that will be used to create the row/record. The where specifies which row/record from maintain00 contains the three columns that will be used.

10 Alter SQL> ALTER TABLE movemain00 2 ADD (itemdept CHAR(2)); Table altered. SQL> DESC movemain00; Name Null? Type ------------------------------- -------- ---- ITEMNUM VARCHAR2(3) ITEMNAME VARCHAR2(12) ITEMCOST NUMBER(6,2) ITEMPRICE NUMBER(6,2) ITEMDEPT CHAR(2) SQL> UPDATE movemain00 2 SET itemdept = (SELECT dept 3 FROM maintain00 4 WHERE idno = '123') 5 WHERE itemnum = '234'; 1 row updated. SQL> SELECT * FROM movemain00; ITE ITEMNAME ITEMCOST ITEMPRICE IT --- ------------ --------- --------- -- 123 Teddy Bear 20 20 234 Dump Truck 14.36 15.95 TY 456 Blocks 10 First I added a column to movemain00. The new column is itemdept. The results are shown below. I want to update the table movemain00 by setting the new column itemdept equal to the dept that is assigned to the record with idno 123 in the table maintain00. The inner select locates record 123 in maintain00 and selects the dept which is TY. It then sets itemdept = TY for the row with itemnum = 234. SQL> SELECT idno, dept 2 FROM maintain00; IDN DE --- -- 123 TY 234 TY 345 456 TY 478 TY 488 TY

11 Update SQL> SELECT * FROM movemain00; ITE ITEMNAME ITEMCOST ITEMPRICE IT --- ------------ --------- --------- -- 123 Teddy Bear 20 20 234 Dump Truck 14.36 15.95 TY 456 Blocks 10 SQL> UPDATE movemain00 2 SET itemdept = (SELECT itemdept 3 FROM movemain00 4 WHERE itemnum = '234') 5 WHERE itemnum = '456'; 1 row updated. SQL> SELECT * FROM movemain00; ITE ITEMNAME ITEMCOST ITEMPRICE IT --- ------------ --------- --------- -- 123 Teddy Bear 20 20 234 Dump Truck 14.36 15.95 TY 456 Blocks 10 TY The inner select finds row 234 and returns the itemdept which is TY. The outer UPDATE assigns the selected TY to itemnum 456.

12 SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 488 Jump Rope 5.99 12-JUN-00 TY 5 6 rows selected. SQL> @ a:\insertmain00 Enter value for userid: 567 Enter value for username: Tea Set Enter value for userprice: 9 Enter value for userpurchase: 09-JUN-00 Enter value for userdept: TY Enter value for usercost: 7.5 old 2: VALUES ('&userid', '&username', &userprice, '&userpurchase', '&userdept', &usercost) new 2: VALUES ('567', 'Tea Set', 9, '09-JUN-00', 'TY', 7.5) 1 row created. SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 488 Jump Rope 5.99 12-JUN-00 TY 5 567 Tea Set 9 09-JUN-00 TY 7.5 Insert I decided to add another record with a cost less than 8 to the table. I used my insertmain00 to do this.

13 Delete SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 488 Jump Rope 5.99 12-JUN-00 TY 5 567 Tea Set 9 09-JUN-00 TY 7.5 7 rows selected. SQL> DELETE FROM maintain00 2 WHERE cost < 8; 2 rows deleted. SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 Note that 2 rows were deleted.

14 Commit, rollback SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 488 Jump Rope 5.99 12-JUN-00 TY 5 567 Tea Set 9 09-JUN-00 TY 7.5 7 rows selected. SQL> commit; Commit complete. I added the Jump Rope and Tea Set back into the file and then did a commit to make these changes permanent. SQL> rollback; Rollback complete. SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 488 Jump Rope 5.99 12-JUN-00 TY 5 567 Tea Set 9 09-JUN-00 TY 7.5 7 rows selected. Note that the rollback after the commit has no impact. The commit has made the changes permanent so there is nothing to rollback

15 Commit, rollback SQL> DELETE FROM maintain00 2 WHERE cost < 8; 2 rows deleted. SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 SQL> rollback; Rollback complete. SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 488 Jump Rope 5.99 12-JUN-00 TY 5 567 Tea Set 9 09-JUN-00 TY 7.5 7 rows selected. The DELETE FROM statement removed two records from the table. Rollback, rolled back the delete and restored the two records to the table.

16 Savepoint SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 488 Jump Rope 5.99 12-JUN-00 TY 5 567 Tea Set 9 09-JUN-00 TY 7.5 7 rows selected. SQL> UPDATE maintain00 2 SET purchased = '12-MAY-00' 3 WHERE idno = '123'; 1 row updated. SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 12-MAY-00 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 488 Jump Rope 5.99 12-JUN-00 TY 5 567 Tea Set 9 09-JUN-00 TY 7.5 7 rows selected. SQL> SAVEPOINT date_update; Savepoint created. The first record in the table was updated to include a purchase date. A savepoint named date_update was created.

17 SQL> DELETE FROM maintain00 2 WHERE idno = '345'; 1 row deleted. SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 12-MAY-00 TY 20 234 Dump Truck 15.95 TY 14.36 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 488 Jump Rope 5.99 12-JUN-00 TY 5 567 Tea Set 9 09-JUN-00 TY 7.5 6 rows selected. SQL> ROLLBACK TO date_update; Rollback complete. SQL> SELECT * FROM maintain00; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 12-MAY-00 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 19-JUN-00 456 Blocks 10 12-JUN-00 TY 8 478 Football 14.98 10-JUN-00 TY 11 488 Jump Rope 5.99 12-JUN-00 TY 5 567 Tea Set 9 09-JUN-00 TY 7.5 7 rows selected. SQL> SAVEPOINT date_update; Savepoint created. Savepoint This is the savepoint shown on the previous slide. After the savepoint was created, I deleted a record. The accompanying select shows that the record for Baby Doll has been deleted. I then did a rollback to the savepoint named date_update. Since this savepoint was prior to the delete, the record was restored.

18 SQL> Rollback; Rollback complete. SQL> SELECT * FROM maintain; IDN ITEMNAME PRICE PURCHASED DE COST --- ------------ --------- --------- -- --------- 123 Teddy Bear 20 TY 20 234 Dump Truck 15.95 TY 14.36 345 Baby Doll 12 23-JUN-99 456 Blocks 10 12-JUN-99 TY 8 789 Radio 24.99 06-JUL-99 TY 20 678 Warrior 15.99 09-JUN-99 TY 14 890 Calculator 20.75 07-JUL-99 TY 17 7 rows selected. Rollback This is not a named rollback so it rollback to the commit statement which means the update of the purchase date is no longer in place.


Download ppt "Table maintenance revisited (again) Please use speaker notes for additional information!"

Similar presentations


Ads by Google