Advertisement
Gaudenz

Performance Task 1-3

Sep 11th, 2024 (edited)
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 3.56 KB | Science | 0 0
  1. -- CREATE a database
  2. CREATE DATABASE school_management_sys;
  3.  
  4. USE school_management_sys;
  5.  
  6.  
  7. -- Table Creation
  8. CREATE TABLE students (
  9.     student_id INT AUTO_INCREMENT PRIMARY KEY,
  10.     first_name VARCHAR(50),
  11.     last_name VARCHAR(50),
  12.     date_of_birth DATE
  13. );
  14.  
  15. CREATE TABLE teachers (
  16.     teacher_id INT AUTO_INCREMENT PRIMARY KEY,
  17.     teacher_name VARCHAR(50),
  18.     teacher_department VARCHAR(10),
  19.     address VARCHAR(100)
  20. );
  21.  
  22. CREATE TABLE courses (
  23.     course_id INT AUTO_INCREMENT PRIMARY KEY,
  24.     course_name VARCHAR(50),
  25.     credits INT
  26. );
  27.  
  28. CREATE TABLE enrollments (
  29.     enrollment_id INT AUTO_INCREMENT PRIMARY KEY,
  30.     student_id INT,
  31.     course_id INT,
  32.     enrollment_date DATE,
  33.     CONSTRAINT fk_students FOREIGN KEY (student_id) REFERENCES students(student_id) ON DELETE CASCADE,
  34.     CONSTRAINT fk_courses FOREIGN KEY (course_id) REFERENCES courses(course_id) ON DELETE CASCADE
  35. );
  36.  
  37.  
  38. -- ALTER/MODIFY THE TABLES
  39. ALTER TABLE students ADD email VARCHAR(100);
  40.  
  41. ALTER TABLE courses MODIFY course_name VARCHAR(200);
  42. -- modify course name for longer course title
  43.  
  44. ALTER TABLE teachers DROP COLUMN address;
  45.  
  46.  
  47. -- INSERT ROWS OF DATA
  48. INSERT INTO students (first_name, last_name, date_of_birth, email)
  49. VALUES ('khier', 'lapurga', '2007-09-11', 'khier.pogi123@gmail.com'),
  50.        ('john', 'credo', '2006-07-11', 'john.onel@gmail.com'),
  51.        ('fred', 'santiago', '2004-07-11', 'running.fred@gmial.com');
  52.  
  53. INSERT INTO courses (course_name, credits)
  54. VALUES ('Introduction to Computer Science', 1),
  55.        ('Data Structures and Algorithms', 4),
  56.        ('Object-Oriented Programming', 2);
  57.  
  58. INSERT INTO enrollments (student_id, course_id, enrollment_date)
  59. VALUES  (1,3,'2024-11-09'),
  60.         (2,2,'2024-10-01'),
  61.         (3,1,'2023-01-05');
  62.  
  63. -- RETRIEVE DATA
  64. SELECT
  65.     students.student_id,
  66.     students.first_name,
  67.     students.last_name,
  68.     students.email,
  69.     courses.course_name,
  70.     enrollments.enrollment_date
  71. FROM enrollments
  72. JOIN students ON enrollments.student_id = students.student_id
  73. JOIN courses ON enrollments.course_id = courses.course_id;
  74.  
  75.  
  76. -- DROPPING/Turncating Tables
  77.  
  78. -- DEPENDE: Identify the foreign key constraints
  79. -- SHOW CREATE TABLE enrollments;
  80.  
  81. -- Drop foreign key constraints
  82. ALTER TABLE enrollments
  83. DROP FOREIGN KEY 'fk_courses'; -- Adjust this name based on your SHOW CREATE TABLE output
  84.  
  85. -- Drop the courses table
  86. DROP TABLE courses;
  87.  
  88.  
  89. -- Truncate the enrollments table
  90. TRUNCATE TABLE enrollments;
  91.  
  92. -- TRUNCATE the students
  93. -- OR WAG NA?
  94.  
  95. -- INSERT new data for the new semester
  96. -- MAKE A NEW COURSES STRUCTURE
  97. CREATE TABLE courses (
  98.     course_id INT AUTO_INCREMENT PRIMARY KEY,
  99.     course_name VARCHAR(200) NOT NULL,
  100.     credits FLOAT NOT NULL,
  101.     course_code VARCHAR(20) UNIQUE NOT NULL,
  102.     description TEXT,
  103.     teacher_id INT,
  104.     semester VARCHAR(50),
  105.     max_students INT DEFAULT 30,
  106.     CONSTRAINT fk_teachers FOREIGN KEY (teacher_id) REFERENCES teachers(teacher_id) ON DELETE SET NULL
  107. );
  108.  
  109.  
  110.  
  111. -- Insert new enrollments for the new semester
  112. INSERT INTO enrollments (student_id, course_id, enrollment_date)
  113. VALUES  
  114.     (1, 1, '2024-09-01'),  --
  115.     (2, 2, '2024-09-02'),  --
  116.     (3, 3, '2024-09-03');  --
  117.  
  118. SELECT students.first_name,
  119.                students.last_name,
  120.                students.email,
  121.                courses.course_name,
  122.                enrollment.enrollment_date
  123. FROM enrollments
  124. JOIN students ON enrollments.students_id = students.student_id
  125. JOIN courses ON enrollment.course_id = courses.course_id;
  126.  
  127.  
  128. https://drawsql.app/teams/aquabluejay/diagrams/reporting-diagram
Tags: sql
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement