drop table comfort

advertisement
Lab 3
Instructions:
1. Use the following data and queries to practice SQL commands from CH15 & 17.
2. In the spool command line, replace your own path to save the result of your activities.
3. Answer questions A-I then turn in the questions and answers only. Please use Microsoft word
for this part and turn in the hard copy. Spooled file is not needed.
4. Make sure where you spool your file to. This helps you locate your lab activities.
5. Let me check your spooled file at the end of the lab before leaving please.
6. You have one week to turn in the Questions and answers.
7. Please make sure all tables are dropped before leaving. To quit working, use “exit” command
then you are logged out.
drop table comfort;
create table COMFORT (
City
VARCHAR2(13) NOT NULL,
SampleDate
Noon
Midnight
DATE NOT NULL,
NUMBER(3,1),
NUMBER(3,1),
Precipitation NUMBER
);
insert into COMFORT values ('SAN FRANCISCO',
TO_DATE('21-MAR-2003','DD-MON-YYYY'),62.5,42.3,.5);
insert into COMFORT values ('SAN FRANCISCO',
TO_DATE('22-JUN-2003','DD-MON-YYYY'),51.1,71.9,.1);
insert into COMFORT values ('SAN FRANCISCO',
TO_DATE('23-SEP-2003','DD-MON-YYYY'),NULL,61.5,.1);
insert into COMFORT values ('SAN FRANCISCO',
TO_DATE('22-DEC-2003','DD-MON-YYYY'),52.6,39.8,2.3);
insert into COMFORT values ('KEENE',
TO_DATE('21-MAR-2003','DD-MON-YYYY'),39.9,-1.2,4.4);
insert into COMFORT values ('KEENE',
TO_DATE('22-JUN-2003','DD-MON-YYYY'),85.1,66.7,1.3);
insert into COMFORT values ('KEENE',
TO_DATE('23-SEP-2003','DD-MON-YYYY'),99.8,82.6,NULL);
insert into COMFORT values ('KEENE',
TO_DATE('22-DEC-2003','DD-MON-YYYY'),-7.2,-1.2,3.9);
Use the following to display the table columns and…
Describe Comfort;
Select * from comfort;
Inserting time:
When inserting time, if no time is specified then the system will use the midnight by default,
otherwise shows the time used in the insert clause.
Execute the following Insert command:
1. insert into comfort values ('LA', to_date('06/22/2003','MM/DD/yyyy HH24:MI'), 56.7,43.8,0);
Now query comfort table:
Select * from comfort;
Now execute the following:
2. select city, sampledate, TO_CHAR(sampledate,'HH24:MI:SS') AS TimeofDay, Noon
from comfort where City = 'LA';
A. What is the time under Time of Day?
Now key in the following, then repeat step 2
3. insert into comfort values ('LA', to_date('06/22/2003 1:35','MM/DD/yyyy HH24:MI'),
56.7,43.8,0);
B. What is the difference between the two insert command lines of step 1 and 3 and why?
Query the table by using:
4. Select * from Comfort where City = ‘Keene’;
What is the result? _____________________________________
Now use the following query and pay attention to the difference:
5. Select * from Comfort where City = ‘KEENE’;
C. Why step 4 did not generate any output?
Update Command
You can use Update command to update rows of table. Before using Update and Rollback
commands, execute Commit command:
Commit;
Commit causes your previous entries not to be rolled back when executing Rollback command.
We will use Rollback after we use Update command in step 7 to see the effect of Rollback.
Execute the following:
6. Update COMFORT set Precipitation = .1, Midnight = 74.1 where city = ‘KEENE’ and
SampleDate = ’22-DEC-03’;
Now execute the following to see the result of update command:
7. Select * from Comfort where City = ‘KEENE’;
To reverse the effect of the step 6 use rollback command:
8. Rollback;
Now query the table again and compare the result with that of step 5.
9. Select * from Comfort where City = ‘KEENE’;
Using Insert with Select will run the Select Query of the table and inserts the query result into
the new row and creates the row, in this case, city name is Walpole. Execute the following and
see the result by running the query in step 11.
10. insert into comfort
(sampledate, precipitation, city, noon, midnight)
select '22-DEC-03', precipitation, 'walpole', noon, midnight from comfort
where city = 'KEENE' and sampledate = '22-DEC-03';
11. select * from comfort;
D. What is the result of the step 10 command line?
Savepoints are parts of table that can be created and saved to refer/rollback to in later time.
To see the effect of savepoint, let insert a new row in the table first:
12. insert into comfort values ('walpole','22-APR-03',50.1,24.8,0);
select * from comfort;
Now, create a savepoint called A by executing the following:
13. savepoint A;
Step 13, saves table with all the entries including the one in step 12.
Now add another row to the table by executing the following:
14. insert into comfort values ('walpole','27-MAY-03',63.7, 33.8, 0);
select * from comfort;
Now use rollback to savepoint A then run the query:
15. Rollback to savepoint A;
select * from comfort;
E. What is the effect of savepoint? What command do you use to see the effect of
savepoint?
Execute Commit command now:
Commit;
DELETE is used to remove a row/rows from the table:
16. delete from comfort where city = 'walpole';
select * from comfort;
Now, use rollback command to undo the delete command.
Rollback;
select * from comfort;
Truncate deletes rows that cannot be rolled back. It is a very powerful tool to purge tables.
The following is used to truncate table, but we DO NOT execute this command now.
Truncate Table comfort;
MERGE is used to perform INSERT and UPDATE at the same time, if the update
condition does not meet, then Oracle will only do the INSERT. To merge, we need two tables.
Load the following to create comfort2:
drop table COMFORT2;
create table COMFORT2 (
City
VARCHAR2(13) NOT NULL,
SampleDate
Noon
Midnight
DATE NOT NULL,
NUMBER(3,1),
NUMBER(3,1),
Precipitation NUMBER
);
insert into comfort2 values ('KEENE', '21-MAR-03',55, -2.2, 4.4);
insert into comfort2 values ('KEENE', '22-DEC-03',55, 66, 0.5);
insert into comfort2 values ('KEENE', '16-MAY-03',55, 55, 1);
execute the following queries for both tables:
17. select * from comfort where city = 'KEENE';
18. select * from comfort2 where city = 'KEENE';
In merge command, Oracle uses alias for two tables as C1 for Target/Comfort and C2 for
Source/Comfort2 table.
Now execute the following long command line:
merge into comfort C1
using (select City, sampledate, noon, midnight, precipitation from comfort2) C2
on (C1.City = C2.City and C1.sampledate=C2.sampledate)
when matched then update set Noon = C2.Noon when not matched then
insert (C1.City, C1.sampledate, C1.noon, C1.midnight, C1.Precipitation)
Values (C2.City, C2.sampledate, C2.Noon, C2.Midnight, C2.Precipitation);
19. select * from comfort where city = 'KEENE';
20. select * from comfort2 where city = 'KEENE';
Compare the output of steps 19&20 with steps 17&18 and observe the difference.
Load table Trouble:
Drop table Trouble;
create table Trouble (
City
VARCHAR2(13) NOT NULL,
SampleDate DATE NOT NULL,
Noon
NUMBER(3,1),
Midnight
NUMBER(3,1),
Precipitation NUMBER
);
insert into Trouble values ('pleasant lake' , To_Date('21-MAR-1999','DD-MON-YYYY'), 39.99,
-1.31, 3.6);
insert into Trouble values ('pleasant lake' , To_Date('22-JUN-1999','DD-MON-YYYY'), 101.44,
86.2, 1.63);
insert into Trouble values ('pleasant lake' , To_Date('23-sep-1999','dd-mon-yyyy'), 92.85, 79.6,
1.00003);
insert into Trouble values ('pleasant lake' , To_Date('22-DEC-1999','dd-mon-yyyy'),-17.445, 10.4, 2.4);
F. Are all rows created? If not why?
Now create the table again with the following values:
Drop Table Trouble;
create table Trouble (
City
VARCHAR2(13) NOT NULL,
SampleDate DATE NOT NULL,
Noon
NUMBER(4,1),
Midnight
NUMBER(4,1),
Precipitation NUMBER
);
insert into Trouble values ('pleasant lake' , To_Date('21-MAR-1999','DD-MON-YYYY'), 39.99,
-1.31, 3.6);
insert into Trouble values ('pleasant lake' , To_Date('22-JUN-1999','DD-MON-YYYY'), 101.44,
86.2, 1.63);
insert into Trouble values ('pleasant lake' , To_Date('23-sep-1999','dd-mon-yyyy'), 92.85, 79.6,
1.00003);
insert into Trouble values ('pleasant lake' , To_Date('22-DEC-1999','dd-mon-yyyy'),-17.445, 10.4, 2.4);
G. Are all rows created? What difference do you see this time?
Creating Keys
There are different ways to create keys in Oracle. Followings are examples of key creation in
Oracle.
Drop table trouble;
create table Trouble (
City
VARCHAR2(13) NOT NULL,
SampleDate DATE NOT NULL,
Noon
NUMBER(4,1),
Midnight
NUMBER(4,1),
Precipitation NUMBER,
constraints trouble_uq unique (City, sampleDate)
);
"Creating PK, using Constraints clause"
Drop table trouble;
create table Trouble(
City
VARCHAR2(13) NOT NULL,
SampleDate DATE NOT NULL,
Noon
NUMBER(4,1),
Midnight
NUMBER(4,1),
Precipitation NUMBER,
constraints trouble_pk primary key (City, sampleDate)
);
"Creating Primary Key, not using constraint clause"
Drop table worker;
Create table worker (
Name
varchar2(25) Primary Key,
Age
Number,
Lodging
varchar2(15)
);
/* Creating Foreign Key*/
Drop table lodging;
Create table LODGING (
Name
varchar2(25),
Age
Number,
Lodging
varchar2(15)Primary Key
);
Drop Table worker;
Create table worker (
Name
varchar2(25) ,
Age
Number,
Lodging
varchar2(15),
constraint worker_pk primary key (name),
foreign key (Lodging) references LODGING (lodging)
);
Now drop table lodging
21. Drop table lodging;
H. What is the result of step 21?
Now drop table worker by using
22. Drop table worker;
Then
23. Drop table lodging;
I. Comparing the result of steps 21, 22 and 23, what do you conclude?
Now, load tables Lodging and Worker again, then use the following command line to drop table
Lodging first then Worker:
24. Drop table lodging cascade constraint;
Cascade Constraint is used with Drop Table command to drop tables with referential integrity.
Make sure table Trouble is created, then query the table:
Drop Table Trouble;
create table Trouble (
City
VARCHAR2(13) NOT NULL,
SampleDate DATE NOT NULL,
Noon
NUMBER(4,1),
Midnight
NUMBER(4,1),
Precipitation NUMBER
);
insert into Trouble values ('pleasant lake' , To_Date('21-MAR-1999','DD-MON-YYYY'), 39.99,
-1.31, 3.6);
insert into Trouble values ('pleasant lake' , To_Date('22-JUN-1999','DD-MON-YYYY'), 101.44,
86.2, 1.63);
insert into Trouble values ('pleasant lake' , To_Date('23-sep-1999','dd-mon-yyyy'), 92.85, 79.6,
1.00003);
insert into Trouble values ('pleasant lake' , To_Date('22-DEC-1999','dd-mon-yyyy'),-17.445, 10.4, 2.4);
Select * from trouble;
Now key in the following:
25. Describe trouble;
"Altering Table", we can alter/modify table by using the following clause to change the values:
alter table trouble add (condition varchar2(9) , Wind
alter table trouble modify(Wind varchar2(9));
26. Describe Trouble;
Compare the output of steps 25 and 26
Number(9));
Query table trouble first, then use the following Update clause to update the table then query it
again to see the difference:
Select * from trouble;
"Updating Table"
Update trouble set condition='sunny';
Update trouble set wind='Moderate';
Select * from trouble;
Now execute rollback then query the table first, then use the following alter clause to make the
column Condition unused:
alter table trouble set unused column condition;
Select * from trouble;
To remove a column from a table you can use Unused Column or Drop Column in an Alter
command line. The difference which is transparent to the user is that Drop Column causes more
internal operations for the database comparing to Unused column.
"Dropping a column" will drop the column from the table:
alter table trouble drop column wind;
Select * from trouble;
"Creating view", In tables with large number of data, at times we need to create a short portion
to “view” certain items. A view can be created from a table by using the followings:
Drop view rain;
Create view RAIN as select City, Precipitation from Trouble;
create or replace view rain as select * from trouble with read only;
Select * from rain;
"Querying the view", a view can be queried the same way a table is queried:
select noon from rain where noon='40';
select noon from rain;
"Creating a table from a table", you can create table from other table, in the following command
line, table named Temp is created from table Trouble:
Drop table temp;
Create table Temp as select city, precipitation from trouble;
Select * from temp;
Download