You are on page 1of 11

Lab Chapter # 4

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

JustLee Books has become the exclusive distributor for a number of books. The company now
needs to assign sales representatives to retail bookstores to handle the new distribution duties.
For these assignments, create new tables to support the following:

Constraint naming: All constraints except NOT NULL constraints should be assigned a name.

1. Modify the following SQL command so that the Rep_ID column is the PRIMARY
KEY for the table and the default value of Y is assigned to the Comm column. (The
Comm column indicates whether the sales representative earns commission.)
CREATE TABLE store_reps
(rep_ID NUMBER(5),
last VARCHAR2(15),
first VARCHAR2(10),
comm CHAR(1));
2. Change the STORE_REPS table so that NULL values can’t be entered in the name
columns (First and Last).

ALTER TABLE store_reps


MODIFY (First CONSTRAINT store_reps_first NOT NULL,
Last CONSTRAINT store_reps_last NOT NULL);
3. Change the STORE_REPS table so that only a Y or N can be entered in the Comm
column.

ALTER TABLE store_reps


ADD CONSTRAINT comm_check CHECK (comm IN ('Y','N'));

4. Add a column named Base_salary with a datatype of NUMBER(7,2) to the


STORE_REPS table. Ensure that the amount entered is above zero.
ALTER TABLE store_reps
ADD Base_Salary NUMBER(7,2)
ADD (CONSTRAINT base_salary_check CHECK (Base_Salary > 0));
5. Create a table named BOOK_STORES to include the columns listed in the
following chart.
CREATE TABLE BOOK_STORES
(Store_ID NUMBER(8),
CONSTRAINT Store_ID_pk PRIMARY KEY (Store_ID),
Name VARCHAR2(30) NOT NULL,
CONSTRAINT Name_unique UNIQUE (Name),
Contact VARCHAR2(30),
Rep_ID VARCHAR2(5));
6. Add a constraint to make sure the Rep_ID value entered in the BOOK_STORES
table is a valid value contained in the STORE_REPS table. The Rep_ID columns of
both tables were initially created as different datatypes. Does this cause an error
when adding the constraint? Make table modifications as needed so that you can
add the required constraint. Datatype Constraint Comments

ALTER TABLE BOOK_STORES


MODIFY (Rep_ID NUMBER(5))
ADD CONSTRAINT rep_id_fk FOREIGN KEY (Rep_ID)
REFERENCES Store_Reps (Rep_ID);
7. Change the constraint created in Assignment #6 so that associated rows of the
BOOK_STORES table are deleted automatically if a row in the STORE_REPS
table is deleted.
ALTER TABLE BOOK_STORES
DROP CONSTRAINT rep_id_fk;

ALTER TABLE BOOK_STORES


ADD CONSTRAINT rep_id_fk FOREIGN KEY (Rep_ID)
REFERENCES Store_Reps (Rep_ID) ON DELETE CASCADE;
8. Create a table named REP_CONTRACTS containing the columns listed in the
following chart. A composite PRIMARY KEY constraint including the Rep_ID,
Store_ID, and Quarter columns should be assigned. In addition, FOREIGN KEY
constraints should be assigned to both the Rep_ID and Store_ID columns.
CREATE TABLE REP_CONTRACTS
(Store_ID NUMBER(8),
Name NUMBER(5),
Quarter CHAR(3),
Rep_ID NUMBER(5),
CONSTRAINT rep_contracts_pk PRIMARY KEY (Store_ID, Rep_ID,
Quarter),
CONSTRAINT store_id_fk FOREIGN KEY (Store_ID)
REFERENCES BOOK_STORES (Store_ID),
CONSTRAINT rc_rep_id_fk FOREIGN KEY (Rep_ID)
REFERENCES Store_Reps (Rep_ID));
9. Produce a list of information about all existing constraints on the STORE_REPS
table.
SELECT constraint_name, constraint_type, search_condition,
r_constraint_name
FROM user_constraints
WHERE table_name = 'STORE_REPS';
10. Issue the commands to disable and then enable the CHECK constraint on the
Base_salary column.
ALTER TABLE Store_Reps

DISABLE CONSTRAINT base_salary_check ;

ALTER TABLE Store_Reps

ENABLE CONSTRAINT base_salary_check ;

11. Add the following Constraints to the Crimes Table created in Lab#3

Crime_ID PRIMARY KEY


Criminal_ID NOT NULL
Classification CHECK IN (‘F’,’M’,’O’,’U’)
Status CHECK IN (‘CL’,’CA’,’IA’)

ALTER TABLE crimes


ADD (CONSTRAINT crime_id_pk PRIMARY KEY (crime_id),
CONSTRAINT classification_ck CHECK (classification IN
('F','M','O','U')),
CONSTRAINT status_ck CHECK (status IN ('CL','CA','IA')))
MODIFY (criminal_id CONSTRAINT criminal_id_nn NOT NULL);
Advanced Challenge

Create two tables based on the E-R model shown in Figure and the business rules in the
following list for a work order tracking database. Include all the constraints in the
CREATE TABLE statements. You should have only two CREATE TABLE statements and
no ALTER TABLE statements. Name all constraints except NOT NULLs.

• Use your judgment for column datatypes and sizes.


• Proj# and Wo# are used to uniquely identify rows in these tables.
• Each project added must be assigned a name, and no duplicate project names are
allowed.
• Each work order must be assigned to a valid project when added and be assigned a
description and number of hours.
• Each work order added must have a different description.
• The number of hours assigned to a work order should be greater than zero.
• If data is provided for the Wo_complete column, only Y or N are acceptable values.
CREATE TABLE Project (
Proj# NUMBER(25) CONSTRAINT Proj_nu_pk PRIMARY KEY,
P_name VARCHAR2(50) NOT NULL CONSTRAINT p_name_unique UNIQUE,
P_desc VARCHAR2(50),
p_budget VARCHAR2(50)
);

CREATE TABLE Work_Order (


Wo# NUMBER(25) CONSTRAINT worko_nu_pk PRIMARY KEY,
Proj# NUMBER(25) NOT NULL,
Wo_desc VARCHAR2(50) NOT NULL CONSTRAINT wo_desc_unique
UNIQUE,
Wo_assigned VARCHAR2(25),
Wo_hours NUMBER(5) NOT NULL,
Wo_start DATE,
Wo_end DATE,
Wo_complete CHAR(1),
CONSTRAINT fk_proj FOREIGN KEY (Proj#) REFERENCES Project
(Proj#),
CONSTRAINT chk_wo_hours CHECK (Wo_hours > 0),
CONSTRAINT chk_wo_complete CHECK (Wo_complete IN ('Y', 'N',
NULL))
);
Create and execute the SQL statements needed to enforce the data relationships among
these tables.

You might also like