Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Consider an application which is being developed for a ‘Used Car seller’.

For storing various


details related to the cars and owners, create the tables according to the schemas given
below;
CAR(Car_Reg_No, Brand, Model, Variant, Model_Year, Color, Year_Of_Purchase,
Kilometers)
OWNER(Owner_ID, Owner_Name, Owner_Phone, License_Number)
OWNER_CAR(Owner_ID, Car_Reg_No, Price)

I - Consider the following as important components while creating the tables;

a. The columns that are underlined are Primary keys


b. All columns must contain some values.
c. Choose the appropriate data types which would match the most for all the attributes.
d. Use constraint names for all the constraints you create.
e. For table CAR;
i. Brand should be one of { ‘Maruti’, ‘Ford’, ‘Hyundai’ }
ii. Color should be one of { ‘Black’, ‘White’, ‘Red’, ‘Blue’ }
iii. Kilometers should be less than 50000.
f. For table OWNER;
i. Owner_ID should start with ‘OID’
ii. License_Number should be unique value.
g. For table OWNER_CAR;
i. Price should be greater than Rs.150000.

II - After table creation, write the queries to alter the tables according to the
requirements given below;

a. Add the Foreign Key in OWNER_CAR table to refer Owner_ID from OWNER table.
b. Add the Foreign Key in OWNER_CAR table to refer Car_Reg_No from CAR table.
c. Add an attribute Owner_Address in OWNER table.
d. Add an attribute Test_Status of the car which accepts character based values.
e. Remove the column Year_Of_Purchase from CAR.
f. Remove the column Owner_Address from OWNER table.
g. Remove the Unique constraint of License_Number attribute.
h. Remove the color constraint so that any colored cars can be inserted.
i. Add a column Years_Used to OWNER_CAR table which stores the number of years the car
used by the owner.
j. Remove the table OWNER_CAR from the database completely.
k. Change the type of License_Number attribute from character based type to number type.
Answer -

CREATE TABLE CAR (


Car_Reg_No VARCHAR2(20) PRIMARY KEY,
Brand VARCHAR2(20) CHECK (Brand IN ('Maruti', 'Ford', 'Hyundai')),
Model VARCHAR2(20),
Variant VARCHAR2(20),
Model_Year NUMBER(4),
Color VARCHAR2(10) CHECK (Color IN ('Black', 'White', 'Red', 'Blue')),
Year_Of_Purchase NUMBER(4),
Kilometers NUMBER(6) CHECK (Kilometers < 50000)
);

CREATE TABLE OWNER (


Owner_ID VARCHAR2(10) PRIMARY KEY,
Owner_Name VARCHAR2(50),
Owner_Phone VARCHAR2(15),
License_Number VARCHAR2(20) UNIQUE
);

CREATE TABLE OWNER_CAR (


Owner_ID VARCHAR2(10) REFERENCES OWNER(Owner_ID),
Car_Reg_No VARCHAR2(20) REFERENCES CAR(Car_Reg_No),
Price NUMBER(10, 2) CHECK (Price > 150000)
);

II)

a) ALTER TABLE OWNER_CAR


ADD CONSTRAINT FK_OWNER_ID
FOREIGN KEY (Owner_ID)
REFERENCES OWNER(Owner_ID);

b) ALTER TABLE OWNER_CAR


ADD CONSTRAINT FK_CAR_REG_NO
FOREIGN KEY (Car_Reg_No)
REFERENCES CAR(Car_Reg_No);

c) ALTER TABLE OWNER


ADD Owner_Address VARCHAR2(100);

d)ALTER TABLE CAR


ADD Test_Status VARCHAR2(20);

e) ALTER TABLE CAR


DROP COLUMN Year_Of_Purchase;

f) ALTER TABLE OWNER


DROP COLUMN Owner_Address;

g) ALTER TABLE OWNER


DROP CONSTRAINT SYS_C008843;

h) ALTER TABLE CAR


DROP CONSTRAINT SYS_C008831;
i)ALTER TABLE OWNER_CAR
ADD Years_Used NUMBER(2);

j) DROP TABLE OWNER_CAR;

k) ALTER TABLE OWNER


MODIFY (License_Number NUMBER);

You might also like