You are on page 1of 10

Lab Chapter # 3

Total Marks: 10

Student Name : KOVID BEHL______________________ Student Id #: N01579154______

Lab exercises You need to be present in the class for your lab to be graded and are to be
submitted within 3 Hours after the class, failing to do the will result in ZERO grade.

Hands-On Assignments

1. Create a new table containing the category code and description for the categories of books sold by
JustLee Books. The table should be called CATEGORY, and the columns should be CatCode and
CatDesc. The CatCode column should store a maximum of 2 characters, and the CatDesc column
should store a maximum of 10 characters.
CREATE TABLE CATEGORY
(CatCode VARCHAR2(2),
CatDesc VARCHAR2(10));

2. Create a new table containing these four columns: Emp#, Lastname, Firstname, and Job_class.
The table name should be EMPLOYEES. The Job_class column should be able to store character
strings up to a maximum length of four, but the column values shouldn’t be padded if the value
has less than four characters. The Emp# column contains a numeric ID and should allow a five-
digit number. Use column sizes you consider suitable for the Firstname and Lastname columns.
CREATE TABLE EMPLOYEES
(Emp# NUMBER(5),
Lastname VARCHAR2(25),
Firstname VARCHAR2(25),
Job_class VARCHAR2(4)
3. Add two columns to the EMPLOYEES table. One column, named EmpDate, contains the date of
employment for each employee, and its default value should be the system date. The second
column, named EndDate, contains employees’ date of termination.
ALTER TABLE EMPLOYEES
ADD (EmpDate DATE DAFULT SYSDATE,
EndDate DATE);
DESC EMPLOYEES;

4. Modify the Job_class column of the EMPLOYEES table so that it allows storing a maximum
width of two characters.
ALTER TABLE EMPLOYEES
MODIFY(Job_class VARCHAR2(2));
DESC EMPLOYEES;
5. Delete the EndDate column from the EMPLOYEES table.
ALTER TABLE EMPLOYEES
DROP(EndDate);
DESC EMPLOYEES;

6. Rename the EMPLOYEES table as JL_EMPS.


RENAME EMPLOYEES
TO JL_EMPS;
DESC JL_EMPS;
7. Create a new table containing these four columns from the existing BOOKS table: ISBN, Cost,
Retail, and Category. The name of the ISBN column should be ID, and the other columns should
keep their original names. Name the new table BOOK_PRICING.
CREATE TABLE BOOK_PRICING(ID, cost, retail, category)
AS (SELECT ISBN, cost, retail, category
From Books);
DESC BOOK_PRICING;
8. Mark the Category column of the BOOK_PRICING table as unused. Verify that the column is no
longer available.
ALTER TABLE BOOK_PRICING
SET UNUSED(CATEGORY);
DESC BOOK_PRICING;

9. Truncate the BOOK_PRICING table, and then verify that the table still exists but no longer
contains any data.
TRUNCATE TABLE BOOK_PRICING;
SELECT * FROM BOOK_PRICING;

10. Delete the BOOK_PRICING table permanently so that it isn’t moved to the recycle bin. Delete the
JL_EMPS table so that it can be restored. Restore the JL_EMPS table and verify that it’s
available again.
DROP TABLE BOOK_PRICING PURGE;
DROP TABLE JL_EMPS;
SELECT original_name
FROM recyclebin;
FLASHBACK TABLE JL_EMPS
TO BEFORE DROP;
SELECT original_name
FROM recyclebin;

Table Column Data Description Length Scale Default Value


11. Create the following Crimes table using the information outlined below:
Crime_ID Numeric 9
Criminal_ID Numeric 6
Classification Fixed character 1
Data_charged Date
Status Fixed character 2
Hearing_date Date
CREAT TABLE Crimes
(Crime_ID NUMBER(9),
Criminal_ID NUMBER(6),
Classification CHAR(1),
Date_charged DATE,
Status CHAR(2),
Hearing_date DATE);
12. Modify the Crimes table to add a default value of U for the Classification column of the Crimes
table.
ALTER TABLE Crimes
MODIFY Classification DEFAULT ‘U’;

13. Modify the Crimes table to add a column named Date_Recorded to the Crimes table. This column
needs to hold date values and should be set to the current date by default.

ALTER TABLE crimes


ADD(date_recorded DATE DEFAULT SYSDATE);
Advanced Challenge
The management of JustLee Books has approved implementing a new commission policy and benefits plan
for the account managers. The following changes need to be made to the existing database:

• Two new columns must be added to the ACCTMANAGER table: one to indicate the commission
classification assigned to each employee and another to contain each employee’s benefits code. The
commission classification column should be able to store integers up to a maximum value of 99 and be
named Comm_id. The value of the Comm_id column should be set to a value of 10 automatically if no
value is provided when a row is added. The benefits code column should also accommodate integer values
up to a maximum of 99 and be named Ben_id.
ALTER TABLE AccManager
ADD(Comm_id NUMBER(2) DEFAULT 10,
Ben_id NUMBER(2));

• A new table, COMMRATE, must be created to store the commission rate schedule and must contain the
following columns:
1. Comm_id: A numeric column similar to the one added to the ACCTMANAGER table
2. Comm_rank: A character field that can store a rank name allowing up to 15 characters
3. Rate: A numeric field that can store two decimal digits (such as .01 or .03)
CREATE TABLE COMMRATE(Comm_id NUMBER(2),
Comm_rank VARCHAR2(15),
Rate NUMBER(2,2));

• A new table, BENEFITS, must be created to store the available benefit plan options and must contain the
following columns:
1. Ben_id: A numeric column similar to the one added to the ACCTMANAGER table
2. Ben_plan: A character field that can store a single character value
3. Ben_provider: A numeric field that can store a three-digit integer
4. Active: A character field that can hold a value of Y or N

CREATE TABLE BENEFITS


(Ben_id NUMBER(2),
Ben_plan CHAR(1),
Ben_provider NUMBER(3),
Active CHAR(1),
CONSTRAINT Active_Check CHECK (Active IN ('Y','N')));
Required: Create the SQL statements to address the changes needed to support the new commission and
benefits data.

You might also like