401 lines
11 KiB
Markdown
401 lines
11 KiB
Markdown
## Writeup
|
|
|
|
### Table Creation
|
|
|
|
**Student Table Creation** - RegNo is the primary key as defined by the question.
|
|
```sql
|
|
SQL> create table STUDENT(regno varchar(20),name varchar(50),major varchar(20),bdate date,constraint pkeySTUDENT primary key(regno));
|
|
|
|
Table created.
|
|
```
|
|
**Course Table Creation**
|
|
```sql
|
|
SQL> create table COURSE(course# int,cname varchar(30),dept varchar(30));
|
|
|
|
Table created.
|
|
```
|
|
Verifying, we get
|
|
```sql
|
|
SQL> desc STUDENT;
|
|
Name Null? Type
|
|
----------------------------------------- -------- ----------------------------
|
|
REGNO NOT NULL VARCHAR2(20)
|
|
NAME VARCHAR2(50)
|
|
MAJOR VARCHAR2(20)
|
|
BDATE DATE
|
|
|
|
SQL> desc COURSE;
|
|
Name Null? Type
|
|
----------------------------------------- -------- ----------------------------
|
|
COURSE# NUMBER(38)
|
|
CNAME VARCHAR2(30)
|
|
DEPT VARCHAR2(30)
|
|
|
|
```
|
|
**Adding the textbook table:**
|
|
```SQL
|
|
SQL> create table text(book_isbn int primary key,booktitle varchar(20),publisher varchar(50),author varchar(50));
|
|
|
|
Table created.
|
|
|
|
SQL> desc TEXT;
|
|
Name Null? Type
|
|
----------------------------------------- -------- ----------------------------
|
|
BOOK_ISBN NOT NULL NUMBER(38)
|
|
BOOKTITLE VARCHAR2(20)
|
|
PUBLISHER VARCHAR2(50)
|
|
AUTHOR VARCHAR2(50)
|
|
|
|
```
|
|
|
|
For the ENROLL table, we'll have
|
|
|
|
```sql
|
|
SQL> ALTER TABLE COURSE
|
|
2 ADD CONSTRAINT pk_course PRIMARY KEY (course#);
|
|
|
|
Table altered.
|
|
|
|
SQL> CREATE TABLE ENROLL (
|
|
2 regno VARCHAR2(20),
|
|
3 course# NUMBER(38),
|
|
4 sem INT,
|
|
5 book_isbn NUMBER(38),
|
|
6 PRIMARY KEY (regno, course#),
|
|
7 CONSTRAINT fkey1course FOREIGN KEY (regno) REFERENCES STUDENT (regno),
|
|
8 CONSTRAINT fkey2course FOREIGN KEY (course#) REFERENCES COURSE (course#),
|
|
9 CONSTRAINT fkey3book FOREIGN KEY (book_isbn) REFERENCES TEXT (book_isbn)
|
|
10 );
|
|
|
|
Table created.
|
|
|
|
SQL>
|
|
SQL> create table BOOK_ADOPTION(course# int,sem int,book_isbn int,primary key(course#,book_isbn),constraint fkey1book_adoption foreign key(course#)references course(course#));
|
|
|
|
Table created.
|
|
|
|
SQL> desc ENROLL;
|
|
Name Null? Type
|
|
----------------------------------------- -------- ----------------------------
|
|
REGNO NOT NULL VARCHAR2(20)
|
|
COURSE# NOT NULL NUMBER(38)
|
|
SEM NUMBER(38)
|
|
BOOK_ISBN NUMBER(38)
|
|
|
|
SQL> desc BOOK_ADOPTION;
|
|
Name Null? Type
|
|
----------------------------------------- -------- ----------------------------
|
|
COURSE# NOT NULL NUMBER(38)
|
|
SEM NUMBER(38)
|
|
BOOK_ISBN NOT NULL NUMBER(38)
|
|
```
|
|
Now, defining the foreign key nature of BOOK_ISBN, we have:
|
|
|
|
```sql
|
|
SQL> alter table BOOK_ADOPTION add constraint fkey2book_adoption foreign key(book_isbn) references text(book_isbn);
|
|
|
|
Table altered.
|
|
|
|
SQL> alter table enroll add constraint fkey3enroll foreign key(book_isbn) references text(book_isbn);
|
|
|
|
Table altered.
|
|
```
|
|
|
|
Verifying status so far, we have
|
|
|
|
```sql
|
|
SQL> desc ENROLL;
|
|
Name Null? Type
|
|
----------------------------------------- -------- ----------------------------
|
|
REGNO NOT NULL VARCHAR2(20)
|
|
COURSE# NOT NULL NUMBER(38)
|
|
SEM NUMBER(38)
|
|
BOOK_ISBN NUMBER(38)
|
|
|
|
SQL> desc BOOK_ADOPTION;
|
|
Name Null? Type
|
|
----------------------------------------- -------- ----------------------------
|
|
COURSE# NOT NULL NUMBER(38)
|
|
SEM NUMBER(38)
|
|
BOOK_ISBN NOT NULL NUMBER(38)
|
|
|
|
SQL> descc TEXT;
|
|
SP2-0042: unknown command "descc TEXT" - rest of line ignored.
|
|
SQL> desc TEXT;
|
|
Name Null? Type
|
|
----------------------------------------- -------- ----------------------------
|
|
BOOK_ISBN NOT NULL NUMBER(38)
|
|
BOOKTITLE VARCHAR2(20)
|
|
PUBLISHER VARCHAR2(50)
|
|
AUTHOR VARCHAR2(50)
|
|
```
|
|
|
|
### POPULATING TUPPLES
|
|
|
|
**Insertion into student:**
|
|
|
|
```sql
|
|
SQL> insert into student values('123','Ashwin','CCE','17-oct-2003');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into student values('456','Aadit','CCE','03-feb-2005');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into student values('789','Shrikanth','Environment','01-jan-2023');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into student values('101','Aarav','Environment','23-sep-2004');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into student values('567','Amogh','CSE','03-mar-2004');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into student values('890','Saarthak','CSE','03-jul-2004');
|
|
|
|
1 row created.
|
|
```
|
|
|
|
Insertion into COURSE:
|
|
```sql
|
|
SQL> insert into COURSE values(1101,'DBMS','ICT');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into COURSE values(1102,'DAA','ICT');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into COURSE values(2201,'COA','CS');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into COURSE values(2202,'DSD','CS');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into COURSE values(3301,'MATH','EVS');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into COURSE values(3302,'POETRY','EVS');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into COURSE values(3303,'Random','EVS');
|
|
|
|
1 row created.
|
|
```
|
|
|
|
**Insertion in TEXT**:
|
|
```sql
|
|
SQL> insert into TEXT values(1234,'DBMS vol 1','Pearson','SS');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into TEXT values(4123,'DBMS vol 2','Pearson','SS');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into TEXT values(5678,'DAA vol 2','Pearson','Ramanujan');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into TEXT values(9012,'COA vol 666','Cengage','Satan');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into TEXT values(3456,'DSD vol 99','Penguin','Panda');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into TEXT values(7890,'Math vol 66','U-Like','R S Aggrwal');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into TEXT values(1123,'Poetry vol 0','OML','Yahya Bootwaala');
|
|
|
|
1 row created.
|
|
```
|
|
|
|
**Insertion into ENROLL**:
|
|
|
|
```sql
|
|
SQL> insert into enroll values('123',1101,1,1234);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into enroll values('123',1102,2,5678);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into enroll values('456',1101,3,4123);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into enroll values('456',2201,4,9012);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into enroll values('789',3301,5,7890);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into enroll values('789',3302,6,1123);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into enroll values('101',3302,7,7890);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into enroll values('567',2201,8,9012);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into enroll values('890',2202,1,3456);
|
|
|
|
1 row created.
|
|
```
|
|
|
|
**Insertion into BOOK_ADOPTION**:
|
|
```sql
|
|
SQL> insert into BOOK_ADOPTION values(1101,1,1234);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into BOOK_ADOPTION values(1102,2,4123);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into BOOK_ADOPTION values(1102,2,5678);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into BOOK_ADOPTION values(2201,3,9012);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into BOOK_ADOPTION values(2202,4,3456);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into BOOK_ADOPTION values(3301,5,7890);
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into BOOK_ADOPTION values(3302,6,1123);
|
|
|
|
1 row created.
|
|
```
|
|
|
|
We also create a book that has not been
|
|
```sql
|
|
SQL> insert into text values(8901,'Random vol 66','Classmate','R D Sharma');
|
|
|
|
1 row created.
|
|
|
|
SQL> insert into BOOK_ADOPTION values(3303,7,8901);
|
|
|
|
1 row created.
|
|
```
|
|
|
|
Resizing the tables to look good in preview...
|
|
|
|
```sql
|
|
SQL> alter table student modify name varchar(20);
|
|
|
|
Table altered.
|
|
|
|
SQL> alter table student modify regno varchar(10);
|
|
|
|
Table altered.
|
|
|
|
SQL> alter table text modify (booktitle varchar(20),publisher varchar(20),author varchar(20));
|
|
|
|
Table altered.
|
|
```
|
|
|
|
### ENTRY VALIDATION
|
|
|
|
```sql
|
|
|
|
SQL> select * from STUDENT;
|
|
|
|
REGNO NAME MAJOR BDATE
|
|
---------- -------------------- -------------------- ---------
|
|
123 Ashwin CCE 17-OCT-03
|
|
456 Aadit CCE 03-FEB-05
|
|
789 Shrikanth Environment 01-JAN-23
|
|
101 Aarav Environment 23-SEP-04
|
|
567 Amogh CSE 03-MAR-04
|
|
890 Saarthak CSE 03-JUL-04
|
|
|
|
6 rows selected.
|
|
|
|
SQL> select * from COURSE;
|
|
|
|
COURSE# CNAME DEPT
|
|
---------- ------------------------------ ------------------------------
|
|
1101 DBMS ICT
|
|
1102 DAA ICT
|
|
2201 COA CS
|
|
2202 DSD CS
|
|
3301 MATH EVS
|
|
3302 POETRY EVS
|
|
3303 Random EVS
|
|
|
|
7 rows selected.
|
|
|
|
SQL> select * from TEXT;
|
|
|
|
BOOK_ISBN BOOKTITLE PUBLISHER AUTHOR
|
|
---------- -------------------- -------------------- --------------------
|
|
1234 DBMS vol 1 Pearson SS
|
|
4123 DBMS vol 2 Pearson SS
|
|
5678 DAA vol 2 Pearson Ramanujan
|
|
9012 COA vol 666 Cengage Satan
|
|
3456 DSD vol 99 Penguin Panda
|
|
7890 Math vol 66 U-Like R S Aggrwal
|
|
1123 Poetry vol 0 OML Yahya Bootwaala
|
|
8901 Random vol 66 Classmate R D Sharma
|
|
|
|
8 rows selected.
|
|
|
|
SQL> select * from BOOK_ADOPTION;
|
|
|
|
COURSE# SEM BOOK_ISBN
|
|
---------- ---------- ----------
|
|
1101 1 1234
|
|
1102 2 4123
|
|
1102 2 5678
|
|
2201 3 9012
|
|
2202 4 3456
|
|
3301 5 7890
|
|
3302 6 1123
|
|
3303 7 8901
|
|
|
|
8 rows selected.
|
|
|
|
SQL> select * from ENROLL;
|
|
|
|
REGNO COURSE# SEM BOOK_ISBN
|
|
-------------------- ---------- ---------- ----------
|
|
123 1101 1 1234
|
|
123 1102 2 5678
|
|
456 1101 3 4123
|
|
456 2201 4 9012
|
|
789 3301 5 7890
|
|
789 3302 6 1123
|
|
101 3302 7 7890
|
|
567 2201 8 9012
|
|
890 2202 1 3456
|
|
|
|
9 rows selected.
|
|
```
|
|
|