0% found this document useful (0 votes)
22 views3 pages

DBMS Table Creation and Queries Guide

Uploaded by

gtoworld00
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
0% found this document useful (0 votes)
22 views3 pages

DBMS Table Creation and Queries Guide

Uploaded by

gtoworld00
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

DBMS Theory Assignment

Name: Dipanjan Sahoo


Roll No: 2370155
================================================

Creating Tables and Populating Data:

Creating SUPPLIER Table


CREATE TABLE SUPPLIER (
s_id INT PRIMARY KEY,
s_name VARCHAR(50),
s_address VARCHAR(100)
);

Creating BOOK Table


CREATE TABLE BOOK (
acc_no INT PRIMARY KEY,
year_of_pub INT,
title VARCHAR(100)
);

Creating USER Table


CREATE TABLE USER (
card_no VARCHAR(10) PRIMARY KEY,
u_name VARCHAR(50),
u_address VARCHAR(100)
);

Creating SUPPLY Table


CREATE TABLE SUPPLY (
acc_no INT,
s_id INT,
price DECIMAL(10, 2),
date_of_supply DATE,
FOREIGN KEY (acc_no) REFERENCES BOOK(acc_no),
FOREIGN KEY (s_id) REFERENCES SUPPLIER(s_id)
);

Creating BORROW Table


CREATE TABLE BORROW (
acc_no INT,
card_no VARCHAR(10),
date_of_issue DATE,
FOREIGN KEY (acc_no) REFERENCES BOOK(acc_no),
FOREIGN KEY (card_no) REFERENCES USER(card_no)
);

Inserting data into SUPPLIER Table


INSERT INTO SUPPLIER VALUES
(1, 'S1', 'Address1'),
(2, 'S2', 'Address2'),
Add more entries as needed;
Inserting data into BOOK Table
INSERT INTO BOOK VALUES
(12001, 1994, 'Book1'),
(12002, 1994, 'Book2'),
Add more entries as needed;

Inserting data into USER Table


INSERT INTO USER VALUES
('F53', 'User1', 'UserAddress1'),
('F54', 'User2', 'UserAddress2'),
Add more entries as needed;

Inserting data into SUPPLY Table


INSERT INTO SUPPLY VALUES
(12001, 1, 50.00, TO_DATE('2023-01-01', 'YYYY-MM-DD')),
(12002, 2, 60.00, TO_DATE('2023-02-01', 'YYYY-MM-DD')),
Add more entries as needed;

Inserting data into BORROW Table


INSERT INTO BORROW VALUES
(12001, 'F53', TO_DATE('2023-01-10', 'YYYY-MM-DD')),
(12002, 'F54', TO_DATE('2023-02-15', 'YYYY-MM-DD')),
Add more entries as needed;

Queries:

a) Find all books where accession number lies between 12000 and 14000 and the year of
publication is 1994.

SELECT * FROM BOOK


WHERE acc_no BETWEEN 12000 AND 14000 AND year_of_pub = 1994;

b) Find all tuples from the BORROW relation where the card number is ‘F53’ and the
date of issue is before December 28, 2004.

SELECT * FROM BORROW


WHERE card_no = 'F53' AND date_of_issue < TO_DATE('2004-12-28', 'YYYY-MM-
DD');

c) Find the accession numbers of all books issued to persons whose first name is
‘Rakesh’.

SELECT B.acc_no
FROM BOOK B
JOIN BORROW O ON B.acc_no = O.acc_no
JOIN USER U ON O.card_no = U.card_no
WHERE UPPER(SUBSTR(U.u_name, 1, INSTR(U.u_name, ' ') - 1)) = 'RAKESH';

d) Find the accession numbers of all books that are available in the library (that is, not
issued).

SELECT B.acc_no
FROM BOOK B
LEFT JOIN BORROW O ON B.acc_no = O.acc_no
WHERE O.acc_no IS NULL;

e) Find the accession numbers of all books that are either issued or have been supplied
by the supplier whose id is ‘S12’.

SELECT B.acc_no
FROM BOOK B
LEFT JOIN BORROW O ON B.acc_no = O.acc_no
WHERE O.acc_no IS NOT NULL
UNION
SELECT S.acc_no
FROM SUPPLY S
WHERE S.s_id = 12;

You might also like