English · 中文
A cleaned and reproducible SQL Server portfolio project based on undergraduate coursework in Database Systems (数据库系统概论).
The original coursework covered interactive SQL, relational queries, integrity constraints, triggers, stored procedures, and cursors. This repository reorganizes those exercises into a consistent T-SQL / Microsoft SQL Server implementation and corrects syntax and design issues in the original lab files.
Portfolio note: this is a retrospective refactor of coursework, not a claim that every script existed in this exact form when the course was originally completed.
- Relational schema design with primary keys, foreign keys,
CHECKconstraints, and referential actions - DDL and DML operations
- Single-table, join, nested, aggregate, and relational-division-style queries
- Views and practical indexes
- Multi-row-safe SQL Server triggers
- Automatically maintained course averages, grade distributions, percentages, and weighted averages
- Stored procedures using set-based SQL
- Cursor-based versions retained as educational exercises, with modern alternatives documented
- Verification script with executable consistency checks
database-systems-sql-labs/
├── README.md
├── README.zh-CN.md
├── LICENSE
├── run_all.sql
├── sql/
│ ├── 00_create_database.sql
│ ├── 01_schema.sql
│ ├── 02_seed_data.sql
│ ├── 03_lab1_queries.sql
│ ├── 04_views_indexes.sql
│ ├── 05_triggers.sql
│ ├── 06_stored_procedures.sql
│ ├── 07_cursor_exercises.sql
│ └── 08_demo.sql
├── tests/
│ └── verification.sql
└── docs/
├── coursework-map.md
└── design-notes.md
Two schemas keep the course exercises logically separated:
spj: supplier / part / project exercises from the introductory SQL labAcademic: student / course / enrollment exercises used for triggers and stored procedures
Supplier 1 ───< Supply >─── 1 Part
│
└──────── 1 Project
Student 1 ───< Enrollment >─── 1 Course
│ │
└── StudentWeightedAverage ├── CourseAverage
└── GradeDistribution
- Microsoft SQL Server 2019+ recommended
- SQL Server Management Studio, VS Code with Microsoft SQL Server tooling, or the
sqlcmdcommand-line client
The scripts use SQL Server batch separators (GO) and T-SQL features such as inserted / deleted trigger tables.
From the repository root:
sqlcmd -S localhost -E -i run_all.sqlFor SQL authentication:
sqlcmd -S localhost -U <username> -P '<password>' -i run_all.sqlExecute these files in order:
sql/00_create_database.sqlsql/01_schema.sqlsql/02_seed_data.sqlsql/03_lab1_queries.sqlsql/04_views_indexes.sqlsql/05_triggers.sqlsql/06_stored_procedures.sqlsql/07_cursor_exercises.sqlsql/08_demo.sqltests/verification.sql
The archived lab material mixed SQL dialects in places. For example, MySQL-style FOR EACH ROW / SHOW CREATE DATABASE appeared alongside SQL Server inserted / deleted pseudo-tables and T-SQL variables. There were also naming mismatches and incomplete statements.
This portfolio version therefore:
- standardizes everything on SQL Server / T-SQL;
- replaces invalid cross joins and incomplete view definitions with explicit joins;
- uses consistent object names;
- makes trigger logic set-based and safe for multi-row statements;
- uses declarative
ON DELETE CASCADEfor student-enrollment cleanup instead of a fragile row-by-row delete trigger; - preserves cursor exercises for learning while also providing preferred set-based procedures.
See docs/design-notes.md for details.
The Lab 1 exercises include the classic query “find projects that use every part supplied by supplier S1.” The refactored solution uses a double NOT EXISTS, avoiding the incorrect shortcut of comparing only part counts.
Academic.trg_Enrollment_RebuildMetrics reacts to inserts, updates, and deletes on enrollment records and refreshes only the affected courses and students. It maintains:
- grade-band counts and percentages;
- average grade by course;
- credit-weighted average by student.
The course included cursor practice. sql/07_cursor_exercises.sql keeps working cursor implementations, but the repository also documents why set-based SQL is normally preferred for production workloads.
tests/verification.sql checks that:
- the supply-detail view matches the base supply table row count;
- stored course averages equal direct calculations;
- grade-distribution totals equal enrollment totals;
- grade percentages sum to approximately 100%;
- weighted averages equal direct credit-weighted calculations;
- grade letters are valid and complete.
A successful run prints:
All verification checks passed.
| Original lab | Topic | Portfolio implementation |
|---|---|---|
| Lab 1 | Interactive SQL, DDL/DML, queries, indexes, views | 01–04 |
| Lab 2 | Integrity constraints and triggers | 01, 05 |
| Lab 3 | Stored procedures and cursors | 06, 07 |
More detail: docs/coursework-map.md.
The repository intentionally does not include original submitted reports, teacher-provided slides, exam-review PDFs, student identifiers, or other third-party course materials. Only rewritten code and documentation suitable for a public portfolio are included.
MIT License. See LICENSE.