Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. --CREATING SCHEMA
  2. CREATE scheme libro;
  3. SET search_path TO libro,public;
  4.  
  5. --CREATING TABLES
  6. CREATE TABLE book (
  7. bno INTEGER PRIMARY KEY,
  8. title VARCHAR(20) CHECK(title <> ' ') NOT NULL,
  9. author VARCHAR(20) CHECK(author <> ' ') NOT NULL,
  10. category CHAR(10) CHECK(category IN('Science', 'Lifestyle', 'Arts', 'Leisure')) NOT NULL,
  11. price DECIMAL(6,2) Default 0.0, CHECK(price>=0.0),
  12. sales INTEGER NOT NULL);
  13.  
  14. CREATE TABLE customer (
  15. cno INTEGER PRIMARY KEY,
  16. name VARCHAR(20) CHECK(name<> ' '),
  17. address VARCHAR(30) NOT NULL,
  18. balance DECIMAL(8,2) CHECK(balance >= 0.0) DEFAULT 0);
  19.  
  20. CREATE TABLE bookOrder (
  21. cno INTEGER NOT NULL,
  22. bno INTEGER NOT NULL,
  23. orderTime TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  24. qty INTEGER NOT NULL,
  25. CONSTRAINT bookOrderfk1 FOREIGN KEY(cno) REFERENCES customer
  26. ON DELETE RESTRICT,
  27. CONSTRAINT bookOrderfk2 FOREIGN KEY(bno) REFERENCES book
  28. ON DELETE RESTRICT
  29. );
  30.  
  31. --INSERTING TEST DATA
  32.  
  33. INSERT INTO book(bno, title, author, category, price, sales) VALUES(1, 'To Kill A bird', 'Harper Lee', 'Arts', 100.00, 10);
  34. INSERT INTO book(bno, title, author, category, price, sales) VALUES(2, 'Ulysses', 'James Joyce', 'Arts', 50.00, 50);
  35. INSERT INTO book(bno, title, author, category, price, sales) VALUES(3, 'The Great Gatsby', 'F.Scott Fitzgerald', 'Arts', 500.00, 2);
  36. INSERT INTO book(bno, title, author, category, price, sales) VALUES(4, 'Moby Dick', 'Herman Melville', 'Arts', 80.00, 7);
  37. INSERT INTO book(bno, title, author, category, price, sales) VALUES(5, 'The story of TSM', 'Andy Dinh', 'Leisure', 250.00, 6);
  38.  
  39. INSERT INTO customer(cno, name, address, balance) VALUES(1, 'Bjergsen', 'TSM House', 100);
  40. INSERT INTO customer(cno, name, address, balance) VALUES(2, 'Josh', 'Joshs House', 500);
  41. INSERT INTO customer(cno, name, address, balance) VALUES(3, 'Thresh', 'Shadow Isles', 2000);
  42. INSERT INTO customer(cno, name, address, balance) VALUES(4, 'Doublelift', 'Liquid Training Facility', 500);
  43.  
  44. INSERT INTO bookOrder(cno, bno, qty) VALUES(1, 3, 2);
  45.  
  46. SELECT * FROM book;
  47. SELECT * FROM customer;
  48. SELECT * FROM bookOrder;
  49.  
  50. --TRIGGERS AND FUNCTIONS
  51. CREATE OR REPLACE FUNCTION updateBalanceOnBookOrder()
  52.  
  53.  
  54.  
  55. --TRANSACTION EXERCISES
  56.  
  57. --A:
  58. INSERT INTO book(bno, title, author, category, price, sales) VALUES(6, 'How Ionia beat Noxus', 'Karma', 'Lifestyle', 5.00, 4);
  59.  
  60. --B:
  61. DELETE * FROM book WHERE bno = 4;
  62.  
  63. --C:
  64. INSERT INTO customer(cno, name, address, balance) VALUES(5, 'DRAVEN', 'Noxus', 9000);
  65.  
  66. --D:
  67. DELETE * from customer WHERE cno = 4;
  68.  
  69. --E:
  70. INSERT INTO bookOrder(cno, bno, qty) VALUES(5, 6, 1);
  71. CREATE OR REPLACE FUNCTION update_sales()
  72.  
  73.  
  74. CREATE OR REPLACE FUNCTION update_balance()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement