CHAPTER 6 HANDS ON
To perform the following assignments, refer to the tables in the JustLee Books database.
1. Create a sequence for populating the Customer# column of the CUSTOMERS table. When setting the
start and increment values, keep in mind that data already exists in this table. The options should be set
to not cycle the values and not cache any values, and no minimum or maximum values should be
declared.
ALTER SEQUENCE customers_seq
START WITH (SELECT MAX(customer#) + 1 FROM customers)
INCREMENT BY 1
NO CYCLE
NO CACHE;s
2. Add a new customer row by using the sequence created in Question 1. The only data currently
available for the customer is as follows: last name = Shoulders, first name = Frank, and zip = 23567
INSERT INTO customers (customer_id, last_name, first_name, zip)
VALUES (customer_seq.NEXTVAL, 'Shoulders', 'Frank', '23567');
3. Create a sequence that generates integers starting with the value 5. Each value should be three less
than the previous value generated. The lowest possible value should be 0, and the sequence shouldn’t
be allowed to cycle. Name the sequence MY_FIRST_SEQ.
CREATE SEQUENCE MY_FIRST_SEQ
START WITH 5
INCREMENT BY -3
MINVALUE 0
NO CYCLE;s
4. Issue a SELECT statement that displays NEXTVAL for MY_FIRST_SEQ three times. Because the value
isn’t being placed in a table, use the DUAL table in the FROM clause of the SELECT statement. What
causes the error on the third SELECT?
SELECT NEXTVAL FOR MY_FIRST_SEQ FROM DUAL;
SELECT NEXTVAL FOR MY_FIRST_SEQ FROM DUAL;
SELECT NEXTVAL FOR MY_FIRST_SEQ FROM DUAL;
The third SELECT statement will cause an error because the sequence has already been incremented
twice and there is no more value to return.
5. Change the setting of MY_FIRST_SEQ so that the minimum value that can be generated is -1000.
MY_FIRST_SEQ = CREATE SEQUENCE MY_FIRST_SEQ MINVALUE -1000;
6. Create a private synonym that enables you to reference the MY_FIRST_SEQ object as NUMGEN.
CREATE SYNONYM NUMGEN FOR MY_FIRST_SEQ;
7. Use a SELECT statement to view the CURRVAL of NUMGEN. Delete the NUMGEN synonym and
MY_FIRST_SEQ.
SELECT CURRVAL('NUMGEN') FROM DUAL;
DROP SYNONYM NUMGEN;
DROP SEQUENCE MY_FIRST_SEQ;
8. Create a bitmap index on the CUSTOMERS table to speed up queries that search for customers based
on their state of residence. Verify that the index exists, and then delete the index.
CREATE BITMAP INDEX customers_state_idx ON customers (state);
SELECT * FROM USER_INDEXES WHERE INDEX_NAME = 'CUSTOMERS_STATE_IDX';
DROP INDEX customers_state_idx;
9. Create a B-tree index on the customer’s Lastname column. Verify that the index exists by querying
the data dictionary. Remove the index from the database.
CREATE INDEX lastname_index ON customers (lastname);
SELECT * FROM user_indexes WHERE index_name = 'LASTNAME_INDEX';
DROP INDEX lastname_index;
10. Many queries search by the number of days to ship (number of days between the order and shipping
dates). Create an index that might improve the performance of these queries.
CREATE INDEX shipping_days_index ON orders (shipping_date - order_date);
CHAPTER 7
Create and execute SQL statements to perform the following actions, using the JustLee Books database:
1. Create a new user account. The account name should be a combination of your first initial and your
last name.
Username: jpadin
2. Attempt to log in to Oracle 11g with the newly created account.
SQLPLUS jpadin/system@database_padin
3. Assign privileges to the new account that allow connecting to the database, creating new tables, and
altering an existing table.
GRANT CONNECT ON DATABASE TO new_account;
GRANT CREATE TABLE TO new_account;
GRANT ALTER ANY TABLE TO new_account;
4. Using an account with the required privileges, create a role named CUSTOMERREP that allows
inserting new rows in the ORDERS and ORDERITEMS tables and deleting rows from these tables.
CREATE ROLE CUSTOMERREP;
GRANT INSERT ON ORDERS, ORDERITEMS TO CUSTOMERREP;
GRANT DELETE ON ORDERS, ORDERITEMS TO CUSTOMERREP;
5. Assign the account created in Assignment 1 the CUSTOMERREP role.
`ALTER USER <jpadin> GRANT ROLE CUSTOMERREP;`
6. Log in to Oracle 11g with the new account created in Assignment 1. Determine the privileges currently
available to the account.
-Create session
-Create table
-Create view
-Create sequence
-Create procedure
-Create trigger
-Create type
-Create synonym
-Create public database link
-Create public synonym
-Create role
-Create user
-Alter session
-Alter table
-Alter
7. Revoke the privilege to delete rows in the ORDERS and ORDERITEMS tables from the CUSTOMERREP
role.
REVOKE DELETE ON ORDERS, ORDERITEMS FROM CUSTOMERREP;
8. Remove the CUSTOMERREP role from the account created in Assignment 1.
`REVOKE CUSTOMERREP FROM <jpadin>;`
9. Delete the CUSTOMERREP role from the Oracle 11g database.
DROP ROLE CUSTOMERREP;
10. Delete the user account created in Assignment 1.