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

Attendance System - SQL

The document outlines the SQL commands to create a database schema for a school management system, including tables for teachers, classes, students, and attendance. It establishes relationships between these tables through foreign keys and includes sample data for teachers, classes, and students. The schema supports features like class enrollment and attendance tracking for students.

Uploaded by

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

Attendance System - SQL

The document outlines the SQL commands to create a database schema for a school management system, including tables for teachers, classes, students, and attendance. It establishes relationships between these tables through foreign keys and includes sample data for teachers, classes, and students. The schema supports features like class enrollment and attendance tracking for students.

Uploaded by

marwanea826
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

-- Create teachers table

CREATE TABLE teachers (


id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
name VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create classes table


CREATE TABLE classes (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
teacher_id INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (teacher_id) REFERENCES teachers(id) ON DELETE CASCADE,
UNIQUE(name, teacher_id)
);

-- Create students table


CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Create class_students junction table


CREATE TABLE class_students (
class_id INT,
student_id INT,
PRIMARY KEY (class_id, student_id),
FOREIGN KEY (class_id) REFERENCES classes(id) ON DELETE CASCADE,
FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE CASCADE
);

-- Create attendance table


CREATE TABLE attendance (
id INT AUTO_INCREMENT PRIMARY KEY,
student_id INT NOT NULL,
class_id INT NOT NULL,
check_in TIMESTAMP NULL,
check_out TIMESTAMP NULL,
date DATE NOT NULL DEFAULT (CURRENT_DATE),
UNIQUE(student_id, class_id, date),
FOREIGN KEY (student_id) REFERENCES students(id) ON DELETE CASCADE,
FOREIGN KEY (class_id) REFERENCES classes(id) ON DELETE CASCADE
);

-- Add some sample data


INSERT INTO teachers (email, password, name) VALUES
('[email protected]',
'$2a$10$rIUQX0WQzRcQiEEJlJHX7OqWQUYOK4xW.H5HOqAVkJQr/2lZDj.PK', 'John Doe');

INSERT INTO classes (name, teacher_id) VALUES


('Mathematics 101', 1),
('Physics 101', 1);

INSERT INTO students (name, email) VALUES


('Alice Smith', '[email protected]'),
('Bob Johnson', '[email protected]'),
('Charlie Brown', '[email protected]');

INSERT INTO class_students (class_id, student_id) VALUES


(1, 1), (1, 2), (2, 1), (2, 3);

You might also like