Data modeling: what it is, types and a worked example

Data modeling is deciding the entities, attributes, relationships and rules a database holds. A bike rental modeled as tables, then as a star schema.

What is data modeling?

Data modeling is the process of deciding what data a system stores and how it fits together: the entities, their attributes, the relationships between them, and the rules every row has to follow. The result is a data model, the blueprint a database is built from, usually drawn as an ER diagram, a visual representation of those entities and relationships.

A model is a set of decisions. Which things get a table, what identifies each row, which columns may be empty, how many rows on one side can meet a row on the other. Every one of them ends up in the DDL, and SQL Server enforces exactly what was decided.

The example here is a bike rental: customers take bikes from docking stations and return them to any station. Each script below was run on SQL Server, so each rule comes with what the engine does to a row that breaks it.

Conceptual, logical and physical data models

Types of data models come in two lists. One is the level of detail, below. The other is the technique: relational and dimensional, compared further down, and the older hierarchical and network models.

  • A conceptual data model names the entities, their attributes and relationships in the business’s words, with many-to-many left unresolved and no foreign keys: customers rent bikes at stations.
  • A logical data model resolves each many-to-many and adds the keys and foreign keys, still for no particular database; ER diagram to relational schema is that step, rule by rule.
  • A physical data model is the tables of one database engine, with data types, NULL or NOT NULL and the constraints.

This post works at the physical level, because that is the model SQL Server runs, and the only one it can check.

A data modeling example: a bike rental

Start with the requirement in plain words:

Customers sign up with an email address and rent bikes. A rental starts at one docking station and ends at any station, and the price is charged when the bike comes back. Each bike has a serial number, and some are electric. A bike is out on one rental at a time.

Station, bike and customer are entities: each has many instances with facts of their own, so each becomes a table. Docks, serial number, email and name are attributes, facts that describe one of them, and they become columns.

Each table gets a primary key, a column, or a set of columns, that uniquely identifies each row of a table. Email, serial number and station name are unique too, but an email can change, and every row that stored it as a key would have to change with it. So each table gets an identity number as its key, and the natural values stay unique as alternate keys:

CREATE TABLE dbo.Station (
  StationId int           IDENTITY NOT NULL
    CONSTRAINT PK_Station PRIMARY KEY,
  Name      nvarchar(100) NOT NULL
    CONSTRAINT AK_Station_Name UNIQUE,
  Docks     tinyint       NOT NULL
);
CREATE TABLE dbo.Bike (
  BikeId     int         IDENTITY NOT NULL
    CONSTRAINT PK_Bike PRIMARY KEY,
  SerialNo   varchar(20) NOT NULL
    CONSTRAINT AK_Bike_SerialNo UNIQUE,
  IsElectric bit         NOT NULL
);
CREATE TABLE dbo.Customer (
  CustomerId int           IDENTITY NOT NULL
    CONSTRAINT PK_Customer PRIMARY KEY,
  Email      nvarchar(254) NOT NULL
    CONSTRAINT AK_Customer_Email UNIQUE,
  FullName   nvarchar(100) NOT NULL,
  Phone      varchar(20)   NULL
);
dbo.Station with primary key StationId and alternate key Name, dbo.Bike with primary key BikeId and alternate key SerialNo, dbo.Customer with primary key CustomerId and alternate key Email; no relationships.
Three entities, each with its primary key above the line and a natural value kept unique as an alternate key (AK). No lines yet.

Rental is the interesting one. It starts out as a verb between customers and bikes, and it is many-to-many: a customer rents many bikes, and a bike goes to many customers over its life. A many-to-many becomes a table of its own. The pair (CustomerId, BikeId) can’t be its primary key, though, because Ana can rent bike 1 on Monday and again on Friday. A rental is an event with its own facts (when it started, where it ended, what it cost), so it gets its own key, RentalId.

Next come the relationships and their cardinality, the number of rows of one table that can relate to one row of another table. A rental has exactly one customer, one bike and one start station; each of those has zero or more rentals. Each is one-to-many, a foreign key on the rental. The end station is the same parent a second time, so dbo.Rental holds two foreign keys to dbo.Station, each with a name that says its role.

Then the NULLs. A bike that is still out has no end station, no end time and no price yet, so those three columns allow NULL, and everything the rental knows from the start is NOT NULL. A nullable foreign key makes its relationship optional: a rental has zero or one end station.

Last, the types: datetime2(0) for times to the second, decimal(7,2) for money, tinyint for a dock count. SQL Server data types covers how to pick each one.

CREATE TABLE dbo.Rental (
  RentalId       int          IDENTITY NOT NULL
    CONSTRAINT PK_Rental PRIMARY KEY,
  BikeId         int          NOT NULL,
  CustomerId     int          NOT NULL,
  StartStationId int          NOT NULL,
  EndStationId   int          NULL,
  StartedAt      datetime2(0) NOT NULL,
  EndedAt        datetime2(0) NULL,
  Price          decimal(7,2) NULL,
  CONSTRAINT FK_Rental_Bike_BikeId FOREIGN KEY (BikeId)
    REFERENCES dbo.Bike (BikeId),
  CONSTRAINT FK_Rental_Customer_CustomerId
    FOREIGN KEY (CustomerId)
    REFERENCES dbo.Customer (CustomerId),
  CONSTRAINT FK_Rental_Station_StartStationId
    FOREIGN KEY (StartStationId)
    REFERENCES dbo.Station (StationId),
  CONSTRAINT FK_Rental_Station_EndStationId
    FOREIGN KEY (EndStationId)
    REFERENCES dbo.Station (StationId)
);
dbo.Rental with primary key RentalId and the foreign keys BikeId, CustomerId, StartStationId and EndStationId; dashed lines from dbo.Bike, dbo.Customer and dbo.Station to Rental, two from Station, the one for EndStationId with a ring at the Station end.
Four lines into dbo.Rental, two of them from dbo.Station; each role column reads as its role and the parent column it takes, StartStationId.StationId. EndStationId allows NULL, so its line has a ring at the station end.

Drawing these tables step by step is the job of how to draw an ER diagram; here the point is the decisions, and that each one is now a line of DDL.

What SQL Server does with the rules in the model

The rules a data model carries are constraints, and SQL Server checks every one on every insert and update:

  • PRIMARY KEY: no two rows share the value, and none leaves it empty.
  • FOREIGN KEY: the value has to exist in the parent’s key.
  • UNIQUE: no two rows share the value, which keeps an alternate key honest.
  • CHECK: a condition each row has to meet.
  • NOT NULL: the column always has a value.

Two rules in the requirement need more than a column type. A station has at least one dock, and a rental ends after it starts: both are checks. “A bike is out on one rental at a time” is a uniqueness rule on part of the table, the rentals with no end yet. A unique constraint can’t take a filter, but a unique index can:

ALTER TABLE dbo.Station ADD CONSTRAINT CK_Station_Docks
  CHECK (Docks > 0);
ALTER TABLE dbo.Rental ADD CONSTRAINT CK_Rental_EndedAt
  CHECK (EndedAt > StartedAt);
CREATE UNIQUE INDEX IX_Rental_BikeId ON dbo.Rental (BikeId)
  WHERE EndedAt IS NULL;

Now some rows: two stations, two bikes, Ana, and her open rental of bike 1. Then five writes the model forbids:

INSERT INTO dbo.Station (Name, Docks)
VALUES (N'Harbor', 12), (N'Old Mill', 8);
INSERT INTO dbo.Bike (SerialNo, IsElectric)
VALUES ('BK-1001', 0), ('BK-1002', 1);
INSERT INTO dbo.Customer (Email, FullName)
VALUES (N'ana@example.com', N'Ana Ruiz');
INSERT INTO dbo.Rental
  (BikeId, CustomerId, StartStationId, StartedAt)
VALUES (1, 1, 1, '2026-06-01 08:10');
INSERT INTO dbo.Customer (Email, FullName)
VALUES (N'ana@example.com', N'Ana R.');
INSERT INTO dbo.Customer (Email)
VALUES (N'tom@example.com');
INSERT INTO dbo.Rental
  (BikeId, CustomerId, StartStationId, StartedAt)
VALUES (2, 1, 9, '2026-06-01 08:15');
INSERT INTO dbo.Rental
  (BikeId, CustomerId, StartStationId, StartedAt)
VALUES (1, 1, 2, '2026-06-01 08:20');
UPDATE dbo.Rental
SET EndStationId = 2, EndedAt = '2026-06-01 07:55'
WHERE RentalId = 1;
Msg 2627, Level 14, State 1, Line 10
Violation of UNIQUE KEY constraint 'AK_Customer_Email'. Cannot insert duplicate key in object 'dbo.Customer'. The duplicate key value is (ana@example.com).
The statement has been terminated.
Msg 515, Level 16, State 2, Line 12
Cannot insert the value NULL into column 'FullName', table 'BikeRental.dbo.Customer'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Msg 547, Level 16, State 1, Line 14
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Rental_Station_StartStationId". The conflict occurred in database "BikeRental", table "dbo.Station", column 'StationId'.
The statement has been terminated.
Msg 2601, Level 14, State 1, Line 17
Cannot insert duplicate key row in object 'dbo.Rental' with unique index 'IX_Rental_BikeId'. The duplicate key value is (1).
The statement has been terminated.
Msg 547, Level 16, State 1, Line 20
The UPDATE statement conflicted with the CHECK constraint "CK_Rental_EndedAt". The conflict occurred in database "BikeRental", table "dbo.Rental".
The statement has been terminated.

The first four statements go in. A second customer with Ana’s email breaks AK_Customer_Email, Msg 2627. A customer with no name is Msg 515, the NOT NULL on FullName. A rental from station 9, which doesn’t exist, is Msg 547 on the foreign key. Renting bike 1 again while it is out breaks the filtered index, Msg 2601. And ending Ana’s rental at 07:55, before it began, is Msg 547 again, from the check this time.

The open rental passed CK_Rental_EndedAt with no end time at all. A CHECK constraint rejects only what evaluates to FALSE, and a comparison with NULL is UNKNOWN. That is what this model wants. A rule that must hold for NULL as well has to test IS NOT NULL in its own expression.

Relational vs dimensional data modeling

The model so far is a relational data model: each fact is stored once, in the table it describes, the shape database normalization leads to. It suits the system that takes rentals, writing one row at a time with every constraint checking it.

Dimensional data modeling shapes the same facts for reading in bulk. Its terms:

  • A star schema is one fact table with its dimensions around it.
  • A fact table records one business event per row.
  • The grain is what one row of the fact table stands for.
  • Measures are the fact table’s numbers to add up.
  • A dimension is a table that describes the event, joined to the fact table by a foreign key.
  • A snowflake schema splits a dimension into further tables.

Here the grain is one row per finished rental, so the open rentals stay out, and the end station, minutes and price are NOT NULL. This runs in a second database, BikeRentalStar, and replaces all four tables:

CREATE TABLE dbo.Day (
  DayId        int        NOT NULL
    CONSTRAINT PK_Day PRIMARY KEY,
  CalendarDate date       NOT NULL,
  Weekday      varchar(9) NOT NULL,
  MonthName    varchar(9) NOT NULL,
  IsWeekend    bit        NOT NULL
);
CREATE TABLE dbo.Station (
  StationId int           NOT NULL
    CONSTRAINT PK_Station PRIMARY KEY,
  Name      nvarchar(100) NOT NULL
);
CREATE TABLE dbo.Bike (
  BikeId   int         NOT NULL
    CONSTRAINT PK_Bike PRIMARY KEY,
  SerialNo varchar(20) NOT NULL,
  BikeType varchar(8)  NOT NULL
);
CREATE TABLE dbo.Customer (
  CustomerId int           NOT NULL
    CONSTRAINT PK_Customer PRIMARY KEY,
  FullName   nvarchar(100) NOT NULL
);
CREATE TABLE dbo.Rental (
  RentalId       int          NOT NULL
    CONSTRAINT PK_Rental PRIMARY KEY,
  DayId          int          NOT NULL,
  BikeId         int          NOT NULL,
  CustomerId     int          NOT NULL,
  StartStationId int          NOT NULL,
  EndStationId   int          NOT NULL,
  Minutes        smallint     NOT NULL,
  Price          decimal(7,2) NOT NULL,
  CONSTRAINT FK_Rental_Day_DayId FOREIGN KEY (DayId)
    REFERENCES dbo.Day (DayId),
  CONSTRAINT FK_Rental_Bike_BikeId FOREIGN KEY (BikeId)
    REFERENCES dbo.Bike (BikeId),
  CONSTRAINT FK_Rental_Customer_CustomerId
    FOREIGN KEY (CustomerId)
    REFERENCES dbo.Customer (CustomerId),
  CONSTRAINT FK_Rental_Station_StartStationId
    FOREIGN KEY (StartStationId)
    REFERENCES dbo.Station (StationId),
  CONSTRAINT FK_Rental_Station_EndStationId
    FOREIGN KEY (EndStationId)
    REFERENCES dbo.Station (StationId)
);
Star schema: the fact table dbo.Rental with primary key RentalId, the foreign keys DayId, BikeId, CustomerId, StartStationId and EndStationId and the measures Minutes and Price; dashed lines into it from dbo.Day, dbo.Bike, dbo.Customer and, twice, dbo.Station.
The fact table dbo.Rental on the right, a dimension at the other end of each line. dbo.Station is joined twice, once per role.

dbo.Day has one row per date, with the weekday and a weekend flag stored so no report has to compute them. dbo.Bike stores BikeType as a word, since a report groups by it. A question becomes one join per dimension it groups by:

INSERT INTO dbo.Day VALUES
  (20260601, '2026-06-01', 'Monday', 'June', 0),
  (20260606, '2026-06-06', 'Saturday', 'June', 1);
INSERT INTO dbo.Station VALUES (1, N'Harbor'), (2, N'Old Mill');
INSERT INTO dbo.Bike VALUES
  (1, 'BK-1001', 'Classic'), (2, 'BK-1002', 'Electric');
INSERT INTO dbo.Customer VALUES
  (1, N'Ana Ruiz'), (2, N'Tom Berg');
INSERT INTO dbo.Rental VALUES
  (1, 20260601, 1, 1, 1, 2, 25, 3.50),
  (2, 20260606, 2, 2, 2, 1, 40, 6.00),
  (3, 20260606, 2, 1, 1, 1, 20, 3.00);
SELECT d.IsWeekend, b.BikeType,
  COUNT(*) AS Rentals, SUM(r.Price) AS Revenue
FROM dbo.Rental AS r
JOIN dbo.Day AS d ON d.DayId = r.DayId
JOIN dbo.Bike AS b ON b.BikeId = r.BikeId
GROUP BY d.IsWeekend, b.BikeType;

The query returns two groups: one classic rental on a weekday for 3.50, and two electric rentals at the weekend for 9.00.

Use a relational model where the data is written: rows change one at a time, and each fact has one place to change. Use a dimensional model where it is read: it is loaded from the relational database, its dimensions repeat values on purpose, which is denormalization, and the load is the only thing that writes to it.

How ERDsmith helps

SQL Server’s five refusals came from choices made with no rows anywhere: an email made unique, a name made NOT NULL, a station key the rental has to match. Keeping those decisions in a model rather than a picture of one means a later change carries them along:

  • Whether a rental has to have an end station lives in one place, the NULL on EndStationId: the line from dbo.Station shows a ring at the station end because of it, and changes with it.
  • Rename EndedAt to ReturnedAt and the expressions that quote it, the check CK_Rental_EndedAt and the filter of IX_Rental_BikeId, are rewritten with the new name.
  • ON DELETE SET NULL on the start station’s relationship marks dbo.Rental with the error SQL Server raises as Msg 1761, since StartStationId is NOT NULL; on EndStationId it passes.