Advertisement
Mary_99

lab6

Nov 6th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 4.26 KB | None | 0 0
  1. --1. Create a new view v_clerk which retrieves the rows of the works_on table,
  2. --for which the condition job = 'Clerk' evaluates to true.
  3.  
  4. CREATE VIEW v_clerk AS
  5. SELECT *
  6. FROM works_on
  7. WHERE job = 'Clerk'
  8.  
  9. -- Create a new view v_without_budget which contains all columns of the project table except the budget column.
  10. CREATE VIEW v_without_budget AS
  11. SELECT project_no, project_name
  12. FROM project
  13. -- Create a new view containing project_no and the number of employees involved.
  14. CREATE VIEW containing_project_no AS
  15. SELECT  project_no ,COUNT(emp_no) AS SUM
  16. FROM works_on
  17. GROUP BY  project_no
  18. -- Create a new view v_clerk2 which is derived from the v_clerk view, containing only emp_no. 5
  19.  
  20. CREATE VIEW  v_clerk2  AS
  21. SELECT  emp_no  
  22. FROM v_clerk
  23.  
  24. --Using the ALTER VIEW statement extend the SELECT statement of
  25. --the v_without_budget view with the new condition in the WHERE clause - project_no >= 'p3'.
  26.  
  27. ALTER VIEW v_without_budget  AS
  28. SELECT project_no, project_name
  29. FROM project
  30. WHERE project_no >=  'p3'
  31.  
  32. ----Remove the definition of the v_clerk view from the system tables
  33. DROP VIEW v_clerk
  34.  
  35. -- Create a new view v_d2 which retrieves emp_no and emp_lname working at d2 department.
  36. CREATE VIEW v_d2 AS
  37. SELECT emp_no,emp_lname, dept_no
  38. FROM employee
  39. WHERE dept_no = 'd2'
  40.  
  41. -- Then select, from this view, employees whose name contain the letter J.
  42.  
  43. SELECT *
  44. FROM v_d2
  45. WHERE emp_lname LIKE '%[J]%'
  46.  
  47. --Create a new view v_dept containing dept_no and dept_name. Afterwards, insert a new department d4 ('Development') into this view.
  48. CREATE VIEW v_dept AS
  49. SELECT dept_no , dept_name
  50. FROM department
  51. INSERT INTO v_dept  VALUES ('d4','Development')
  52.  
  53. -- Create a new view v_p1 for p1 project, containing emp_no and job. Update this view setting NULL instead of the 'Manager' job.
  54. CREATE VIEW v_p1 AS
  55. SELECT emp_no, job, project_no
  56. FROM works_on
  57. WHERE project_no = 'p1'
  58. UPDATE  v_p1
  59. SET job=NULL
  60. WHERE job ='Manager';
  61.  
  62. --- Delete rows connected with clerks from view v_p1.
  63. DELETE FROM v_p1
  64. WHERE job ='clerk'
  65.  
  66. --Create a view that comprises the data of all employees that work for the department d1.
  67.  
  68. CREATE VIEW empt_data AS
  69. SELECT*
  70. FROM employee
  71. WHERE dept_no = 'd1'
  72.  
  73. -- For the project table, create a view that can be used by employees who are allowed to view all data of this table except
  74. --the budget column.
  75.  
  76. CREATE VIEW lel AS
  77. SELECT project_name, project_no
  78. FROM project
  79. --. Create a view that comprises the first and last names of all employees
  80. --who entered their projects in the second half of the year 2007.
  81. CREATE VIEW zad14 AS
  82. SELECT emp_lname, emp_fname, works_on.enter_date
  83. FROM employee
  84. JOIN works_on
  85. ON works_on.emp_no = employee.emp_no
  86. WHERE enter_date >'01-07-2007'
  87.  
  88. --15, Solve Exercise 13 so that the original columns f_name and l_name have new names in the view: first and last, respectively.
  89.  
  90. EXEC sp_rename 'v_p1.emp_fname', 'first', 'COLUMN'
  91. EXEC sp_rename 'v_p1.emp_lname', 'last', 'COLUMN'
  92.  
  93. ---16. Use the view in 11 to display full details of every employee whose last name begins with the letter m
  94. SELECT *
  95. FROM v_p1
  96. WHERE v_p1.LAST LIKE '[m]%'--dont work
  97.  
  98. --17  Create a view that comprises full details of all projects on which the employee named Smith works.
  99. CREATE VIEW hehe AS
  100. SELECT project.BY=udget, project.project_name
  101. FROM project
  102. JOIN works_on
  103. ON works_on.emp_no = employee.emp_no
  104. WHERE empl_lname = 'Smith'
  105.  
  106.  
  107.  
  108.  
  109. --Create a view that comprises full details of all projects on which the employee named Smith works.
  110. CREATE VIEW [zad17a] AS
  111. SELECT project.budget, project.project_name
  112. FROM project
  113. JOIN works_on
  114. ON project.project_no=works_on.project_no
  115. JOIN employee
  116. ON works_on.emp_no=employee.emp_no
  117. WHERE employee.emp_lname='Smith'
  118.  
  119. --Using the ALTER VIEW statement, modify the condition in the view in 11.The modified view should comprise the data of all employees that work either for the department d1 or d2, or both.
  120. ALTER VIEW new1
  121. AS  
  122. SELECT  * FROM employee
  123. WHERE dept_no= 'd1' OR dept_no= 'd2'
  124.  
  125. --Delete the view created in 13. What happens with the view created in 14
  126. DROP VIEW zad13
  127. SELECT * FROM zad14
  128.  
  129. --Using the view from 12, insert the details of the new project with the project number p2 and the name Moon
  130. INSERT INTO[v_without_budget]
  131. VALUES ('moon', 'p2')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement