Model studio
Draw a data model at three levels, change it, and generate the tables for PostgreSQL, Spark SQL with Delta Lake and T-SQL; then run the PostgreSQL version on the estate's data, in your browser.
Exercise: A claim against one policy and one cover
A claim is against one policy and is paid under one of that policy's covers. The covers a policy has are rows of policy_covers. Today the claims handler types the cover's name into the claim.
- Each claim refers to the policy cover it is paid under.
- Each claim is still against one policy.
- The cover is not typed into the claim as text.
- Each cover belongs to one policy.
Logical: every attribute with its type and keys (PK, FK), independent of any database; a question mark marks an optional attribute.
Edit claims
A claim against a policy; the handler types the cover's name.
The generated tables
Checked in CI on a real PostgreSQL 18.3, and run below in PGlite, the same release.
CREATE TABLE policies (
policy_id integer GENERATED BY DEFAULT AS IDENTITY NOT NULL,
policy_number text NOT NULL,
start_date date NOT NULL,
CONSTRAINT pk_policies PRIMARY KEY (policy_id)
);
COMMENT ON TABLE policies IS 'A policy.';
COMMENT ON COLUMN policies.policy_id IS 'Surrogate key';
CREATE TABLE policy_covers (
policy_cover_id integer GENERATED BY DEFAULT AS IDENTITY NOT NULL,
policy_id integer NOT NULL,
cover_code char(3) NOT NULL,
CONSTRAINT pk_policy_covers PRIMARY KEY (policy_cover_id),
CONSTRAINT fk_policy_covers_policy_id FOREIGN KEY (policy_id) REFERENCES policies (policy_id)
);
COMMENT ON TABLE policy_covers IS 'One cover a policy includes, such as collision or theft.';
COMMENT ON COLUMN policy_covers.policy_cover_id IS 'Surrogate key';
COMMENT ON COLUMN policy_covers.policy_id IS 'the policies row';
CREATE TABLE claims (
claim_id integer GENERATED BY DEFAULT AS IDENTITY NOT NULL,
policy_id integer NOT NULL,
loss_date date NOT NULL,
cover_name text,
CONSTRAINT pk_claims PRIMARY KEY (claim_id),
CONSTRAINT fk_claims_policy_id FOREIGN KEY (policy_id) REFERENCES policies (policy_id)
);
COMMENT ON TABLE claims IS 'A claim against a policy; the handler types the cover''s name.';
COMMENT ON COLUMN claims.claim_id IS 'Surrogate key';
COMMENT ON COLUMN claims.policy_id IS 'the policies row';
Run it on the data
PostgreSQL starts in this browser when you press Run.
| Table | PGlite | Seed | Same |
|---|
Used in
- Chapter 5 · Conceptual models: the business in boxes
- Chapter 6 · Logical models
- Chapter 7 · Physical models: one logical model, four physical designs
- Chapter 8 · Canonical models for integration
- Chapter 27 · The medallion architecture 2: design choices
- Chapter 39 · Stage 2: Common meaning
Glossary: Conceptual modelLogical modelPhysical modelPrimary keyForeign key