0% found this document useful (0 votes)
2 views2 pages

Student Database SQL

The document provides SQL codes for creating a student management database named 'school'. It includes the creation of three tables: 'students', 'courses', and 'enrollments' with appropriate foreign key relationships. Additionally, it demonstrates how to insert data into these tables and perform a JOIN query to retrieve student names along with their enrolled course names.

Uploaded by

ab.wadood2024
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Student Database SQL

The document provides SQL codes for creating a student management database named 'school'. It includes the creation of three tables: 'students', 'courses', and 'enrollments' with appropriate foreign key relationships. Additionally, it demonstrates how to insert data into these tables and perform a JOIN query to retrieve student names along with their enrolled course names.

Uploaded by

ab.wadood2024
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Student Management Database (SQL

Codes)
Database Creation
CREATE DATABASE school;
USE school;

Students Table
CREATE TABLE students (
student_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
age INT
);

Courses Table
CREATE TABLE courses (
course_id INT AUTO_INCREMENT PRIMARY KEY,
course_name VARCHAR(100) NOT NULL
);

Enrollments Table (Foreign Keys)


CREATE TABLE enrollments (
enroll_id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT,
course_id INT,
FOREIGN KEY (student_id) REFERENCES students(student_id),
FOREIGN KEY (course_id) REFERENCES courses(course_id)
);

Insert Data
INSERT INTO students(name, age) VALUES ('Ali', 20);
INSERT INTO courses(course_name) VALUES ('Computer Science');
INSERT INTO enrollments(student_id, course_id) VALUES (1, 1);
JOIN Query
SELECT
[Link],
courses.course_name
FROM enrollments
JOIN students ON enrollments.student_id = students.student_id
JOIN courses ON enrollments.course_id = courses.course_id;

You might also like