Robbo CRM
A relational Oracle database for a club that teaches children robotics, programming, 3D modelling and electronics. It covers the whole life of a family: the first call, the trial lesson, weekly groups and attendance, summer camps, prepaid packages, invoices and payments. It is built from the structure of the club's real CRM export and loaded with an anonymised copy of its data.
What the database models
A child enters as a lead when a parent calls or fills in a form. Every call is logged until the family books a trial lesson; after it the child can become a client, join a weekly group and buy a package of lessons. Each lesson records who came, and each attended lesson is charged from the package. Packages and camps are invoiced, and invoices are settled by payments.
28 tables, 45 relationships
Crow's-foot notation: lookup tables and BRANCH on the left, CUSTOMER and LESSON as the hubs, and the tables with the most parents (ATTENDANCE, INVOICE, PAYMENT) on the right. Scroll the diagram sideways, or open the interactive version to highlight one table's relationships.
Tables, columns and constraints
Every column, key and named constraint exactly as created by 01_create_tables.sql, with the number of sample rows and the first rows of the anonymised data.
Normalisation and integrity
The CRM export keeps most data in a few wide tables with lists and free text in single fields. The schema brings it to third normal form, and the rules that a CHECK cannot express are enforced by triggers.
1NF · atomic values
- Comma-separated phone numbers → one row each in
PARENT_PHONE. - Lists of teachers and students inside each lesson →
LESSON_TEACHER,ATTENDANCE. - A free-text call history → one row per contact in
CONTACT_LOG.
2NF · whole-key dependency
- Associative tables keep only what depends on the whole key, e.g.
ATTENDANCE(is_present, absence_reason_id, amount_charged). - Lesson data stays in
LESSON, child data inCUSTOMER.
3NF · no transitive dependencies
- Parent data repeated on every sibling →
PARENT. - Tariff name and lesson count read from
TARIFF, not copied. - Text categories → seven lookup tables referenced by id.
Columns kept on purpose
SUBSCRIPTION.priceandATTENDANCE.amount_chargedfreeze prices at the time of sale. The remaining credit is computed, never stored.LESSON.subject_idis a fact of the lesson: groups rotate modules from week to week.LESSON.branch_idis needed for lessons without a group; a trigger keeps it equal to the group's branch.
Declarative constraints
- Every constraint is named (
pk_,fk_,uk_,ck_), so errors say which rule failed. - Conditional checks: only clients have a study status; an invoice is for a subscription or a camp; a group lesson needs a group.
- A camp invoice references
CAMP_ENROLLMENT(camp_id, customer_id)through a composite foreign key.
Rules enforced by triggers
- No attendance on a cancelled lesson, nor for a child outside the group.
- Subscription, invoice and payment must belong to the same child.
- Room, schedule and branch of a lesson must agree.
- Invoice status follows its payments in both directions.
Six views for the club's daily questions
"Today" is fixed to the export date, 9 November 2026, so every view returns the same result whenever it is run.
Triggers, functions and procedures
Ten stored objects. Each raises a named application error (ORA-200xx) when a business rule is broken.
Twenty business questions in SQL
Each query answers a real question the club asks and demonstrates one SQL technique: joins, grouping, subqueries, set operators, analytic functions and CTEs.
The PL/SQL at work: 07_demo.sql
The demo script follows one new family from enrolment to payment, then tries six operations that the database must refuse.
- Attendance rate of the members of one group, computed by
FN_ATTENDANCE_RATE. - Enrolment:
PRC_ENROLL_STUDENTlocks the group, checks the free seats, branch and age, and turns the lead into a client. - Package and invoice: a 4-lesson package at 520 lei; the invoice is checked by
TRG_INVOICE_CONSISTENCY. - Payment: the invoice becomes
PAID; corrected to 300 lei it goes back toISSUED; a second payment of 220 lei settles it again. - Attendance: validated by
TRG_ATTENDANCE_VALIDATEand priced at 520 / 4 = 130 lei byTRG_ATTENDANCE_CHARGE;FN_SUBSCRIPTION_CREDITshows 390 lei left. - Lesson generation:
PRC_GENERATE_LESSONScreates the January 2027 lessons of a weekly schedule, skipping existing dates. - Six refused operations: wrong age, double enrolment, attendance of a non-member, payment on another family's invoice, camp invoice without enrolment, invalid time.
Output
script07_demo.sqlThe full demonstration script›
Run the project in Oracle
The scripts run in Oracle APEX (SQL Workshop → SQL Scripts → Upload) or any Oracle 19c+ schema. Run them in this order and check each result for errors.
| # | Script | What it does |
|---|
What changed in this revision
Corrections made after a review of the first version, each checked against the data.
Stored balance removed. SUBSCRIPTION.balance was 0 in all 653 rows and only an INSERT trigger maintained it. It is replaced by the frozen price; the credit left is computed by V_SUBSCRIPTION_USAGE and FN_SUBSCRIPTION_CREDIT.
Camp invoices tied to enrolments. The foreign key now references CAMP_ENROLLMENT(camp_id, customer_id), so the database cannot bill a child for a camp they are not enrolled in.
Times validated. 'HH24:MI' format checks on lessons and schedules; schedule date order; a GROUP lesson must have a group. 123 named constraints in total.
Consistency of redundant columns. New TRG_ATTENDANCE_VALIDATE, TRG_LESSON_CONSISTENCY and TRG_INVOICE_CONSISTENCY. Analysis of the data showed that LESSON.subject_id legitimately differs from the group's subject in 267 lessons (module rotation), so it is documented rather than forced.
Invoice status in both directions. TRG_PAYMENT_SETTLES_INVOICE is now a compound trigger that recomputes only the invoices touched, handles UPDATE and DELETE, and rejects a payment on another customer's invoice.
Safer enrolment. PRC_ENROLL_STUDENT locks the group (FOR UPDATE), rejects double enrolments, ended groups and other branches, reports missing ids clearly and looks statuses up by name. PRC_GENERATE_LESSONS takes a start date.
V_STUDENT_ATTENDANCE and FN_ATTENDANCE_RATE count held lessons only; Q1 lists co-teachers with LISTAGG instead of repeating the lesson.
A redundant index removed (39 left); the drop script also removes views, functions and procedures; the ERD shows the new relationship and column.