-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsql.txt
54 lines (43 loc) · 1.49 KB
/
sql.txt
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
-- Create the database
CREATE DATABASE IF NOT EXISTS student_db;
USE student_db;
-- Create a table for storing student details
CREATE TABLE IF NOT EXISTS students (
usn VARCHAR(20) PRIMARY KEY,
sname VARCHAR(50) NOT NULL
);
-- Create a table for storing subject details
CREATE TABLE IF NOT EXISTS subjects (
subject_code VARCHAR(10) PRIMARY KEY,
subject_name VARCHAR(100) NOT NULL,
department VARCHAR(50) NOT NULL
);
-- Create a table for storing marks
CREATE TABLE IF NOT EXISTS marks (
mark_id INT PRIMARY KEY AUTO_INCREMENT,
usn VARCHAR(20) NOT NULL,
subject_code VARCHAR(10) NOT NULL,
semester INT NOT NULL,
marks_obtained INT NOT NULL,
UNIQUE KEY unique_student_subject_semester (usn, subject_code, semester),
FOREIGN KEY (usn) REFERENCES students(usn)
ON DELETE CASCADE
ON UPDATE CASCADE,
FOREIGN KEY (subject_code) REFERENCES subjects(subject_code)
ON DELETE CASCADE
ON UPDATE CASCADE
);
-- Create a procedure to count the number of students and subjects
DROP PROCEDURE IF EXISTS CountStudentsAndSubjects;
DELIMITER //
CREATE PROCEDURE CountStudentsAndSubjects()
BEGIN
DECLARE studentCount INT;
DECLARE subjectCount INT;
-- Count the number of students
SELECT COUNT(*) INTO studentCount FROM students;
-- Count the number of subjects
SELECT COUNT(*) INTO subjectCount FROM subjects;
-- Display the counts
SELECT CONCAT(studentCount) AS StudentCount, CONCAT(subjectCount) AS SubjectCount;
END //