Lab 1 IS380, Fall 2012 Instructions: 1. Use the following data and queries to practice SQL commands from CH5 of Oracle Handbook.. 2. In the spool command line, replace your own path to save the result of your activities. 3. Answer the questions, then turn in the questions and answers along with related queries. Please use Microsoft word for this part and turn in the hard copy. 4. Make sure where you spool your file to. This helps you locate your lab activities. 5. Print the spooled file at the end of the lab before leaving. You can skip printing by saving it on a flash drive and e-mail me the soft copy of the spooled file. 6. You have one week to turn in the Questions and Queries. 7. To login to Oracle server use the following values: User name: Password: Host: cba10g If your username is John and password is smith: Username: john Password: smith Host: cba10g All lowercase 8. Please make sure all tables are dropped before leaving. To quit working, use “exit” command then you are logged out. 9. In all lab activities, syntaxes that MUST be entered at SQL prompt by you are in italic for you to identify them and are ended with semicolon (;). If there is an italic syntax without a semicolon, I forgot to put it in, please add it to the syntax. Drop table newspaper; Create table NEWSPAPER ( Feature VARCHAR2(15) not null, Section CHAR(7), Page NUMBER ); insert into NEWSPAPER insert into NEWSPAPER insert into NEWSPAPER insert into NEWSPAPER select * from newspaper; values values values values ('National News', 'A', 1); ('Sports', 'D', 1); ('Editorials', 'A', 12); ('Business', 'E', 1); Now lets change some component and remove 15 from VARCHAR2 and try to create the table. Reload the following: Drop table newspaper; Create table NEWSPAPER ( Feature VARCHAR2() not null, Section CHAR(7), Page NUMBER 2 ); insert into NEWSPAPER insert into NEWSPAPER insert into NEWSPAPER insert into NEWSPAPER select * from newspaper; values values values values ('National News', 'A', 1); ('Sports', 'D', 1); ('Editorials', 'A', 12); ('Business', 'E', 1); What happens? What is the effect of removing 15? Now enter the following: Drop table newspaper; Create table NEWSPAPER ( Feature VARCHAR2(8) not null, Section CHAR(7), Page NUMBER ); insert into NEWSPAPER insert into NEWSPAPER insert into NEWSPAPER insert into NEWSPAPER select * from newspaper; values values values values ('National News', 'A', 1); ('Sports', 'D', 1); ('Editorials', 'A', 12); ('Business', 'E', 1); What is the result? Why? Pay attention to the error message. Now load the following Drop table newspaper; Create table NEWSPAPER ( Feature VARCHAR2(10) not null, Section CHAR(4), Page NUMBER ); insert into NEWSPAPER insert into NEWSPAPER insert into NEWSPAPER insert into NEWSPAPER select * from newspaper; values values values values ('National News', 'A', 1); ('Sports', 'D', 1); ('Editorials', 'A', 12); ('Business', 'E', 1); What is the result? What happens to the column called “Section”? Why? Now load the following: Drop table newspaper; 3 Create table NEWSPAPER ( Feature VARCHAR2(13) not null, Section CHAR(7), Page NUMBER ); insert into NEWSPAPER insert into NEWSPAPER insert into NEWSPAPER insert into NEWSPAPER select * from newspaper; values values values values ('National News', 'A', 1); ('Sports', 'D', 1); ('Editorials', 'A', 12); ('Business', 'E', 1); Is the result correct now? Now load the following and enter the first Insert line then query the table: Drop table newspaper; Create table NEWSPAPER ( Feature VARCHAR(15) not null, Section CHAR(7), Rate VARCHAR(10), Page NUMBER ); Select * from newspaper; Now what is the result? Why? Which column is added? Now enter the following Insert command lines then query the table: insert into NEWSPAPER values ('Sports', 'D', ‘Poor’, 1); insert into NEWSPAPER values ('Editorials', 'A', ‘Excellent’, 12); insert into NEWSPAPER values ('Business', 'E', ‘Good’, 1); select * from newspaper; Load the following with the exception of the path for spool command. Drop table newspaper; Spool c:\users\cba436moshen\desktop\f11\380\assignments\CH5Prac.txt; Create table NEWSPAPER ( Feature VARCHAR2(15) not null, Section CHAR(7), Page NUMBER ); 4 insert insert insert insert insert insert insert insert insert insert insert insert insert insert into into into into into into into into into into into into into into NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER values values values values values values values values values values values values values values ('National News', 'A', 1); ('Sports', 'D', 1); ('Editorials', 'A', 12); ('Business', 'E', 1); ('Weather', 'C', 2); ('Television', 'B', 7); ('Births', 'F', 7); ('Classified', 'F', 8); ('Modern Life', 'B', 1); ('Comics', 'C', 4); ('Movies', 'B', 4); ('Bridge', 'B', 2); ('Obituaries', 'F', 6); ('Doctor Is In', 'F', 6); select Feature, Section, Page from newspaper; “The above line displays all rows and also states that 14 rows selected, using “set feedback off” will not display the message about 14 rows selected.” “You can set feedback to any number of columns you want to be displayed.” The following makes Oracle not to respond back to you by displaying messages. Set feedback off; Now, key in the following to see the result of “Set feedback off” select Feature, Section, Page from newspaper; Now set it back on by entering the following: Set feedback on; Now, key in the following to see the result of “Set feedback on” select Feature, Section, Page from newspaper; The following is used to display the feedback after line 10. Key in the followings: Set feedback 10; Show feedback; . select Feature, Section, Page from newspaper; Set feedback 20; Show feedback; These commands must be used together. You can set the width of the characters(numbers) displayed to a certain number, by using the following command line: Show numwidth; Now key in the followings: Drop table newspaper; Create table NEWSPAPER ( 5 Feature Section Page ); VARCHAR2(15) not null, CHAR(7), NUMBER(10) insert into NEWSPAPER values ('National News', 'A', 1234567890); insert into NEWSPAPER values ('Sports', 'D', 1); Set numwidth 5; Select * from newspaper; 1. What do you see under “Page” column? Now use the following commands: Set numwidth 10 Select * from newspaper; 2. What difference do you see now? Selecting columns Load the followings: Drop table newspaper; Create table NEWSPAPER ( Feature VARCHAR2(18) not null, Section CHAR(7), Page NUMBER ); insert insert insert insert insert into into into into into NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER values values values values values ('Obituaries', 'F', 6); ('Doctor Is In', 'F', 6); ('Teachers are In', 'F', 8); ('Managers are In', 'F', 5 ); ('borrowers are In', 'C', 7 ); Select * from newspaper; The following shows all the F sections only: Select Feature, Section, Page from NEWSPAPER Where Section = ‘F’; “Using Order by, with select command” 6 Feature will be listed alphabetically in the following query, run the query and pay attention to the items under Feature column: Select Feature, Section, Page from NEWSPAPER Where Section = ‘F’ order by Feature; The following displays pages from smaller to larger (Ascending order): Select Feature, Section, Page from NEWSPAPER Where Section = ‘F’ order by Page; The order of column coming first takes precedence over the others, use the following query: Select Feature, Section, Page from NEWSPAPER Where Section = ‘F’ order by Page, Feature; Select Feature, Section, Page from NEWSPAPER Where Section = ‘F’ order by Feature, Page; 3.What difference do you see in the above two queries? Order By is Ascending by default, to display descending, use the following; Select Feature, Section, Page from NEWSPAPER Where Section = ‘F’ order by Page desc, Feature; Load the following table: Drop table Newspaper; Create table NEWSPAPER ( Feature VARCHAR2(15) not null, Section CHAR(7), Page NUMBER ); insert insert insert insert insert insert insert insert insert insert insert insert into into into into into into into into into into into into NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER values values values values values values values values values values values values ('National News', 'A', 1); ('Sports', 'D', 1); ('Editorials', 'A', 12); ('Business', 'E', 1); ('Weather', 'C', 2); ('Television', 'B', 7); ('Births', 'F', 7); ('Classified', 'F', 8); ('Modern Life', 'B', 1); ('Comics', 'C', 4); ('Movies', 'B', 4); ('Bridge', 'B', 2); 7 insert into NEWSPAPER values ('Obituaries', 'F', 6); insert into NEWSPAPER values ('Doctor Is In', 'F', 6); “Using logical operators(=, >, <, AND, OR) with WHERE clause” Make sure the newspaper table is loaded, then key in the following: Select Feature, Section, Page from NEWSPAPER Where Page = 6; 4.What is the result of the above query? Now load the following queries: Select Feature, Section, Page from NEWSPAPER Where Section = ‘D’; Select Feature, Section, Page from NEWSPAPER Where Page> = 6; 5.What is the result of the above query that uses >=6? Key in the following query: Select Feature, Section, Page from NEWSPAPER Where Page ! = 6; What does the above query generate? “Like” is one of the most powerful feature of SQL . It is able to search through the rows for values that look like a pattern you describe. For example, if you are looking for a word that the third letter is an I use two underline characters; percent sign as follow: “_ _ I” . Looking for all those items starting with Te by using the following query: Select Feature, Section, Page from NEWSPAPER Where Feature LIKE ‘Te%’; 6. What did the above query generate? Looking for items with “i” being the third character: Select Feature, Section, Page from NEWSPAPER Where Feature LIKE ‘__i%’; 7. What is the result of the above query? “Using % sign for attributes with two same characters” The following displays all items containing two Os: 8 Select Feature, Section, Page from NEWSPAPER Where Feature LIKE ‘%o%o%’ ; Load the original Newspaper table now: Drop table newspaper; Create table NEWSPAPER ( Feature VARCHAR2(15) not null, Section CHAR(7), Page NUMBER ); insert insert insert insert insert insert insert insert insert insert insert insert insert insert into into into into into into into into into into into into into into NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER NEWSPAPER values values values values values values values values values values values values values values ('National News', 'A', 1); ('Sports', 'D', 1); ('Editorials', 'A', 12); ('Business', 'E', 1); ('Weather', 'C', 2); ('Television', 'B', 7); ('Births', 'F', 7); ('Classified', 'F', 8); ('Modern Life', 'B', 1); ('Comics', 'C', 4); ('Movies', 'B', 4); ('Bridge', 'B', 2); ('Obituaries', 'F', 6); ('Doctor Is In', 'F', 6); Select * from newspaper; “Testing against a list of values” Using “in” with Section, shows only A,B and F under the Feature column. Key in the following: Select Feature, Section, Page from NEWSPAPER Where Section in (‘A’, ‘B’, ‘F’); Using “not in”, shows all but not A, B, and F: Select Feature, Section, Page from NEWSPAPER Where Section not in (‘A’, ‘B’, ‘F’); The following shows pages between 7 and 10, including 7 and 10. Select Feature, Section, Page from NEWSPAPER Where Page between 7 and 10; 9 “Using Logical operators AND & OR” Key in the following: Select Feature, Section, Page from NEWSPAPER Where Section = ‘F’ and Page > 7; Select Feature, Section, Page from NEWSPAPER Where Section = ‘F’ or Page > 7; Observe the results of the above two queries. “Combining AND & OR” Key in the following: Select Feature, Section, Page from NEWSPAPER Where Section = ‘A’ or Section = ‘B’ and Page > 2; 8. What does the above query generate and why? “The result is not correct, because in AND & OR combination AND takes the precedence. “and Page > 2” only affected the rows for section B. Now let have = signs inside prentices. Use the following to show the effect of AND and OR Combination Select Feature, Section, Page from NEWSPAPER Where Page > 2 and ( Section = ‘A’ or Section = ‘B’) ; 9. Compare the results of the last two queries and explain the difference. Querying info based on the attributes and not the entity, for example, if you do not know the column(entity) name but you have the row’s(attribute) name.” To display the specific section name, key in the following: Select Section from NEWSPAPER Where Feature = ‘Doctor Is In’; Columns will be displayed, then query the proper column by entering the following: Select Feature from NEWSPAPER Where Section = ‘F’ ; “Remember, = sign tests against single value only, if you do require more than one column, then use other operators such as < or >. The following code will generate results for one group only, in this case, F .” 10 Select * from NEWSPAPER where Section = ( select Section from NEWSPAPER Where Feature = ‘Doctor Is In’); “changing the above query to the following: Select * from NEWSPAPER where Section < ( select Section from NEWSPAPER Where Feature = ‘Doctor Is In’); Selecting columns from two tables. Load the following two tables: drop table LOCATION; Create Table LOCATION( City VARCHAR2(25), Country VARCHAR2(25), Continent VARCHAR2(25), Latitude NUMBER, NorthSouth CHAR(1), Longitude NUMBER, EastWest CHAR(1) ); insert into LOCATION values ( 'ATHENS','GREECE','EUROPE',37.58,'N',23.43,'E'); insert into LOCATION values ( 'CHICAGO','UNITED STATES','NORTH AMERICA',41.53,'N',87.38,'W'); insert into LOCATION values ( 'CONAKRY','GUINEA','AFRICA',9.31,'N',13.43,'W'); insert into LOCATION values ( 'LIMA','PERU','SOUTH AMERICA',12.03,'S',77.03,'W'); insert into LOCATION values ( 'MADRAS','INDIA','INDIA',13.05,'N',80.17,'E'); insert into LOCATION values ( 'MANCHESTER','ENGLAND','EUROPE',53.30,'N',2.15,'W'); insert into LOCATION values ( 'MOSCOW','RUSSIA','EUROPE',55.45,'N',37.35,'E'); insert into LOCATION values ( 'PARIS','FRANCE','EUROPE',48.52,'N',2.20,'E'); insert into LOCATION values ( 'SHENYANG','CHINA','CHINA',41.48,'N',123.27,'E'); insert into LOCATION values ( 'ROME','ITALY','EUROPE',41.54,'N',12.29,'E'); insert into LOCATION values ( 'TOKYO','JAPAN','ASIA',35.42,'N',139.46,'E'); insert into LOCATION values ( 'SYDNEY','AUSTRALIA','AUSTRALIA',33.52,'S',151.13,'E'); 11 insert into LOCATION values ( 'SPARTA','GREECE','EUROPE',37.05,'N',22.27,'E'); insert into LOCATION values ( 'MADRID','SPAIN','EUROPE',40.24,'N',3.41,'W'); drop table WEATHER; create table WEATHER ( City VARCHAR2(11), Temperature NUMBER, Humidity NUMBER, Condition VARCHAR2(9) ); insert into WEATHER values insert into WEATHER values insert into WEATHER values insert into WEATHER values insert into WEATHER values insert into WEATHER values insert into WEATHER values ('LIMA',45,79,'RAIN'); ('PARIS',81,62,'CLOUDY'); ('MANCHESTER',66,98,'FOG'); ('ATHENS',97,89,'SUNNY'); ('CHICAGO',66,88,'RAIN'); ('SYDNEY',29,12,'SNOW'); ('SPARTA',74,63,'CLOUDY'); Select * from Location; Select * from weather; “combining info from two separate tables” Following query combines information from Location and Weather tables: Select City, Country from LOCATION Where City IN (select City from WEATHER Where Condition = ‘CLOUDY’); 10. What rows are being displayed as a result of the above query? And why? “The above code does not display other info, using the following code will display other columns as well” Select WEATHER.City, Condition, Temperature, Latitude, NorthSouth, Longitude, EastWest 12 From WEATHER, Location where WEATHER.City = Location.City; Load the table comfort now: drop table COMFORT; create table COMFORT ( City VARCHAR2(13) NOT NULL, SampleDate DATE NOT NULL, Noon NUMBER(3,1), Midnight NUMBER(3,1), Precipitation NUMBER ); insert into COMFORT values ('SAN FRANCISCO', TO_DATE('21-MAR-1999','DD-MON-YYYY'),62.5,42.3,.5); insert into COMFORT values ('SAN FRANCISCO', TO_DATE('22-JUN-1999','DD-MON-YYYY'),51.1,71.9,.1); insert into COMFORT values ('SAN FRANCISCO', TO_DATE('23-SEP-1999','DD-MON-YYYY'),NULL,61.5,.1); insert into COMFORT values ('SAN FRANCISCO', TO_DATE('22-DEC-1999','DD-MON-YYYY'),52.6,39.8,2.3); insert into COMFORT values ('KEENE', TO_DATE('21-MAR-1999','DD-MON-YYYY'),39.9,-1.2,4.4); insert into COMFORT values ('KEENE', TO_DATE('22-JUN-1999','DD-MON-YYYY'),85.1,66.7,1.3); insert into COMFORT values ('KEENE', TO_DATE('23-SEP-1999','DD-MON-YYYY'),99.8,82.6,NULL); insert into COMFORT values ('KEENE', TO_DATE('22-DEC-1999','DD-MON-YYYY'),-7.2,-1.2,3.9); Now query the table by entering the following: Select * from comfort; There is a NULL in the table, right? Using Not Null, will display all non-null rows/columns. “Using Null and Not Null” The following query shows you how to use “not null” command in a query. select City, SampleDate, Precipitation from comfort where Precipitation is not null; Spool off;