-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent_Database_System.sql
More file actions
63 lines (47 loc) · 1.47 KB
/
Student_Database_System.sql
File metadata and controls
63 lines (47 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
CREATE DATABASE university;
USE university;
CREATE TABLE students (
student_id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
age INT,
major VARCHAR(50)
);
CREATE TABLE courses (
course_id INT PRIMARY KEY AUTO_INCREMENT,
course_name VARCHAR(100)
);
CREATE TABLE enrollments (
enrollment_id INT PRIMARY KEY AUTO_INCREMENT,
student_id INT,
course_id INT,
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);
-- Insert Students
INSERT INTO students (name, age, major) VALUES
('John Doe', 20, 'Computer Science'),
('Jane Smith', 22, 'Mathematics'),
('Sam Johnson', 21, 'Physics');
-- Insert Courses
INSERT INTO courses (course_name) VALUES
('Introduction to Programming'),
('Calculus I'),
('Quantum Mechanics');
-- Insert Enrollments
INSERT INTO enrollments (student_id, course_id) VALUES
(1, 1),
(1, 2),
(2, 2),
(3, 3);
SELECT students.*, courses.course_name
FROM students
JOIN enrollments ON students.student_id = enrollments.student_id
JOIN courses ON courses.course_id = enrollments.course_id;
SELECT courses.*, students.name AS student_name
FROM courses
JOIN enrollments ON courses.course_id = enrollments.course_id
JOIN students ON students.student_id = enrollments.student_id;
SELECT students.name AS student_name, courses.course_name
FROM enrollments
JOIN students ON students.student_id = enrollments.student_id
JOIN courses ON courses.course_id = enrollments.course_id;