Prepared by M. Sahebjame IS380, Lab 5 Instructions: 1. Use the following data and queries to practice SQL commands from CH 10 and 18. 2. In the spool command line, replace your own path to save the result of your activities. 3. Answer questions A-N 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. You have one week to turn in the Questions and answers. 6. Please make sure all tables are dropped before leaving. To quit working, use “exit” command then you are logged out. 7. All syntaxes are italic and end with semicolon (;), all questions A-N are bold. Conversion and Transformation Functions Getting the date and time from Oracle: Oracle uses the operating system date and time to display the current date and time. The following command line is used to get the current time and date from Oracle. Dual is a small table of Oracle used for testing functions. Do the following before steps 1 & 2: Describe Dual; 1. Select sysdate from dual; 2. Select Current_Date from DUAL; The above two queries display current date. Load Table Holiday Spool h:\123.txt drop table HOLIDAY; create table HOLIDAY ( Holiday VARCHAR2(25), ActualDate DATE, CelebratedDate DATE ); 1 Prepared by M. Sahebjame insert into HOLIDAY values ('NEW YEARS DAY', TO_DATE('01-JAN-2004','DD-MON-YYYY'), TO_DATE('01-JAN-2004','DD-MON-YYYY')); insert into HOLIDAY values ('MARTIN LUTHER KING, JR.', TO_DATE('15-JAN-2004','DD-MON-YYYY'), TO_DATE('19-JAN-2004','DD-MON-YYYY')); insert into HOLIDAY values ('LINCOLNS BIRTHDAY', TO_DATE('12-FEB-2004','DD-MON-YYYY'), TO_DATE('16-FEB-2004','DD-MON-YYYY')); insert into HOLIDAY values ('WASHINGTONS BIRTHDAY', TO_DATE('22-FEB-2004','DD-MON-YYYY'), TO_DATE('16-FEB-2004','DD-MON-YYYY')); insert into HOLIDAY values ('FAST DAY, NEW HAMPSHIRE', TO_DATE('22-FEB-2004','DD-MON-YYYY'), TO_DATE('22-FEB-2004','DD-MON-YYYY')); insert into HOLIDAY values ('MEMORIAL DAY', TO_DATE('30-MAY-2004','DD-MON-YYYY'), TO_DATE('31-MAY-2004','DD-MON-YYYY')); insert into HOLIDAY values ('INDEPENDENCE DAY', TO_DATE('04-JUL-2004','DD-MON-YYYY'), TO_DATE('04-JUL-2004','DD-MON-YYYY')); insert into HOLIDAY values ('LABOR DAY', TO_DATE('06-SEP-2004','DD-MON-YYYY'), TO_DATE('06-SEP-2004','DD-MON-YYYY')); insert into HOLIDAY values ('COLUMBUS DAY', TO_DATE('12-OCT-2004','DD-MON-YYYY'), TO_DATE('11-OCT-2004','DD-MON-YYYY')); insert into HOLIDAY values ('THANKSGIVING', TO_DATE('25-NOV-2004','DD-MON-YYYY'), TO_DATE('25-NOV-2004','DD-MON-YYYY')); Now query the table: Select * from holiday; Now do the following query: 3. Select holiday, actualDate, CelebratedDate from Holiday; 2 Prepared by M. Sahebjame Step 3 query is the same as the following query, do the following query now: Select * from holiday; The Difference Between Two Dates Refer to Holiday table, to find out which holidays are not celebrated on the actual anniversary, we find the difference of the actualdate and the celebrated date and if the answer is not zero , then the result shows the date of the anniversary . Now key in the following: 4. Select holiday, actualDate, celebratedDate from holiday Where celebratedDate – ActualDate != 0; A. How many holidays have not been celebrated on their anniversary? Adding Months: Refer to the output of the step 3 query, if you want to know the date for 6 months after labor day, then do the following, this combination of column and select commands will create a new column called SixMonth and the select clause will add six months to the labor day date and displays the new date. Do the following now: 5. Column SixMonth heading “Six Month” Select ADD_MONTHS (CelebratedDate, 6) As SixMonth from holiday where holiday like ‘LABOR%’; B. What is the date? Subtracting dates: This can be done by using the ADD_MONTHS but using negative sign which causes the subtraction to be performed. Do the following query now: 6. Column SixLess heading “Six Less” Select ADD_MONTHS (CelebratedDate,- 6) -1 As SixLess from holiday where holiday like ‘LABOR%’; C. What is the date? 3 Prepared by M. Sahebjame Least function: Least function chooses the earliest date from a list of dates, whether columns or literals. Key in the following: 7. Select holiday, LEAST(ActualDate, CelebratedDate) As First, ActualDate, CelebratedDate from holiday where ActualDate – CelebratedDate != 0; D. Under what column do you see the result of LEAST function? Load Payday table: drop table PAYDAY; create table PAYDAY ( CycleDate DATE ); insert into PAYDAY values (TO_DATE('15-JAN-2004','DD-MON-YYYY')); insert into PAYDAY values (TO_DATE('15-FEB-2004','DD-MON-YYYY')); insert into PAYDAY values (TO_DATE('15-MAR-2004','DD-MON-YYYY')); insert into PAYDAY values (TO_DATE('15-APR-2004','DD-MON-YYYY')); insert into PAYDAY values (TO_DATE('15-MAY-2004','DD-MON-YYYY')); insert into PAYDAY values (TO_DATE('15-JUN-2004','DD-MON-YYYY')); insert into PAYDAY values (TO_DATE('15-JUL-2004','DD-MON-YYYY')); insert into PAYDAY values (TO_DATE('15-AUG-2004','DD-MON-YYYY')); insert into PAYDAY values (TO_DATE('15-SEP-2004','DD-MON-YYYY')); insert into PAYDAY values (TO_DATE('15-OCT-2004','DD-MON-YYYY')); insert into PAYDAY values (TO_DATE('15-NOV-2004','DD-MON-YYYY')); insert into PAYDAY values (TO_DATE('15-DEC-2004','DD-MON-YYYY')); NEXT_DAY Next_Day computes the date of the next named day of the week (that is, Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, or Saturday) after the given date. For example, suppose payday is always the first Friday after the 15th of the month. The table PAYDAY contains only the pay cycle dates, each one being the 15th of the month, with one row for each month of the year. Do the following query now: 8. select CycleDate from PAYDAY; 4 Prepared by M. Sahebjame E. What day of the month is the cycle date for payday? Now key in the following: 9. Column Payday heading “Pay Day” select NEXT_DAY(CycleDate,'FRIDAY') AS Payday from PAYDAY; F. Do you see any problem? If the payday is going to be Friday, which date in the list does not look correct? Now key in the following and compare the result with that in step 9: 10. column Payday heading “Pay Day” select NEXT_DAY(CycleDate-1,'FRIDAY') AS PayDay from PAYDAY; G. What difference do you see? Why? Load table Birthday drop table BIRTHDAY; create table BIRTHDAY ( FirstName VARCHAR2(15), LastName VARCHAR2(15), BirthDate DATE, Age NUMBER, constraint PK_BIRTHDAY primary key (FirstName, LastName) ); insert into BIRTHDAY values ('GEORGE','SAND', TO_DATE('12-MAY-1946','DD-MON-YYYY'),42); insert into BIRTHDAY values ('ROBERT','JAMES', 5 Prepared by M. Sahebjame TO_DATE('23-AUG-1937','DD-MON-YYYY'),52); insert into BIRTHDAY values ('NANCY','LEE', TO_DATE('02-FEB-1947','DD-MON-YYYY'),42); insert into BIRTHDAY values ('VICTORIA','LYNN', TO_DATE('20-MAY-1949 3:27','DD-MON-YYYY HH24:MI'),42); insert into BIRTHDAY values ('FRANK','PILOT', TO_DATE('11-NOV-1942','DD-MON-YYYY'),42); Select * from birthday; LAST_DAY LAST_DAY produces the date of the last day of the month. Suppose that commissions and bonuses are always paid on the last day of the month. What are those dates in 2004? Do step 11: 11. column EndMonth heading "End Month" select LAST_DAY(CycleDate) AS EndMonth from PAYDAY; MONTHS_BETWEEN Two Dates To calculate each person’s age, compute the months between today’s date and their birth dates, and divide by 12 to get the years. 12. select FirstName, LastName, Birthdate, FLOOR( MONTHS_BETWEEN(SysDate,Birthdate)/12 ) AS Age from BIRTHDAY; H. List all the ages____________________________ FLOOR FLOOR is the intuitive opposite of CEIL. Here is the format for FLOOR and some examples: FLOOR(value) FLOOR(2) = 2 6 Prepared by M. Sahebjame FLOOR(1.3) = 1 CEIL CEIL (for ceiling) simply produces the smallest integer (or whole number) that is greater than or equal to a specific value. Pay special attention to its effect on negative numbers. The following shows the format for CEIL and some examples: CEIL(value) CEIL(2) = 2 CEIL(1.3) = 2 The following table is not on the list of tables on my website, you need to key it in. This table and the two queries followed after, are used to show the usage of Ceil and Floor command. Drop table MyTable; create table myTable( id NUMBER(2), value NUMBER(6,2) ) / insert into myTable(ID, insert into myTable(ID, insert into myTable(ID, insert into myTable(ID, select * from myTable; value)values (1,9); value)values (2,2.11); value)values (3,3.44); value)values (4,-4.21); SELECT id, value,CEIL(value) FROM myTable; I. List values under CEIL column _______________________________ SELECT id, value,FLOOR(value) FROM myTable; J. List values under FLOOR column _______________________________ Combining Date Functions 7 Prepared by M. Sahebjame To find out what date is 6 months from now which is going to be your review date, or… we combine the ADD_MONTHS and LAST_DAY functions with SysDate as follow: 13. select SysDate AS Today, LAST_DAY(ADD_MONTHS(SysDate,6)) + 1 Review from DUAL; In step 13 query we are telling the system to show the result under a column called Review. K. What is the review date? ADD_MONTHS ADD_MONTHS takes the SysDate and adds six months to it. LAST_DAY takes this result and figures the last day of that month. You then add 1 to the date to get the first day of the next month. Now, to see how many days until that review date, you simply subtract today’s date from it as follow. Note the use of parentheses to ensure the proper order of the calculation in the following: 14. select (LAST_DAY(ADD_MONTHS(SysDate,6))+ 1)-SysDate Wait from DUAL; L. How many days is the result of step 14? TO_DATE and TO_CHAR Formatting TO_DATE and TO_CHAR are alike insofar as they both have powerful formatting capabilities. They are opposite insofar as TO_DATE converts a character string or a number into an Oracle date, whereas TO_CHAR converts an Oracle date into a character string. The formats for these two functions are as follows: TO_CHAR(date[,'format'[,'NLSparameters']]) TO_DATE(string[,'format'[,'NLSparameters']]) date must be a column defined as a DATE datatype in Oracle. It cannot be a string, even if it is in the most common date format of DD-MON-YY. The only way to 8 Prepared by M. Sahebjame use a string where date appears in the TO_CHAR function is to enclose it within a TO_DATE function. Now key in the following: select sysdate from dual; string is a literal string, a literal number, or a database column containing a string or a number. In every case but one, the format of string must correspond to that described by format. Only if a string is in the default format can format be left out. The default starts out as ‘DD-MON-YY’, but you can change this with the following: 15. alter session set NLS_DATE_FORMAT = "DD/MON/YYYY"; Now key in the following: select sysdate from dual; To change the date format back to Oracle default format, key in the following: 16. alter session set NLS_DATE_FORMAT = "DD-MON-YYYY"; Now key in the following: select sysdate from dual; The Most Common TO_CHAR Error Always check the date formats when using the TO_CHAR function. The most common error is to interchange the ‘MM’ (Month) format and the ‘MI’ (Minutes) format when formatting the time portion of a date. For example, to view the current time, use the TO_CHAR function to query the time portion of SysDate as follow: 17. select TO_CHAR(SysDate,'HH:MI:SS') Now from DUAL; 9 Prepared by M. Sahebjame M. What date do you get? This example is correct because it uses ‘MI’ to show the minutes. However, users often select ‘MM’ instead—partly because they are also selecting two other pairs of double letters, ‘HH’ and ‘SS’. Selecting ‘MM’ will return the month, not the minutes: 18. select TO_CHAR(SysDate,'HH:MM:SS') NowWrong from DUAL; N. What date do you see now? Compare with step 17 date. For more on TO_DATE and TO_CHAR functions refer to Oracle Reference Book, Chapter 10. Determining certain date: Following Queries determine certain dates based on the current date. For example: If you want to know what the date would be 247 days from today or before current date, do the following quires: 19. Select Sysdate, sysdate +247 from dual; 20. Select Sysdate, sysdate -247 from dual; Power Command: Power command is used to raise the value to an exponent power. The following command line raises all the values of myTable under the column called VALUE to 2. 21. select power(value, 2) from myTable; Chapter 18 commands and activities CANNOT be practiced in CSULB LAB due to security reasons. These are for your reference and can be worked on at home if you have Oracle installed. Creating User 10 Prepared by M. Sahebjame The following is the general format to create user: create user user identified {by password | externally | globally as 'extnm'}; Now key in the following example; create user csulb identified by thebeach; Changing Password: To change a password, use the alter user command: alter user csulb identified by is380; To change your own password, use password command: Password; Then old and new password accordingly To change other users password, use password command with the username as follow: Password csulb; Create user csulb identified by thebeach default tablespace users; Enforcing Password Expiration You can use profiles to manage the expiration, reuse, and complexity of passwords. You can limit the lifetime of a password, and lock an account whose password is too old. You can also force a password to be at least moderately complex, and lock any account that has repeated failed login attempts. For example, if you set the FAILED_LOGIN_ATTEMPTS resource of the user’s profile to 5, then four consecutive failed login attempts will be allowed for the account; the fifth will cause the account to be locked. NOTE If the correct password is supplied on the fifth attempt, the “failed login attempt count” is reset to 0, allowing for five more consecutive unsuccessful login attempts before the account is locked. 11 Prepared by M. Sahebjame In the following listing, the LIMITED_PROFILE profile is created for use by the user CSULB: create profile LIMITED_PROFILE limit FAILED_LOGIN_ATTEMPTS 5; create user CSULB identified by THEBEACH profile LIMITED_PROFILE; grant CREATE SESSION to CSULB; If there are five consecutive failed connection attempts to the CSULB account, the account will be automatically locked by Oracle. When you then use the correct password for the CSULB account, you will receive an error. Connecting to Oracle using a different username or logging in under different username: Connect msahebja@cba10g; Connect csulb@cba10g; Creating Roles: Create role MANAGER; Granting Privileges to a Role: grant select on COMFORT to MANAGER; Granting a Role to Users: grant MANAGER to CSULB; Adding a Password to a Role: alter role MANAGER identified by is380; Removing a Password from a Role: alter role MANAGER not identified; Revoking Privileges from a Role: 12 Prepared by M. Sahebjame revoke SELECT on COMFORT from MANAGER; Dropping a Role To drop a role, use the drop role command, as shown in the following example: drop role MANAGER; Granting Privilege: Grant connect, resource to csulb; Csulb cannot log into her/his account creating tables until she/he first has the CREATE SESSION system privilege: grant CREATE SESSION to csulb; Grant manager to csulb with admin option; Grant dba to csulb; DBA privilege gives the user full control on the server Granting Limited Resources: When granting resource quotas in an Oracle database, you use the quota parameter of the create user or alter user command, as shown in the following listing. In this example, CSULB is granted a quota of 100MB in the USERS tablespace: alter user CSULB quota 100M on USERS; Revoking Privilege: Revoke dba from csulb; Spool Off; Thank you and good luck to you all. (This is not a syntax ) 13 Prepared by M. Sahebjame 14