Datenbankschema: Schülerportal
Die Tabellen hinter der App, als SQL, als ER-Diagramm und als Prisma-Schema.
Schülerportal als fertige Vorlage
Das Datenbankschema
Erstellt vom kostenlosen Datenbank-Schema-Generator, nach einer Kurzbeschreibung der App.
Das Modell trennt Stammdaten (Schüler, Klassen, Fächer, Lehrkräfte) von den Unterrichtsstunden, an denen Anwesenheit erfasst wird, und von den Leistungsnachweisen, zu denen Noten eingetragen werden. Konten sind einheitlich in app_user mit einer Rolle abgelegt; Schüler und Lehrkräfte verweisen darauf, damit sich jeder anmelden kann.
Tabellen
app_user: ein Anmeldekonto mit E-Mail, Passwort-Hash und Rolle (Verwaltung, Lehrkraft oder Schüler); das erste registrierte Konto erhält die Rolle Verwaltung.school_class: eine Klasse eines Schuljahres, z. B. „7a“.student: ein Schüler, einer Klasse zugeordnet und mit einem Konto verknüpft.teacher: eine Lehrkraft, mit einem Konto verknüpft.subject: ein Unterrichtsfach, z. B. Mathematik.lesson: eine einzelne Unterrichtsstunde (Klasse, Fach, Lehrkraft, Datum, Beginn und Ende), zugleich Grundlage des Stundenplans.attendance: der Anwesenheitsstatus eines Schülers in einer Unterrichtsstunde.assessment: ein Leistungsnachweis (z. B. Klassenarbeit) für eine Klasse und ein Fach an einem Datum.grade: die Note eines Schülers für einen Leistungsnachweis.announcement: eine Ankündigung der Schule, optional auf eine Klasse beschränkt.
SQL (PostgreSQL)
CREATE TABLE app_user (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
email text NOT NULL UNIQUE,
password_hash text NOT NULL,
role text NOT NULL DEFAULT 'student' CHECK (role IN ('admin', 'teacher', 'student')),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE school_class (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name text NOT NULL,
school_year text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (name, school_year)
);
CREATE TABLE subject (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name text NOT NULL UNIQUE,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE teacher (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid NOT NULL UNIQUE REFERENCES app_user(id) ON DELETE CASCADE, -- is the login of
first_name text NOT NULL,
last_name text NOT NULL,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE student (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid UNIQUE REFERENCES app_user(id) ON DELETE SET NULL, -- is the login of
class_id uuid NOT NULL REFERENCES school_class(id) ON DELETE RESTRICT, -- has as pupils
first_name text NOT NULL,
last_name text NOT NULL,
date_of_birth date,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE lesson (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
class_id uuid NOT NULL REFERENCES school_class(id) ON DELETE CASCADE, -- has
subject_id uuid NOT NULL REFERENCES subject(id) ON DELETE RESTRICT, -- is taught in
teacher_id uuid NOT NULL REFERENCES teacher(id) ON DELETE RESTRICT, -- teaches
lesson_date date NOT NULL,
starts_at time NOT NULL,
ends_at time NOT NULL,
room text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE attendance (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
lesson_id uuid NOT NULL REFERENCES lesson(id) ON DELETE CASCADE, -- records
student_id uuid NOT NULL REFERENCES student(id) ON DELETE CASCADE, -- is recorded for
status text NOT NULL DEFAULT 'present' CHECK (status IN ('present', 'absent_excused', 'absent_unexcused', 'late')),
note text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (lesson_id, student_id)
);
CREATE TABLE assessment (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
class_id uuid NOT NULL REFERENCES school_class(id) ON DELETE CASCADE, -- writes
subject_id uuid NOT NULL REFERENCES subject(id) ON DELETE RESTRICT, -- is examined in
teacher_id uuid NOT NULL REFERENCES teacher(id) ON DELETE RESTRICT, -- sets
title text NOT NULL,
assessment_date date NOT NULL,
weight numeric(4,2) NOT NULL DEFAULT 1.00,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE grade (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
assessment_id uuid NOT NULL REFERENCES assessment(id) ON DELETE CASCADE, -- is graded in
student_id uuid NOT NULL REFERENCES student(id) ON DELETE CASCADE, -- receives
value numeric(3,1) NOT NULL CHECK (value >= 1.0 AND value <= 6.0),
comment text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (assessment_id, student_id)
);
CREATE TABLE announcement (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
author_id uuid NOT NULL REFERENCES app_user(id) ON DELETE RESTRICT, -- publishes
class_id uuid REFERENCES school_class(id) ON DELETE CASCADE, -- is addressed to
title text NOT NULL,
body text NOT NULL,
published_at timestamptz NOT NULL DEFAULT now(),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX lesson_class_date_idx ON lesson (class_id, lesson_date);
CREATE INDEX attendance_student_idx ON attendance (student_id);
CREATE INDEX grade_student_idx ON grade (student_id);
CREATE INDEX announcement_published_idx ON announcement (published_at DESC);
ERD
erDiagram
app_user {
uuid id PK
text email
text password_hash
text role
timestamptz created_at
timestamptz updated_at
}
school_class {
uuid id PK
text name
text school_year
timestamptz created_at
timestamptz updated_at
}
subject {
uuid id PK
text name
timestamptz created_at
timestamptz updated_at
}
teacher {
uuid id PK
uuid user_id FK
text first_name
text last_name
timestamptz created_at
timestamptz updated_at
}
student {
uuid id PK
uuid user_id FK
uuid class_id FK
text first_name
text last_name
date date_of_birth
timestamptz created_at
timestamptz updated_at
}
lesson {
uuid id PK
uuid class_id FK
uuid subject_id FK
uuid teacher_id FK
date lesson_date
time starts_at
time ends_at
text room
timestamptz created_at
timestamptz updated_at
}
attendance {
uuid id PK
uuid lesson_id FK
uuid student_id FK
text status
text note
timestamptz created_at
timestamptz updated_at
}
assessment {
uuid id PK
uuid class_id FK
uuid subject_id FK
uuid teacher_id FK
text title
date assessment_date
numeric weight
timestamptz created_at
timestamptz updated_at
}
grade {
uuid id PK
uuid assessment_id FK
uuid student_id FK
numeric value
text comment
timestamptz created_at
timestamptz updated_at
}
announcement {
uuid id PK
uuid author_id FK
uuid class_id FK
text title
text body
timestamptz published_at
timestamptz created_at
timestamptz updated_at
}
app_user ||--o| teacher : "is the login of"
app_user |o--o| student : "is the login of"
school_class ||--o{ student : "has as pupils"
school_class ||--o{ lesson : "has"
subject ||--o{ lesson : "is taught in"
teacher ||--o{ lesson : "teaches"
lesson ||--o{ attendance : "records"
student ||--o{ attendance : "is recorded for"
school_class ||--o{ assessment : "writes"
subject ||--o{ assessment : "is examined in"
teacher ||--o{ assessment : "sets"
assessment ||--o{ grade : "is graded in"
student ||--o{ grade : "receives"
app_user ||--o{ announcement : "publishes"
school_class |o--o{ announcement : "is addressed to"
Prisma
model AppUser {
id String @id @default(uuid()) @db.Uuid
email String @unique
passwordHash String @map("password_hash")
role String @default("student")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
teacher Teacher?
student Student?
announcements Announcement[]
@@map("app_user")
}
model SchoolClass {
id String @id @default(uuid()) @db.Uuid
name String
schoolYear String @map("school_year")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
students Student[]
lessons Lesson[]
assessments Assessment[]
announcements Announcement[]
@@unique([name, schoolYear])
@@map("school_class")
}
model Subject {
id String @id @default(uuid()) @db.Uuid
name String @unique
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
lessons Lesson[]
assessments Assessment[]
@@map("subject")
}
model Teacher {
id String @id @default(uuid()) @db.Uuid
userId String @unique @map("user_id") @db.Uuid
firstName String @map("first_name")
lastName String @map("last_name")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
user AppUser @relation(fields: [userId], references: [id], onDelete: Cascade)
lessons Lesson[]
assessments Assessment[]
@@map("teacher")
}
model Student {
id String @id @default(uuid()) @db.Uuid
userId String? @unique @map("user_id") @db.Uuid
classId String @map("class_id") @db.Uuid
firstName String @map("first_name")
lastName String @map("last_name")
dateOfBirth DateTime? @map("date_of_birth") @db.Date
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
user AppUser? @relation(fields: [userId], references: [id], onDelete: SetNull)
schoolClass SchoolClass @relation(fields: [classId], references: [id], onDelete: Restrict)
attendances Attendance[]
grades Grade[]
@@map("student")
}
model Lesson {
id String @id @default(uuid()) @db.Uuid
classId String @map("class_id") @db.Uuid
subjectId String @map("subject_id") @db.Uuid
teacherId String @map("teacher_id") @db.Uuid
lessonDate DateTime @map("lesson_date") @db.Date
startsAt DateTime @map("starts_at") @db.Time
endsAt DateTime @map("ends_at") @db.Time
room String?
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
schoolClass SchoolClass @relation(fields: [classId], references: [id], onDelete: Cascade)
subject Subject @relation(fields: [subjectId], references: [id], onDelete: Restrict)
teacher Teacher @relation(fields: [teacherId], references: [id], onDelete: Restrict)
attendances Attendance[]
@@index([classId, lessonDate])
@@map("lesson")
}
model Attendance {
id String @id @default(uuid()) @db.Uuid
lessonId String @map("lesson_id") @db.Uuid
studentId String @map("student_id") @db.Uuid
status String @default("present")
note String?
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
lesson Lesson @relation(fields: [lessonId], references: [id], onDelete: Cascade)
student Student @relation(fields: [studentId], references: [id], onDelete: Cascade)
@@unique([lessonId, studentId])
@@index([studentId])
@@map("attendance")
}
model Assessment {
id String @id @default(uuid()) @db.Uuid
classId String @map("class_id") @db.Uuid
subjectId String @map("subject_id") @db.Uuid
teacherId String @map("teacher_id") @db.Uuid
title String
assessmentDate DateTime @map("assessment_date") @db.Date
weight Decimal @default(1.00) @db.Decimal(4, 2)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
schoolClass SchoolClass @relation(fields: [classId], references: [id], onDelete: Cascade)
subject Subject @relation(fields: [subjectId], references: [id], onDelete: Restrict)
teacher Teacher @relation(fields: [teacherId], references: [id], onDelete: Restrict)
grades Grade[]
@@map("assessment")
}
model Grade {
id String @id @default(uuid()) @db.Uuid
assessmentId String @map("assessment_id") @db.Uuid
studentId String @map("student_id") @db.Uuid
value Decimal @db.Decimal(3, 1)
comment String?
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
assessment Assessment @relation(fields: [assessmentId], references: [id], onDelete: Cascade)
student Student @relation(fields: [studentId], references: [id], onDelete: Cascade)
@@unique([assessmentId, studentId])
@@index([studentId])
@@map("grade")
}
model Announcement {
id String @id @default(uuid()) @db.Uuid
authorId String @map("author_id") @db.Uuid
classId String? @map("class_id") @db.Uuid
title String
body String
publishedAt DateTime @default(now()) @map("published_at") @db.Timestamptz
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
author AppUser @relation(fields: [authorId], references: [id], onDelete: Restrict)
schoolClass SchoolClass? @relation(fields: [classId], references: [id], onDelete: Cascade)
@@index([publishedAt(sort: Desc)])
@@map("announcement")
}