Advertisement
vvccs

6

Apr 25th, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. CREATE TABLE Book_info (
  2. bookid INT,
  3. bname VARCHAR(100),
  4. bauthor VARCHAR(100),
  5. price DECIMAL(10, 2),
  6. edition VARCHAR(50),
  7. publication VARCHAR(100),
  8. pur_date DATE
  9. );
  10. DESCRIBE Book_info;
  11.  
  12. CREATE TABLE Student (
  13. lib_car_num INT,
  14. stud_name VARCHAR(100),
  15. class VARCHAR(50),
  16. branch VARCHAR(50),
  17. roll_no INT
  18. );
  19. DESCRIBE Student;
  20.  
  21. CREATE TABLE Issue_table (
  22. issue_date DATE,
  23. sub_date DATE,
  24. bookid INT,
  25. lib_car_num INT,
  26. due DATE
  27. );
  28. DESCRIBE Issue_table;
  29.  
  30.  
  31. INSERT INTO Book_info VALUES(1, 'Book1', 'Author1', 20, '1st Edition', 'Publication1', DATE '2024-01-01');
  32. INSERT INTO Book_info VALUES(2, 'Book2', 'Author2', 25, '2nd Edition', 'Publication2', DATE '2024-01-02');
  33. INSERT INTO Book_info VALUES(3, 'Book3', 'Author3', 30, '3rd Edition', 'Publication3', DATE '2024-01-03');
  34. INSERT INTO Book_info VALUES(4, 'Book4', 'Author4', 35, '4th Edition', 'Publication4', DATE '2024-01-04');
  35. INSERT INTO Book_info VALUES(5, 'Book5', 'Author5', 40, '5th Edition', 'Publication5', DATE '2024-01-05');
  36. INSERT INTO Book_info VALUES(6, 'Book6', 'Author6', 45, '6th Edition', 'Publication6', DATE '2024-01-06');
  37. INSERT INTO Book_info VALUES(7, 'Book7', 'Author7', 50, '7th Edition', 'Publication7', DATE '2024-01-07');
  38. INSERT INTO Book_info VALUES(8, 'Book8', 'Author8', 55, '8th Edition', 'Publication8', DATE '2024-01-08');
  39. INSERT INTO Book_info VALUES(9, 'Book9', 'Author9', 60, '9th Edition', 'Publication9', DATE '2024-01-09');
  40. INSERT INTO Book_info VALUES(10, 'Book10', 'Author10', 65, '10th Edition', 'Publication10', DATE '2024-01-10');
  41. SELECT *FROM Book_info;
  42.  
  43. INSERT INTO Student VALUES(1, 'Student1', 'Class1', 'Branch1', 1);
  44. INSERT INTO Student VALUES(2, 'Student2', 'Class2', 'Branch2', 2);
  45. INSERT INTO Student VALUES(3, 'Student3', 'Class3', 'Branch3', 3);
  46. INSERT INTO Student VALUES(4, 'Student4', 'Class4', 'Branch4', 4);
  47. INSERT INTO Student VALUES(5, 'Student5', 'Class5', 'Branch5', 5);
  48. INSERT INTO Student VALUES(6, 'Student6', 'Class6', 'Branch6', 6);
  49. INSERT INTO Student VALUES(7, 'Student7', 'Class7', 'Branch7', 7);
  50. INSERT INTO Student VALUES(8, 'Student8', 'Class8', 'Branch8', 8);
  51. INSERT INTO Student VALUES(9, 'Student9', 'Class9', 'Branch9', 9);
  52. INSERT INTO Student VALUES(10, 'Student10', 'Class10', 'Branch10', 10);
  53. SELECT *FROM Student;
  54.  
  55. INSERT INTO Issue_table VALUES('01-JAN-24','10-JAN-24', 2, 1,'20-JAN-24');
  56. INSERT INTO Issue_table VALUES('20-JAN-24','25-JAN-24', 4, 5,'31-JAN-24');
  57. SELECT *FROM Issue_table;
  58. -------------------------------------------------------------------------------------------------------------------------------------
  59. 5. Add primary key constraint and not null constraint
  60.  
  61. ALTER TABLE Book_info
  62. ADD CONSTRAINT pk_book_id PRIMARY KEY (bookid);
  63. ALTER TABLE Student
  64. ADD CONSTRAINT pk_lib_car_num PRIMARY KEY (lib_car_num);
  65. ALTER TABLE Issue_table
  66. ADD CONSTRAINT pk_issue PRIMARY KEY (issue_date, bookid, lib_car_num);
  67.  
  68. ALTER TABLE Book_info
  69. MODIFY bname VARCHAR(100) NOT NULL;
  70. ALTER TABLE Student
  71. MODIFY stud_name VARCHAR(100) NOT NULL;
  72. ALTER TABLE Student
  73. MODIFY roll_no INT NOT NULL;
  74.  
  75. ALTER TABLE Issue_table
  76. MODIFY issue_date DATE NOT NULL;
  77. ALTER TABLE Issue_table
  78. MODIFY bookid INT NOT NULL;
  79. ALTER TABLE Issue_table
  80. MODIFY lib_car_num INT NOT NULL;
  81.  
  82. 6. Add primary key Foreign key constraint
  83. -- Add foreign key constraint for bookid column in Issue_table referencing bookid column in Book_info table
  84. ALTER TABLE Issue_table
  85. ADD CONSTRAINT fk_bookid FOREIGN KEY (bookid) REFERENCES Book_info(bookid);
  86.  
  87. -- Add foreign key constraint for lib_car_num column in Issue_table referencing lib_car_num column in Student table
  88. ALTER TABLE Issue_table
  89. ADD CONSTRAINT fk_lib_car_num FOREIGN KEY (lib_car_num) REFERENCES Student(lib_car_num) ON UPDATE CASCADE ON DELETE CASCADE;;
  90.  
  91. 1. Find the details of the books issued to the library card number 1.
  92.  
  93. SELECT i.*, b.bname, b.bauthor
  94. FROM Issue_table i
  95. JOIN Book_info b ON i.bookid = b.bookid
  96. WHERE i.lib_car_num = 1;
  97.  
  98. 2. Give all the information about student and the book issued with ascending order of library card number
  99.  
  100. SELECT s.*, i.*, b.*
  101. FROM Student s
  102. inner JOIN Issue_table i ON s.lib_car_num = i.lib_car_num
  103. inner JOIN Book_info b ON i.bookid = b.bookid
  104. ORDER BY s.lib_car_num ASC;
  105.  
  106. 3. Find the author, edition, price of book.
  107.  
  108. SELECT bauthor, edition, price
  109. FROM Book_info;
  110.  
  111. 4. Find the names of the students with dues on the book issue.
  112.  
  113. SELECT DISTINCT s.stud_name
  114. FROM Student s
  115. INNER JOIN Issue_table i ON s.lib_car_num = i.lib_car_num
  116. WHERE SYSDATE > i.due;
  117.  
  118.  
  119.  
  120. 7. Show issue_date of Issue_table in Ascending Order.(order by clause)
  121.  
  122. SELECT issue_date
  123. FROM Issue_table
  124. ORDER BY issue_date ASC;
  125.  
  126. 8. Show roll_no of Student in descending Order.(order by clause)
  127.  
  128. SELECT roll_no
  129. FROM Student
  130. ORDER BY roll_no DESC;
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement