Advertisement
Teammasik

DataBase_defense(2-6)

Apr 17th, 2023 (edited)
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 2.04 KB | None | 0 0
  1. --2
  2. create table pl_coun(
  3. id int primary key
  4. )
  5.  
  6. alter table pl_coun
  7. add id_plant int
  8.  
  9. alter table pl_coun
  10. add constraint FK_country foreign key (id_coun) references country(id_c) on delete set null
  11.  
  12. alter table pl_coun
  13. add constraint FK_plant foreign key (id_plant) references plant(id) on delete set null
  14.  
  15.  
  16.  
  17.  
  18. --3
  19. --фамилия инициалы
  20. --дата рождения название месяца
  21. --условие 1 или 2 курс
  22. --вывод в 1 столбец
  23.  
  24. --select Surname+ '; ' + left(name,1) + '.' + left(Middle_name,1) + '.' + convert(varchar(20),Birthday,106) as 'Data'
  25. select Surname+ '; ' + Substring(Name,1,1) + '.' + Substring(Middle_name,1,1) + '.' + convert(varchar(20),Birthday,106) as 'Data'
  26. from Student
  27. where Kurs in (1,2)
  28.  
  29.  
  30. --4
  31. --вывести для каждого студента мин маки авг оценку
  32.  
  33. select Student_Id, min(Mark) as 'min', avg(Mark) as 'avg', max(Mark) as 'max'
  34. from Exam_mark
  35. where Mark is not null
  36. group by Student_Id
  37.  
  38. --5
  39.  
  40. -- новая таблица, в нее поместить компании в которых кол-во сотрудников > 2
  41.  
  42.  
  43. insert into Good_Company (Full_name, Short_name, City, Coefficient)
  44. select  Full_name, Short_name, City, Coefficient
  45. from Company
  46. where Company_id in (select Company_id
  47.                      from Department
  48.                      where Dept_id in (select Dept_id
  49.                                        from Cooperator
  50.                                        group by Dept_id
  51.                                        having count(cooperator_id) > 2
  52.                                        )
  53.                     )
  54.  
  55. select *
  56. from Company
  57.  
  58. select *
  59. from Department
  60.  
  61. select *
  62. from Cooperator
  63.  
  64. --6
  65. --уменьшать коэф компании, если сотрудников < 3, коэф не должен становится <0
  66.  
  67. update Company
  68. set Coefficient = Coefficient - 1
  69. where Coefficient != 0 and Company_id in(
  70.     select co.Company_id  
  71.     from Company co, Department dp, Cooperator cp
  72.     where (co.Company_id = dp.Company_id and dp.Dept_id = cp.Dept_id)
  73.     group by co.Company_id
  74.     having count(cp.Cooperator_id) < 3
  75. )
  76. select*
  77. from Company
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement