100% found this document useful (1 vote)
34 views3 pages

SQL 4

The document describes creating several tables (Sailors, Boats, Reserves) with attributes like primary keys (sid, bid), foreign keys, and sample data. It then lists 15 SQL commands to query the tables for information like sailor names and ages, boat colors, oldest/youngest sailors, sailor ratings, boat reservations by sailor name/color, and more.

Uploaded by

Kartik Chopra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
34 views3 pages

SQL 4

The document describes creating several tables (Sailors, Boats, Reserves) with attributes like primary keys (sid, bid), foreign keys, and sample data. It then lists 15 SQL commands to query the tables for information like sailor names and ages, boat colors, oldest/youngest sailors, sailor ratings, boat reservations by sailor name/color, and more.

Uploaded by

Kartik Chopra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab Assignment–5 4

1. Create table emp which has the following attributes (employee


table)
(@empno, ename, job, sal, deptno)
Where empno is primary key, ename is unique, job in (Prof, AP,
and Lect), sal is not NULL, and deptno default is 10.
Insert appropriate records, check error messages in case of
violation and list all the constraint names for given table.

CREATE TABLE book (


Rno NUMBER PRIMARY KEY,
DOI DATE,
DOR DATE,
CONSTRAINT check_dor_gt_doi CHECK (DOR > DOI)
);
2. Create table book:
Rno number—PK
DOI-date
DOR-date
DOR>DOI
Insert appropriate records, check error messages in case of
violation and list all the constraint names for given table.
CREATE TABLE book (
Rno NUMBER PRIMARY KEY,
DOI DATE,
DOR DATE,
CONSTRAINT check_dor_gt_doi CHECK (DOR > DOI)
);

3. Create table st
Rno-Number
Class-Char
Marks-Number
Primary key(rno,class)
Marks>0
Insert appropriate records, check error messages in case of
violation and list all the constraint names for given table.
CREATE TABLE st (
Rno NUMBER,
Class CHAR,
Marks NUMBER CHECK (Marks > 0),
PRIMARY KEY (Rno, Class)
);

4. Create table S which has the following attributes (Salesperson


table)
(sno, sname, city)
Where sno is primary key
CREATE TABLE S (
sno NUMBER PRIMARY KEY,
sname VARCHAR2(50),
city VARCHAR2(50)
);

5. Create table P which has the following attributes (Part table)


(pno, pname, color)
Where pno is primary key
CREATE TABLE P (
pno NUMBER PRIMARY KEY,
pname VARCHAR2(50),
color VARCHAR2(20)
);

6. Create table SP which has the following attributes


(sno, pno qty)
Where combination of (sno, pno) is primary key, also sno and
pno are foreign keys

CREATE TABLE SP (
sno NUMBER,
pno NUMBER,
qty NUMBER,
PRIMARY KEY (sno, pno),
FOREIGN KEY (sno) REFERENCES S(sno),
FOREIGN KEY (pno) REFERENCES P(pno)
);

7. Create table dept which has the following attributes


(department table)
(deptno, dname)
Where deptno is primary key, dname in (Acc, comp, elect)

CREATE TABLE dept (


deptno NUMBER PRIMARY KEY,
dname VARCHAR2(50) CHECK (dname IN ('Acc', 'comp', 'elect'))
);
8. Create table emp which has the following attributes (employee
table)
(@empno, ename, job, sal, deptno)
Where empno is primary key, ename is unique, job in (Prof, AP,
and Lect), sal is not NULL, and deptno is foreign key

CREATE TABLE emp (


empno NUMBER PRIMARY KEY,
ename VARCHAR2(50) UNIQUE,
job VARCHAR2(10) CHECK (job IN ('Prof', 'AP', 'Lect')),
sal NUMBER NOT NULL,
deptno NUMBER,
FOREIGN KEY (deptno) REFERENCES dept(deptno)
);
Lab Assignment 5
1. Create the following tables and insert some tuples in these tables shown below. Where sid is
the primary key for the Sailors table, bid is the primary key for the Boats table and sid and
bid are the foreign keys for the Reserves table referencing to the Sailors and Boats table,
respectively.
Sailors(sid: integer, sname: string, rating: integer, age: real)
Boats(bid: integer, bname: string, color: string)
Reserves(sid: integer, bid: integer, day: date)
After inserting the records in these tables, the instances should look like as follows:
Sailors Reserves Boats

2. Write SQL command for the following:


i) Show the names and ages of all sailors.
ii) Show the details of the boats which are red and blue in color.
iii) Find the oldest and youngest sailors’ age.
iv) Find the ages of sailors whose name begins and ends with B and has at leastthree
characters.
v) Show the average rating of the sailors.
vi) Find all sailors with a rating above 7.
vii) Find the number of boats reserved by the sailor namedHoratio.
viii) Find the colors of boats reserved by Lubber.
ix) Show the details of the sailors who have reserved the boat with bid 102.
x) Find the sid of sailors who have reserved green boats.
xi) Find the names of sailors who have reserved boat number 103.
xii) Find the sids and names of sailors who have reserved a red boat.
xiii) Find the names of the sailors who have reserved a green or a blue boat.
xiv) Find the names of sailors who have reserved both a red and a green boat.
xv) Find the names of sailors who have reserved at least one boat.

You might also like