{"id":32778672,"url":"https://github.com/msmrexe/postgres-course-registration-system","last_synced_at":"2026-05-18T05:41:42.193Z","repository":{"id":320639368,"uuid":"1082869093","full_name":"msmrexe/postgres-course-registration-system","owner":"msmrexe","description":"A comprehensive university course registration system simulated in PostgreSQL. Features prerequisite checking, time conflict detection, and section capacity management using advanced PL/pgSQL functions and triggers.","archived":false,"fork":false,"pushed_at":"2025-10-24T23:02:16.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-11-04T15:03:47.455Z","etag":null,"topics":["course-project","course-registration-system","databases","postgresql","sql","university-project"],"latest_commit_sha":null,"homepage":"","language":"PLpgSQL","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/msmrexe.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-10-24T22:39:52.000Z","updated_at":"2025-10-24T23:04:45.000Z","dependencies_parsed_at":"2025-10-25T00:27:37.218Z","dependency_job_id":"35be49de-a292-4e5a-8d55-f43db95a30cb","html_url":"https://github.com/msmrexe/postgres-course-registration-system","commit_stats":null,"previous_names":["msmrexe/postgres-course-registration-system"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/msmrexe/postgres-course-registration-system","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msmrexe%2Fpostgres-course-registration-system","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msmrexe%2Fpostgres-course-registration-system/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msmrexe%2Fpostgres-course-registration-system/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msmrexe%2Fpostgres-course-registration-system/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/msmrexe","download_url":"https://codeload.github.com/msmrexe/postgres-course-registration-system/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/msmrexe%2Fpostgres-course-registration-system/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33166640,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-17T22:39:12.733Z","status":"online","status_checked_at":"2026-05-18T02:00:06.436Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["course-project","course-registration-system","databases","postgresql","sql","university-project"],"created_at":"2025-11-04T15:01:20.087Z","updated_at":"2026-05-18T05:41:42.165Z","avatar_url":"https://github.com/msmrexe.png","language":"PLpgSQL","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Smart Course Registration System (PostgreSQL)\n\nThis project simulates a smart university course registration system within a PostgreSQL database. It uses advanced PL/pgSQL functions, views, and recursive queries to enforce complex business rules, such as prerequisite checking, time conflict detection, and section capacity management.\n\nThis project was developed from a series of \"Mathematical Databases\" course homework assignments to create a single, cohesive application.\n\n## Features\n\n* **Recursive Prerequisite Enforcement:** Automatically checks if a student has successfully passed all direct and indirect prerequisites for a course before registration.\n* **Time Conflict Detection:** Prevents a student from registering for two sections that share the same time slot in a given semester.\n* **Section Capacity Management:** Enforces classroom capacity, preventing registration in sections that are already full.\n* **Atomic Registration Function:** A main `udf_attempt_registration(...)` function orchestrates all checks. If any check fails, the registration is rolled back, and an informative error message is returned.\n* **Course Drop Functionality:** A `udf_drop_course(...)` function that safely removes a student from a course and updates their total credits.\n* **Helper Views:** Includes useful views like `v_available_sections` (showing sections with open seats) and `v_student_schedule` (showing a student's schedule with times).\n\n## Database Concepts Showcased\n\n* **Advanced PL/pgSQL:** Writing complex stored functions and procedures with parameters, conditional logic (IF/ELSE), and robust error handling (`RAISE EXCEPTION`, `BEGIN/EXCEPTION` blocks).\n* **Recursive Queries:** Using `WITH RECURSIVE` to traverse the prerequisite graph.\n* **Data Integrity:** Enforcing complex business rules at the database level, ensuring data remains consistent.\n* **Complex SQL:** Writing queries with multi-table joins, subqueries, `INTERSECT`, `EXCEPT`, and aggregation functions.\n* **Database Views:** Using `CREATE VIEW` to abstract complex queries and simplify logic for other functions.\n* **Transactions:** Using `BEGIN`, `COMMIT`, and `ROLLBACK` to ensure atomic operations.\n\n---\n\n## How It Works\n\nThe system is built on two pillars: the database schema (the tables) and the business logic (the functions and views).\n\n### 1. Database Schema Overview\n\nThe logic primarily revolves around these key tables:\n\n* **`student`**: Stores student information (ID, name, department, total credits).\n* **`course`**: Stores course definitions, including `course_id`, `title`, and `credits`.\n* **`prereq`**: A simple mapping table with two columns: `course_id` and `prereq_id`. This table defines the prerequisite graph (e.g., 'CS-347' requires 'CS-101').\n* **`section`**: Represents a specific offering of a `course` in a given `semester` and `year`. It links to a `classroom` (for capacity) and a `time_slot_id` (for scheduling).\n* **`time_slot`**: Defines the actual days (M, T, W, R, F) and times for a given `time_slot_id`.\n* **`classroom`**: Stores building/room information and, most importantly, the `capacity` of that room.\n* **`takes`**: This is the central transaction table. An entry here signifies that a `student` has taken or is currently taking a `section`. It stores the student's `grade`. A `NULL` grade indicates the course is in progress.\n\n### 2. Core Logic and Functions\n\nThe logic is implemented in PL/pgSQL functions, which act as a smart API for the database.\n\n#### Helper Views and Functions (`02_views_and_helpers.sql`)\n\nThese are created first to simplify the main logic:\n\n* **`udf_convert_grade_to_points(grade)`**: A helper function that converts letter grades ('A', 'A-', 'F', etc.) into numeric grade points (4.0, 3.7, 0.0). This is crucial for defining what a \"passing\" grade is (any grade point \u003e 0.0).\n* **`v_student_grade_points`**: A view built on `takes` that automatically includes the numeric grade points for every course a student has taken.\n* **`v_section_enrollment`**: A simple view that `COUNT`s the number of students enrolled in every section.\n* **`v_available_sections`**: A powerful view that joins `section`, `classroom`, and `v_section_enrollment` to show a list of all sections that still have `seats_available \u003e 0`.\n\n#### Main Registration Logic (`03_registration_logic.sql`)\n\nThese functions perform the actual registration checks:\n\n* **`udf_check_prerequisites(student_id, course_id)`**:\n    1.  Uses a `WITH RECURSIVE` query to find *all* direct and indirect prerequisites for the target `course_id` by traversing the `prereq` table.\n    2.  It then finds all courses the student has *passed* (grade point \u003e 0.0) by querying the `v_student_grade_points` view.\n    3.  It compares the two lists. If the list of prerequisites is not fully contained within the list of passed courses, the function returns `FALSE`.\n\n* **`udf_check_time_conflict(student_id, ...)`**:\n    1.  Finds the `time_slot_id` for the new section the student wants to register for.\n    2.  Finds all `time_slot_id`s for courses the student is *already* registered for in the *same semester and year*.\n    3.  Uses `INTERSECT` to see if there is any overlap between the new time slot and the existing ones. If there is, it returns `FALSE`.\n\n* **`udf_check_capacity(...)`**:\n    * This function is now very simple: it just checks if the target section `EXISTS` in the `v_available_sections` view. If it's not in that view, it's full, and the function returns `FALSE`.\n\n* **`udf_attempt_registration(student_id, ...)`**:\n    * This is the main \"orchestrator\" function. It wraps all checks into a single transaction.\n    1.  Checks if the student is already registered for the course in the same semester.\n    2.  Calls `udf_check_prerequisites`. If it fails, returns an error.\n    3.  Calls `udf_check_time_conflict`. If it fails, returns an error.\n    4.  Calls `udf_check_capacity`. If it fails, returns an error.\n    5.  If all checks pass, it `INSERT`s a new record into the `takes` table (with a `NULL` grade) and `UPDATE`s the `student.tot_cred` by adding the course's `credits`.\n    6.  Returns a success message.\n\n* **`udf_drop_course(student_id, ...)`**:\n    * Allows a student to drop a course. It `DELETE`s the record from `takes` **only if** the `grade IS NULL` (meaning the course is in-progress).\n    * It then subtracts the course `credits` from the `student.tot_cred` table.\n\n---\n\n## Project Structure\n\n```\npostgres-course-registration-system/\n├── .gitignore                      # Ignores system and credential files\n├── LICENSE                         # MIT license file\n├── README.md                       # This documentation\n├── demo.sql                        # Example script showing how to use the system\n└── src/\n    ├── 00_schema.sql               # Main DDL script to create all tables\n    ├── 01_data.sql                 # Script to insert sample data\n    ├── 02_views_and_helpers.sql    # Creates helper views and functions\n    └── 03_registration_logic.sql   # Creates the core functions (checks, register, drop)\n```\n\n## How to Use\n\n1.  **Setup Database:**\n    Create a new PostgreSQL database.\n    ```bash\n    createdb university_db\n    ```\n\n2.  **Connect to Database:**\n    Use `psql` or any SQL client to connect to your new database.\n    ```bash\n    psql university_db\n    ```\n\n3.  **Run SQL Scripts (in order):**\n    Execute the SQL scripts in the following order to build the database, load data, and create the functions.\n\n    ```sql\n    -- 1. Create the schema\n    \\i src/00_schema.sql\n    \n    -- 2. Load the sample data\n    \\i src/01_data.sql\n    \n    -- 3. Create helper views\n    \\i src/02_views_and_helpers.sql\n    \n    -- 4. Create the core registration logic\n    \\i src/03_registration_logic.sql\n    ```\n\n4.  **Test the System:**\n    Run the `demo.sql` script to see the system in action. This script is wrapped in a `BEGIN...ROLLBACK` block, so it will not make permanent changes to your data.\n\n    ```sql\n    \\i demo.sql\n    ```\n    You will see output for each test case, for example:\n    ```\n                    test_1_result\n    ---------------------------------------------\n     Registration successful for CS-315.\n    (1 row)\n    \n                    test_2_result\n    --------------------------------------------------\n     Registration failed: Prerequisite check failed.\n    (1 row)\n    ```\n\n## Example Function Call\n\nTo manually register a student, you can call the main function directly:\n\n```sql\nSELECT udf_attempt_registration(\n    p_student_id := '12345',       -- Student ID\n    p_course_id := 'CS-315',       -- Course ID\n    p_sec_id := '1',               -- Section ID\n    p_semester := 'Spring',        -- Semester\n    p_year := 2018                 -- Year\n);\n```\n\n---\n\n## Author\n\nFeel free to connect or reach out if you have any questions!\n\n* **Maryam Rezaee**\n* **GitHub:** [@msmrexe](https://github.com/msmrexe)\n* **Email:** [ms.maryamrezaee@gmail.com](mailto:ms.maryamrezaee@gmail.com)\n\n---\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for full details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmsmrexe%2Fpostgres-course-registration-system","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmsmrexe%2Fpostgres-course-registration-system","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmsmrexe%2Fpostgres-course-registration-system/lists"}