Advertisement
Teammasik

DataBase_labs(2-6)

Apr 17th, 2023 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 10.52 KB | None | 0 0
  1. --lr2
  2. create table Subject_1
  3. (Subj_id integer primary key,
  4. Subj_name varchar(20),
  5. Hour smallint,
  6. )
  7. create table Lecturer_1
  8. (Lecturer_id integer primary key,
  9. Surname varchar(20),
  10. Name varchar(20),
  11. Middle_name varchar(20),
  12. City varchar(20),
  13. )
  14. create table Semester_plan_1
  15. (Semester_id integer primary key,
  16. Lecturer_id integer FOREIGN KEY (Lecturer_id) REFERENCES Lecturer_1 (Lecturer_id),
  17. Subj_id integer foreign key references Subject_1(Subj_id),
  18. Semester tinyint,
  19. )
  20. create table University_1
  21. (Univ_id integer primary key,
  22. Univ_name varchar(20),
  23. Rating tinyint,
  24. City varchar(20),
  25. )
  26. create table Student_1
  27. (Student_id integer primary key,
  28. Name varchar(20),
  29. Surname varchar(20),
  30. Middle_name varchar(20),
  31. City varchar(20),
  32. Birthday date,
  33. Univ_id integer foreign key references University_1(Univ_id) ON UPDATE CASCADE ON DELETE set null,
  34. )
  35. create table Exam_marks_1
  36. (Exam_id integer primary key,
  37. Student_id integer foreign key(Student_id) references Student_1(Student_id)ON UPDATE CASCADE ON DELETE CASCADE,
  38. Subj_id integer foreign key(Subj_id) references Subject_1(Subj_id)ON UPDATE CASCADE ON DELETE CASCADE,
  39. Mark decimal(3,2),
  40. Exam_date date,
  41. )
  42.  
  43. create table University_2
  44. (Univ_id integer not null,
  45. Univ_name varchar(20),
  46. Rating tinyint,
  47. City varchar(20),
  48. )
  49. create table Student_2
  50. (Student_id integer not null,
  51. Name varchar(20),
  52. Surname varchar(20),
  53. Middle_name varchar(20),
  54. City varchar(20),
  55. Birthday date,
  56. Univ_id integer,
  57. )
  58. alter table University_2
  59. add primary key(Univ_id);
  60.  
  61. alter table Student_2
  62. add primary key(Student_id);
  63. alter table Student_2
  64. add constraint Univ_id foreign key(Univ_id) references University_1(Univ_id) ON UPDATE CASCADE ON DELETE CASCADE;
  65.  
  66.  
  67. ---------------------------------------------------------------------------------------------------------------
  68.  
  69. select Subj_name, Hour
  70. from Subject_1
  71.  
  72. select Subj_id
  73. from Exam_marks_1
  74. where Subj_id = 5
  75.  
  76. select Addres, Surname, Name
  77. from Student_1
  78.  
  79. select distinct Mark
  80. from Exam_marks_1
  81.  
  82. select Kurs, Surname
  83. from Student_1
  84. where Kurs >= 3
  85.  
  86. Select Kurs, Surname, Name, Stipendia
  87. from Student_1
  88. where Stipendia > 2500
  89.  
  90. select Hour, Subj_name
  91. from Subject_1
  92. where Hour > 26
  93.  
  94. select Univ_name, Rating
  95. from University_1
  96. where Rating > 60
  97.  
  98. Select Kurs, Surname, Name, Stipendia, City
  99. from Student_1
  100. where Stipendia >= 2500 and City = 'Уфа'
  101.  
  102. select*
  103. from Student_1
  104. where (Stipendia < 100 or not(Birthday >= '10/03/1995' and Student_id > 5))
  105.  
  106. select*
  107. from Student_1
  108. where not ((Birthday >= '10/03/1995' or Stipendia > 100) and Student_id >= 3)
  109.  
  110.  
  111.  
  112.  
  113.  
  114. --lr 3
  115. SELECT Subj_Id, Exam_Date
  116. FROM Exam_mark
  117. WHERE Exam_Date BETWEEN '2019-01-10' AND '2019-01-21'
  118.  
  119. SELECT Subj_Id,Student_Id
  120. FROM Exam_mark
  121. WHERE Student_Id IN (10, 12)
  122.  
  123. SELECT Subj_name
  124. FROM Subject
  125. WHERE Subj_name LIKE %'
  126.  
  127. SELECT *
  128. FROM Student
  129. WHERE Surname LIKE '%[ИС]%'
  130.  
  131. SELECT *
  132. FROM Student
  133. WHERE Name LIKE '_В%' or Name LIKE '__Р%'
  134.  
  135. SELECT *
  136. FROM Exam_mark
  137. WHERE Mark IS NULL
  138.  
  139. SELECT *
  140. FROM Exam_mark
  141. WHERE Mark IS NOT NULL
  142.  
  143. SELECT UPPER(Surname)+'; '+UPPER(Name)+'; '+UPPER(City)+'; '
  144. + concat(convert(varchar(20),Birthday,103), '; ',Univ_Id, '.')
  145. FROM Student
  146.  
  147. SELECT SUBSTRING(Name, 1, 1)+'. '+ Surname +'; место жительства - '+City+'; родился - ' + concat(convert(varchar(20),Birthday,104), '.')
  148. FROM Student
  149.  
  150. SELECT LOWER(SUBSTRING(Name, 1, 1))+'. '+LOWER(Surname)+'; место жительства - '+LOWER(City)+
  151. '; родился: '+concat(replace(convert(varchar(20),Birthday,106), ' ', '-'), '.')
  152. FROM Student
  153.  
  154. SELECT Surname+' '+Name+' родился в '+concat(year(Birthday),' году.')
  155. FROM Student
  156.  
  157. SELECT Surname, Name, (Stipendia*100) AS 'Стипендия'
  158. FROM Student   
  159.  
  160. SELECT UPPER(Surname)+' '+UPPER(Name)+' родился - '+concat(year(Birthday),' году')  
  161. FROM Student WHERE Kurs=1 or Kurs=2 or Kurs=4
  162.  
  163. SELECT UPPER(Univ_Name)+'-г.'+UPPER(City)+'; РЕЙТИНГ='+UPPER(Rating)
  164. FROM University
  165.  
  166. SELECT Univ_name+'-г.'+City+concat('; Рейтинг=',round(Rating,-2),'.')
  167. FROM University
  168.  
  169.  
  170.  
  171.  
  172. --lr 4
  173. --1)
  174. SELECT COUNT(Student_Id) as 'students'
  175. FROM Exam_mark
  176. WHERE Subj_Id = 2
  177.  
  178. --2)
  179. SELECT COUNT(DISTINCT Subj_Id) as 'Количество дисциплин'
  180. FROM Exam_mark;
  181.  
  182. --3)
  183. SELECT Student_Id, MIN(Mark) as 'Мин оценка'
  184. FROM Exam_mark
  185. GROUP BY Student_Id;
  186.  
  187. --4)
  188. SELECT Student_Id, MAX(Mark) as 'Макс оценка'
  189. FROM Exam_mark
  190. GROUP BY Student_Id;
  191.  
  192. --5)
  193. SELECT Subj_Id, MIN(Mark) as 'Мин оценка'
  194. FROM Exam_mark
  195. GROUP BY Subj_Id;
  196.  
  197. --6)
  198. SELECT Exam_Date, COUNT(Student_Id) as 'Кол-во студентов'
  199. FROM Exam_mark
  200. GROUP BY Exam_Date;
  201.  
  202. --7)
  203. SELECT Subj_Id, AVG(Mark) as 'Средний балл'
  204. FROM Exam_mark
  205. GROUP BY Subj_Id
  206.  
  207. --8)
  208. SELECT Student_Id, AVG(Mark) as 'Средний балл'
  209. FROM Exam_mark
  210. GROUP BY Student_Id
  211.  
  212. --9)
  213. SELECT COUNT(DISTINCT Subj_Id) as 'Кол-во дисциплин'
  214. FROM Exam_mark
  215. WHERE Mark>4
  216.  
  217. ------------------------------------------------------------------
  218. --1)*
  219. SELECT Student_id, Surname, Stipendia,(Stipendia*120)/100 AS 'Стипендия +20%'
  220. FROM Student
  221. ORDER BY Stipendia, Surname
  222.  
  223. --2)
  224. SELECT TOP (2) Surname
  225. FROM Student WHERE Surname like %'
  226. ORDER BY Surname
  227.  
  228. --3)
  229. SELECT Student_id, max(Mark) as 'Макс. оценка' , min(Mark) as 'Мин. оценка'
  230. FROM Exam_mark
  231. GROUP BY Student_Id
  232.  
  233. --4)*
  234. SELECT Subj_Name, Hour
  235. FROM Subject
  236. ORDER BY Subj_Name DESC, hour asc
  237.  
  238. --5)
  239. SELECT Exam_Date, Sum(Mark) as 'Сумма баллов'
  240. FROM Exam_mark
  241. GROUP BY Exam_Date
  242. ORDER BY Sum(Mark) DESC
  243.  
  244. --6)*
  245. SELECT AVG(Mark) as 'Средняя оценка',MIN(Mark) as 'Минимальная оценка',MAX(Mark) as 'Максимальная оценка', Exam_Date
  246. FROM Exam_mark
  247. GROUP BY Exam_Date
  248. ORDER BY 1 DESC, 2 desc, 3 desc
  249.  
  250.  
  251.  
  252.  
  253. --lr 5
  254. --1
  255. INSERT INTO Department (Full_name, Hours)
  256. VALUES ('Производственный', 150);
  257.  
  258. --2
  259. insert into Company(Full_name, Short_name, City)
  260. values ('Нефтяная компания Башнефть','Башнефть','Воронеж')
  261.  
  262. INSERT INTO Department (Full_name, Company_id)
  263. VALUES ('Научный', 4);
  264.  
  265. INSERT INTO Cooperator (Surname, Name, City, Dept_id)
  266. VALUES ('Орлов','Николай', 'Воронеж', 3);
  267.  
  268. --3
  269. delete  from Evaluation
  270. where Cooperator_id = 6
  271.  
  272. --4
  273. update Company
  274. set Coefficient = Coefficient + 1
  275. where City = 'Санкт-Петербург'
  276.  
  277. --5
  278. update Cooperator
  279. set City = 'Москва'
  280. where Surname = 'Иванов'
  281.  
  282. --6
  283. INSERT INTO Good_Cooperator (Surname, Name, City, Phone_number, Passport_num, Start_date, Salary, Category, Dept_id)
  284. SELECT Surname, Name, City, Phone_number, Passport_num, Start_date, Salary, Category, Dept_id
  285. FROM Cooperator
  286. WHERE Cooperator_id IN
  287.         (SELECT Cooperator_id
  288.         FROM Evaluation
  289.         WHERE 3 <=
  290.             (SELECT Count(Status)
  291.             FROM Evaluation
  292.             WHERE Evaluation.Cooperator_id = Cooperator.Cooperator_id AND Evaluation.Status > 2));
  293.  
  294. --7
  295. DELETE FROM Cooperator
  296. WHERE Cooperator_id NOT IN
  297.     (SELECT Cooperator_id
  298.     FROM Evaluation
  299.     WHERE Cooperator.Cooperator_id = Evaluation.Cooperator_id);
  300.  
  301.  
  302. --8-
  303. UPDATE Cooperator
  304. SET Salary = Salary * 0.9
  305. WHERE 3 >=
  306.     (SELECT AVG(Status)
  307.     FROM Evaluation
  308.     WHERE Evaluation.Cooperator_id = Cooperator.Cooperator_id)
  309.  
  310. --9
  311. UPDATE Cooperator
  312. SET Salary = Salary * 1.2
  313. WHERE 4.7 <
  314.     (SELECT MIN(Status)
  315.     FROM Evaluation
  316.     WHERE Evaluation.Cooperator_id = Cooperator.Cooperator_id)
  317.  
  318.  
  319.  
  320.  
  321. --test for defense
  322. insert into Cooperator (Surname, Name, Salary, City, Dept_id)
  323. values ('Бычков', 'Алексанdр', 90000 ,'Воронеж', 1)
  324.  
  325. select Dept_id, (
  326. select count(Cooperator_id)
  327. from Cooperator
  328. where Dept_id = A.Dept_id
  329. )
  330. from Department A;
  331.  
  332.  
  333. --lr 6
  334. --1
  335. SELECT *
  336. FROM Evaluation
  337. WHERE Cooperator_id IN (
  338.     SELECT Cooperator_id
  339.     FROM Cooperator
  340.     WHERE Surname = 'Иванов');
  341.  
  342.  
  343. --2
  344. SELECT Name
  345. FROM Cooperator
  346. WHERE Cooperator.Dept_id = 5 AND Cooperator_id IN (
  347.     SELECT Cooperator_id
  348.     FROM Evaluation
  349.     WHERE Status > (SELECT AVG(Status)
  350.                    FROM Evaluation))
  351.  
  352.  
  353. --3
  354. SELECT Name
  355. FROM Cooperator
  356. WHERE Dept_id = 7 AND Cooperator_id IN (
  357.         SELECT Cooperator_id
  358.         FROM Evaluation
  359.         WHERE Status < (
  360.             SELECT AVG(Status)
  361.             FROM Evaluation));
  362.  
  363.  
  364. --4
  365. select Dept_id /*count(*)*/ as amount_of_Deps
  366. from Department D
  367. where (select min(Stat_count)
  368.     from (select count(Status) as Stat_count
  369.         from Evaluation Ev
  370.         where Cooperator_id in (select Cooperator_id
  371.                          from Cooperator
  372.                          where Dept_id = D.Dept_id)
  373.         group by Cooperator_id) B) >3;
  374.  
  375.  
  376. --5
  377. SELECT C1.Surname, C1.Name, C1.Dept_id
  378. FROM Cooperator AS C1
  379. WHERE C1.Salary = (
  380.     SELECT MAX(C2.Salary)
  381.     FROM Cooperator AS C2
  382.     WHERE C1.Dept_id = C2.Dept_id);
  383.  
  384.  
  385. --6
  386. SELECT Surname, Name
  387. FROM Cooperator
  388. WHERE City NOT IN (
  389.       SELECT DISTINCT City
  390.       FROM Company)
  391.  
  392.  
  393. --7
  394. SELECT Coop.Cooperator_id, Coop.Name, Coop.Surname, Coop.City, Comn.City as City_of_comp
  395. FROM Cooperator AS Coop, Department AS Dept, Company AS Comn
  396. WHERE Coop.Dept_id = Dept.Dept_id
  397.     AND Comn.Company_id = Dept.Company_id
  398.         AND Coop.City != Comn.City;
  399.  
  400. SELECT Cooperator_id, Name, Surname, City
  401. FROM Cooperator AS C
  402. WHERE C.Dept_id IN (
  403.     SELECT Dept_id
  404.     FROM Department
  405.     WHERE Company_id IN (
  406.         SELECT Company_id
  407.         FROM Company
  408.         WHERE City != C.City));
  409.  
  410.  
  411.  
  412. --------------------------------------------------------------------------------------
  413. --8
  414. SELECT DISTINCT Cooperator.Cooperator_id, Surname, Name, City, Salary
  415. FROM Cooperator
  416. INNER JOIN Evaluation ON Cooperator.Cooperator_id = Evaluation.Cooperator_id
  417. WHERE EXISTS (SELECT Cooperator_id
  418.               FROM Evaluation
  419.               WHERE Cooperator.Cooperator_id = Evaluation.Cooperator_id
  420.               GROUP BY Cooperator_id
  421.               HAVING MIN(Status) > 3);
  422.  
  423.  
  424. --9
  425. select distinct Cp.Cooperator_id, Cp.Surname, Cp.Name, City, Salary
  426. from Cooperator Cp, Evaluation Ev
  427. where Cp.Cooperator_id = Ev.Cooperator_id and 3 <
  428.                                             (select min(Status)
  429.                                             from Evaluation
  430.                                             where Cooperator_id = Cp.Cooperator_id);
  431.  
  432.  
  433.  
  434. --10
  435. SELECT *
  436. FROM Cooperator c
  437. WHERE EXISTS (
  438.     SELECT *
  439.     FROM Department d
  440.     WHERE d.Dept_id = c.Dept_id
  441.     AND c.City = (
  442.         SELECT City
  443.         FROM Company
  444.         WHERE d.Company_id = Company_id));
  445.  
  446.  
  447. --11
  448. SELECT d.Full_name
  449. FROM DEPARTMENT d, Cooperator c
  450. WHERE d.Dept_id = c.Dept_id AND EXISTS (
  451.     SELECT *
  452.     FROM Evaluation e
  453.     WHERE e.Cooperator_id = c.Cooperator_id)
  454. GROUP BY d.Full_name
  455. HAVING COUNT(DISTINCT c.Cooperator_id) > 1;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement