Design a relational schema from your entities and query patterns, with keys, constraints, indexes justified by queries, and a migration plan.
You are a database architect. Design a schema for the application below. Derive tables from the entities and constraints, and derive indexes from the listed queries. Explain every deliberate deviation from normal form. ## Inputs Application and its data needs: [APPLICATION_CONTEXT] Entities, relationships and business rules (uniqueness, allowed values, lifecycle): [ENTITIES_AND_RULES] Database system and version: [DATABASE] Most frequent and most important queries: [QUERY_PATTERNS] Expected size and growth per table, if known: [VOLUME] Existing schema, if migrating: [EXISTING_SCHEMA] ## Instructions 1. Normalize to remove update anomalies (typically third normal form). Denormalize only for a named query, and state how the duplicated data stays consistent. 2. Choose data types deliberately: exact numerics for money, time zone-aware timestamps where events happen across zones, text length limits only where the business requires them. 3. Define primary keys, foreign keys with ON DELETE behaviour, NOT NULL, UNIQUE and CHECK constraints. Enforce business rules in the database when the database can express them. 4. Model one-to-one, one-to-many and many-to-many (junction table) relationships explicitly. 5. Use soft delete only if the rules require restoring or auditing deleted rows; explain how uniqueness works with soft-deleted rows. 6. Propose indexes only for [QUERY_PATTERNS] and foreign keys used in joins; consider partitioning only when [VOLUME] justifies it. ## Return 1. Text entity-relationship diagram, for example `[customers] 1----* [orders]`. 2. SQL DDL for [DATABASE]. 3. Index statements, each mapped to the query it serves. 4. Example queries for the listed patterns. 5. Migration strategy if [EXISTING_SCHEMA] was supplied. 6. Assumptions and open questions. ## Acceptance checks - Every business rule is enforced by a constraint or listed as application-enforced with the reason. - Every index maps to a listed query or join. - The DDL is valid for the stated database and version; features that require an extension say so. - No performance claims; index benefit is framed as something to confirm with the database's EXPLAIN output.
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
Application: appointment booking for a physiotherapy clinic Entities and rules: practitioners; patients (email unique); services with duration and price; appointments linking one patient, one practitioner and one service; a practitioner cannot have overlapping appointments; cancelled appointments are kept for reporting Database: PostgreSQL 17 Queries: a practitioner's appointments for a date range; a patient's upcoming appointments; daily revenue by service Volume: about 200,000 appointments per year
DDL with appointments(starts_at timestamptz, ends_at timestamptz, status with a CHECK of allowed values, price stored as numeric copied from the service at booking time and explained as intentional denormalization); a CHECK that ends_at > starts_at; an exclusion constraint using tstzrange(starts_at, ends_at) with && on practitioner_id that ignores cancelled rows, noting the btree_gist extension requirement; indexes on (practitioner_id, starts_at) and (patient_id, starts_at); no partitioning at roughly 200,000 rows per year; and a revenue query grouped by service and day. Illustrative expectation, not a recorded model run.
Ask questions or share concrete improvements.
No comments yet.
Sign in to join the discussion.
Guides for adapting and checking prompts:
Author-supplied tool and model tags; confirm behavior with your own examples.
IDEs
Models