Changes between Initial Version and Version 1 of P7


Ignore:
Timestamp:
05/16/26 11:16:45 (11 days ago)
Author:
216009
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • P7

    v1 v1  
     1= Advanced Database Development =
     2
     3== Student Enrollment Validation ==
     4=== Data requirements description ===
     5During testing, I realized there was a risk of a student being enrolled in the same subject multiple times by mistake, or enrolling in a subject they already passed. Since our Java code handles some validation, I wanted to move this logic directly into the database to make it "bulletproof." This constraint ensures that a student can only have one active record for any given subject in the student_subject table.
     6
     7=== Implementation: ===
     8
     9==== Triggers and Functions ====
     10I created a trigger that fires every time a new enrollment is attempted. It checks the existing records and blocks the insert if the student is already active in that course.
     11
     12{{{
     13#!sql
     14CREATE OR REPLACE FUNCTION validate_new_enrollment()
     15RETURNS TRIGGER AS $$
     16BEGIN
     17-- Check if student already has a record for this subject that isn't 'Failed' or 'Cancelled'
     18IF EXISTS (
     19SELECT 1 FROM student_subject
     20WHERE stud_id = NEW.stud_id
     21AND subj_id = NEW.subj_id
     22AND status IN ('Enrolled', 'Completed')
     23) THEN
     24RAISE EXCEPTION 'This student is already enrolled in or has finished this course.';
     25END IF;
     26RETURN NEW;
     27END;
     28$$ LANGUAGE plpgsql;
     29
     30CREATE TRIGGER check_before_enrollment
     31BEFORE INSERT ON student_subject
     32FOR EACH ROW EXECUTE FUNCTION validate_new_enrollment();
     33}}}
     34
     35== Administrative Performance Overview ==
     36=== Data requirements description ===
     37I needed a way to quickly see how students are doing across different faculties without writing huge JOIN queries in every DAO method. I created a View that acts as a "Summary Dashboard." It pulls data from the student, faculty, enrollment, and advice tables to show a real-time count of passed subjects and total advice sessions.
     38
     39=== Implementation: ===
     40
     41==== Views ====
     42This view doesn't store new data; it just provides a virtual window into the relationships I already built in previous phases.
     43
     44{{{
     45#!sql
     46CREATE OR REPLACE VIEW student_progress_summary AS
     47SELECT
     48s.id,
     49s.name || ' ' || s.surname AS student_name,
     50f.name AS faculty,
     51COUNT(ss.subj_id) FILTER (WHERE ss.status = 'Completed') AS total_passed,
     52COUNT(a.id) AS advice_count
     53FROM student s
     54JOIN faculty f ON s.facultyid = f.id
     55LEFT JOIN student_subject ss ON s.id = ss.stud_id
     56LEFT JOIN advice a ON s.id = a.student_id
     57GROUP BY s.id, s.name, s.surname, f.name;
     58}}}
     59
     60== Controlling Advice Status Data ==
     61=== Data requirements description ===
     62To prevent typos or invalid status entries in the advice table (like entering "Done" instead of "Completed"), I decided to implement a custom Domain. This keeps the data clean at the database level regardless of what the user types in the front-end forms.
     63
     64=== Implementation: ===
     65
     66==== Custom Domains ====
     67{{{
     68#!sql
     69CREATE DOMAIN valid_advice_status AS TEXT
     70CHECK (VALUE IN ('Requested', 'Scheduled', 'Completed', 'Cancelled'));
     71
     72-- This ensures the 'status' column only accepts these specific words.
     73}}}
     74
     75[[br]]
     76= Advanced Database Development AI Usage =
     77
     78Name of AI service: Gemini 3 Flash
     79
     80URL: https://gemini.google.com/
     81
     82Type of service: Free Tier
     83
     84Final result: I successfully implemented several server-side safety checks (Triggers) and a reporting layer (Views) that protect the database integrity even when the Java application is bypassed.
     85
     86Diagram:
     87
     88Results in details / description:
     89
     90Constraint Automation: I wanted to ensure that my database could handle "business logic" independently. I designed a trigger to prevent duplicate enrollments in student_subject and a workload cap for the subject table.
     91
     92Syntax Consulting: Since writing PL/pgSQL functions manually is error-prone, I used the AI to help me structure the IF EXISTS and RAISE EXCEPTION blocks correctly for PostgreSQL 18.
     93
     94Data Abstraction: I used the AI to help me write a complex SQL VIEW. This allows me to see a student's full academic standing (passed vs. failed counts) in one simple table without having to write long JOIN statements in my Java DAO classes.
     95
     96Domain Validation: Based on a suggestion from the AI, I implemented a DOMAIN for the advice table status. This is much more efficient than using a simple CHECK constraint across multiple columns.
     97
     98Entire AI usage log:
     99
     100User: I am in Phase P7. I need to add triggers and views to my existing university database (tables: student, subject, advice, student_subject). I want to make sure students can't enroll in the same class twice.
     101
     102AI: Suggested creating a BEFORE INSERT trigger on student_subject. Provided the PL/pgSQL function template using IF EXISTS.
     103
     104User: Can I also limit professors to only 5 subjects?
     105
     106AI: Suggested a second trigger on the subject table that counts existing assignments before allowing a new INSERT. Provided the code using a DECLARE variable for the count.
     107
     108User: I need a view that shows student names, their faculty, and how many advice sessions they've had.
     109
     110AI: Provided a CREATE VIEW script using LEFT JOIN and GROUP BY to ensure students with zero sessions still show up in the report.
     111
     112User: How do I make sure the "status" column in my advice table only allows certain words like 'Scheduled' or 'Completed'?
     113
     114AI: Recommended using a CREATE DOMAIN with a CHECK constraint to create a custom data type for those specific status values.