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

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

Embed Size (px)

Citation preview

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

Table maintenance revisited (again)

Please use speaker notes for additional information!

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

SQL> SELECT * FROM maintain;

IDN ITEMNAME PRICE PURCHASED DE COST--- ------------ --------- --------- -- ---------123 Teddy Bear 20 TY 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 23-JUN-99456 Blocks 10 12-JUN-99 TY 8789 Radio 24.99 06-JUL-99 TY 20678 Warrior 15.99 09-JUN-99 TY 14890 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 20234 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 copyCreate 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.

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

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 20234 Dump Truck 15.95 TY 14.36345 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)

InsertInsert

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.

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

Insert with user entryInsert with user entry

SQL> INSERT INTO maintain00 2 VALUES ('&userid', '&username', &userprice, '&userpurchase', '&userdept', &usercost);Enter value for userid: 456Enter value for username: BlocksEnter value for userprice: 10Enter value for userpurchase: 12-JUN-00Enter value for userdept: TYEnter value for usercost: 8old 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 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 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.

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

SQL> edit

InsertInsert

Edit takes you into the editor. The code was then saved on my floppy disk as

insertmain00.sql

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

InsertInsert

SQL> @a:\insertmain00.sqlEnter value for userid: 478Enter value for username: FootballEnter value for userprice: 14.98Enter value for userpurchase: 10-JUN-00Enter value for userdept: TYEnter value for usercost: 11old 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: 488Enter value for username: Jump RopeEnter value for userprice: 5.99Enter value for userpurchase: 12-JUN-00Enter value for userdept: TYEnter value for usercost: 5old 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 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11488 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.

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

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 tableCreate tableNOTE: 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.

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

InsertInsert

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 20234 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 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11488 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

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

InsertInsert SQL> SELECT * FROM maintain00;

IDN ITEMNAME PRICE PURCHASED DE COST--- ------------ --------- --------- -- ---------123 Teddy Bear 20 TY 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11488 Jump Rope 5.99 12-JUN-00 TY 5

SQL> SELECT * FROM movemain00;

ITE ITEMNAME ITEMCOST ITEMPRICE--- ------------ --------- ---------123 Teddy Bear 20 20234 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 20234 Dump Truck 14.36 15.95456 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.

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

AlterAlter

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 20234 Dump Truck 14.36 15.95 TY456 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 TY234 TY345456 TY478 TY488 TY

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

UpdateUpdate SQL> SELECT * FROM movemain00;

ITE ITEMNAME ITEMCOST ITEMPRICE IT--- ------------ --------- --------- --123 Teddy Bear 20 20234 Dump Truck 14.36 15.95 TY456 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 20234 Dump Truck 14.36 15.95 TY456 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.

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

SQL> SELECT * FROM maintain00;

IDN ITEMNAME PRICE PURCHASED DE COST--- ------------ --------- --------- -- ---------123 Teddy Bear 20 TY 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11488 Jump Rope 5.99 12-JUN-00 TY 5

6 rows selected.

SQL> @ a:\insertmain00Enter value for userid: 567Enter value for username: Tea SetEnter value for userprice: 9Enter value for userpurchase: 09-JUN-00Enter value for userdept: TYEnter value for usercost: 7.5old 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 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11488 Jump Rope 5.99 12-JUN-00 TY 5567 Tea Set 9 09-JUN-00 TY 7.5

InsertInsert

I decided to add another record with a cost less than 8 to the table. I used my insertmain00 to do this.

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

DeleteDeleteSQL> SELECT * FROM maintain00;

IDN ITEMNAME PRICE PURCHASED DE COST--- ------------ --------- --------- -- ---------123 Teddy Bear 20 TY 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11488 Jump Rope 5.99 12-JUN-00 TY 5567 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 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11

Note that 2 rows were deleted.

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

Commit, rollback

Commit, rollback

SQL> SELECT * FROM maintain00;

IDN ITEMNAME PRICE PURCHASED DE COST--- ------------ --------- --------- -- ---------123 Teddy Bear 20 TY 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11488 Jump Rope 5.99 12-JUN-00 TY 5567 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 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11488 Jump Rope 5.99 12-JUN-00 TY 5567 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

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

Commit, rollback

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 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 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 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11488 Jump Rope 5.99 12-JUN-00 TY 5567 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.

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

SavepointSavepoint SQL> SELECT * FROM maintain00;

IDN ITEMNAME PRICE PURCHASED DE COST--- ------------ --------- --------- -- ---------123 Teddy Bear 20 TY 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11488 Jump Rope 5.99 12-JUN-00 TY 5567 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 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11488 Jump Rope 5.99 12-JUN-00 TY 5567 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.

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

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 20234 Dump Truck 15.95 TY 14.36456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11488 Jump Rope 5.99 12-JUN-00 TY 5567 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 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 19-JUN-00456 Blocks 10 12-JUN-00 TY 8478 Football 14.98 10-JUN-00 TY 11488 Jump Rope 5.99 12-JUN-00 TY 5567 Tea Set 9 09-JUN-00 TY 7.5

7 rows selected.

SQL> SAVEPOINT date_update;

Savepoint created.

SavepointSavepoint

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.

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

SQL> Rollback;

Rollback complete.

SQL> SELECT * FROM maintain;

IDN ITEMNAME PRICE PURCHASED DE COST--- ------------ --------- --------- -- ---------123 Teddy Bear 20 TY 20234 Dump Truck 15.95 TY 14.36345 Baby Doll 12 23-JUN-99456 Blocks 10 12-JUN-99 TY 8789 Radio 24.99 06-JUL-99 TY 20678 Warrior 15.99 09-JUN-99 TY 14890 Calculator 20.75 07-JUL-99 TY 17

7 rows selected.

RollbackRollback

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.