Advertisement
brandblox

Assignment 2

Nov 14th, 2023
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.88 KB | None | 0 0
  1. -- Create Project table
  2. CREATE TABLE Project (
  3.     pno NUMBER PRIMARY KEY,
  4.     pname VARCHAR2(50) NOT NULL,
  5.     manager_no NUMBER,
  6.     CONSTRAINT FK_Manager FOREIGN KEY (manager_no) REFERENCES Manager_details(manager_no)
  7. );
  8.  
  9. -- Create Manager_details table
  10. CREATE TABLE Manager_details (
  11.     manager_no NUMBER PRIMARY KEY,
  12.     manager_name VARCHAR2(50)
  13. );
  14.  
  15. INSERT INTO Manager_details (manager_no, manager_name) VALUES (101, 'John Manager');
  16. INSERT INTO Manager_details (manager_no, manager_name) VALUES (102, 'Jane Manager');
  17.  
  18. -- Ensure that the manager_no in Project references existing manager_no in Manager_details
  19. INSERT INTO Project (pno, pname, manager_no) VALUES (1, 'Project A', 101);
  20. INSERT INTO Project (pno, pname, manager_no) VALUES (2, 'Project B', 102);
  21. -- Try to insert a record with a non-existent manager_no, which will be restricted
  22. INSERT INTO Project (pno, pname, manager_no) VALUES (3, 'Project C', 103); -- This should fail
  23.  
  24. -- Alter the EMP table to add 'commission' attribute and modify 'sal' datatype
  25. ALTER TABLE EMP ADD commission NUMBER(7, 2);
  26.  
  27. -- Modify the datatype of 'sal'
  28. ALTER TABLE EMP MODIFY sal NUMBER(7, 2);
  29.  
  30. -- Insert a new record with all attributes
  31. INSERT INTO EMP (empid, name, date_of_birth, city, salary, commission, dob, designation, dependence)
  32. VALUES (201, 'New Employee', TO_DATE('1990-01-01', 'YYYY-MM-DD'), 'City X', 80000.00, 1000.00, TO_DATE('2022-01-01', 'YYYY-MM-DD'), 'Analyst', 1);
  33.  
  34. -- Add 'designation' attribute to EMP table
  35. ALTER TABLE EMP ADD designation VARCHAR2(50);
  36.  
  37. -- Update designation for existing records
  38. UPDATE EMP SET designation = 'Programmer' WHERE empid IN (101, 102, 103);
  39.  
  40. -- Add 'manager_no' attribute to EMP table
  41. ALTER TABLE EMP ADD manager_no NUMBER(10);
  42.  
  43. -- Update 'manager_no' for existing records
  44. UPDATE EMP SET manager_no = 1001 WHERE empid = 101;
  45. UPDATE EMP SET manager_no = 1002 WHERE empid = 102;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement