Advertisement
JmihPodvalbniy

Untitled

May 21st, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 4.31 KB | Software | 0 0
  1. --Создайте БД и заполните таблицы.
  2. --Создайте три таблицы: `Students`, `Courses`, `StudentCourses`.
  3. --Таблица `Students` должна содержать следующие поля:
  4. --`StudentID` (int, Primary Key, AutoIncrement)
  5. --`FirstName` (nvarchar(50))
  6. --`LastName` (nvarchar(50))
  7. --`BirthDate` (date)
  8. --Таблица `Courses` должна содержать следующие поля:
  9. --`CourseID` (int, Primary Key, AutoIncrement)
  10. --`CourseName` (nvarchar(100))
  11. --`Instructor` (nvarchar(100))
  12. --`CreditHours` (tinyint)
  13. --Таблица `StudentCourses` должна содержать следующие поля для отслеживания принятых студентами курсов и их оценок:
  14. --`StudentID` (int, Foreign Key references Students(StudentID))
  15. --`CourseID` (int, Foreign Key references Courses(CourseID))
  16. --`Grade` (tinyint)
  17. CREATE DATABASE School_DB;
  18.  
  19. USE School_DB;
  20.  
  21. USE School_DB;
  22.  
  23. CREATE TABLE Students (
  24.     StudentID INT PRIMARY KEY IDENTITY(1,1),
  25.     FirstName NVARCHAR(50),
  26.     LastName NVARCHAR(50),
  27.     BirthDate DATE
  28. );
  29.  
  30. CREATE TABLE Courses (
  31.     CourseID INT PRIMARY KEY IDENTITY(1,1),
  32.     CourseName NVARCHAR(100),
  33.     Instructor NVARCHAR(100),
  34.     CreditHours TINYINT
  35. );
  36.  
  37. CREATE TABLE StudentCourses (
  38.     StudentID INT,
  39.     CourseID INT,
  40.     Grade TINYINT,
  41.     PRIMARY KEY (StudentID, CourseID),
  42.     FOREIGN KEY (StudentID) REFERENCES Students(StudentID),
  43.     FOREIGN KEY (CourseID) REFERENCES Courses(CourseID)
  44. );
  45.  
  46. INSERT INTO Students (FirstName, LastName, BirthDate) VALUES
  47.     ('Иван', 'Иванов', '2003-01-15'),
  48.     ('Петр', 'Петров', '2002-08-20'),
  49.     ('Анна', 'Сидорова', '2001-05-10');
  50.  
  51.  
  52. INSERT INTO Courses (CourseName, Instructor, CreditHours) VALUES
  53.     ('Математика', 'Иванов И.И.', 4),
  54.     ('Физика', 'Петрова А.П.', 3),
  55.     ('История', 'Сидоров С.С.', 2);
  56.  
  57.  
  58. INSERT INTO StudentCourses (StudentID, CourseID, Grade) VALUES
  59.     (1, 1, 5),
  60.     (1, 2, 4),
  61.     (2, 1, 3),
  62.     (3, 3, 5);
  63. --Создайте представление `StudentCourseOverview`, которое должно включать следующую информацию о студентах и их курсах:
  64. --`StudentFullName` (ФИО студента)
  65. --`CourseName`
  66. --`Grade`
  67. --`CreditHours`
  68. CREATE VIEW StudentCourseOverview AS
  69. SELECT Students.FirstName + ' ' + Students.LastName AS StudentFullName,
  70.        Courses.CourseName,
  71.        StudentCourses.Grade,
  72.        Courses.CreditHours
  73. FROM Students
  74. JOIN StudentCourses
  75. ON Students.StudentID = StudentCourses.StudentID
  76. JOIN Courses
  77. ON StudentCourses.CourseID = Courses.CourseID;
  78. --Создайте представление `CourseStatistics`, которое показывает статистику по каждому курсу:
  79. --`CourseName`
  80. --`TotalStudentsEnrolled` (общее количество студентов, зарегистрированных на курс)
  81. --`AverageGrade` (средняя оценка по курсу)
  82. CREATE VIEW CourseStatistics AS
  83. SELECT
  84.     Courses.CourseName,
  85.     COUNT(DISTINCT StudentCourses.StudentID) AS TotalStudentsEnrolled,
  86.     AVG(StudentCourses.Grade) AS AverageGrade
  87. FROM Courses
  88. JOIN StudentCourses
  89. ON Courses.CourseID = StudentCourses.CourseID
  90. GROUP BY Courses.CourseName;
  91. --·        Напишите SQL-запросы для извлечения информации из созданных представлений.
  92. SELECT * FROM StudentCourseOverview;
  93. SELECT * FROM CourseStatistics;
  94. --·        Один запрос должен выводить информацию о всех курсах и средних оценках по ним.
  95. SELECT CourseName, AverageGrade
  96. FROM CourseStatistics;
  97. --·        Другой запрос должен выводить информацию о всех курсах, которые проходит конкретный студент, включая название курса, оценку --и количество кредитных часов.
  98. DECLARE @StudentFullName NVARCHAR(100);
  99.  
  100. SET @StudentFullName = 'Иван Иванов';
  101. SELECT StudentFullName, CourseName, Grade, CreditHours
  102. FROM StudentCourseOverview
  103. WHERE StudentFullName = @StudentFullName;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement