You are on page 1of 16

Modeling Data Relationships

Building Data Relations

Data types are rarely isolated

● A rich data model establishes relations between data

● Relations can be as important as the data itself


How is Data Referenceable

Primary Key
Uniquely identifies a record in a table

In data modeling, primary keys can be either:


● Simple (single column)
○ no two rows have the same value in this column
● Composite (multiple columns)
○ no two rows have the same combination of values for these columns
How to Reference Data

Foreign Key
Associates a record from a table with a record in a different table
● Value should match that of an existing primary key

NULL value
Represent missing information
● Columns can allow/disallow NULL value
● Primary key column(s) cannot have NULL values
Referencing Data in OutSystems

To create a relation to it, an entity must have an Identifier (primary key)

● Long Integer Id attribute automatically


generated by the platform by default
● Is Mandatory
● Is AutoNumber, by default
● Possible types: Text, Integer/Long Integer, or
any other Entity Identifier

Only simple primary keys exist in OutSystems. NO composite keys!


Referencing Data in OutSystems (cont.)

Entities are referenced by their identifier


● Create a reference attribute (foreign key) of
type Entity Identifier
● Is Mandatory referenced attributes are
checked by the Database
● Use EntityNameId as an attribute name to hint
the platform (e.g. attribute UserId will be
automatically assigned type User Identifier)

NullIdentifier() is the equivalent NULL value for


reference attributes
Common Relational Patterns: 1-to-1

Extension Entity
The extension entity shares its
identifier with the base entity

Biography extends Author:


● Biography has an identifier of type
Author Identifier
● Is AutoNumber will be automatically
set to No
● Biography Identifier must be explicitly
assigned with the Author identifier
before creation of record
Common Relational Patterns: 1-to-1 (cont.)

It’s called 1-to-1 because


● Each single Biography belongs to one
single Author
● Each single Author has, at most, one
single Biography

In theory, these two entities could be


merged into a single entity

Practical issues (performance, backups,


etc.) give preference to having them split
Common Relational Patterns: 1-to-many

Master-Detail
The Detail entity references the Master
entity

A Publisher publishes multiple Books


● Book entity with reference attribute to
Publisher entity

Some Books are self-published, so they


don’t have a Publisher
● Reference attribute may or may not be
mandatory
Common Relational Patterns: 1-to-many (cont.)

It’s called 1-to-many because


● Each single Publisher may have
many Books
● Many Books may belong to the
same single Publisher
Common Relational Patterns: many-to-many

Junction Entity
An intermediate entity establishes
relationship between two entities

Since a Book has many Authors, and an


Author can write multiple Books, a
BookAuthor junction entity is required
● Reference attribute AuthorId of type
Author Identifier
● Reference attribute BookId of type
Book Identifier
● Unique Index with both BookId and
AuthorId (recommended)
Common Relational Patterns: many-to-many (cont.)

It’s called many-to-many because


● Each single Author may have
written many Books
● Each single Book may be authored
by many Authors
Referential Integrity

How to guarantee it?


● Specify Delete Rule per reference attribute

Applied when deleting referenced entity instance


● Protect - do not allow deletion of referenced
entity instances (default)
● Delete - cascade delete of referenced entity
instances into instances of this entity
● Ignore - do not guarantee referential integrity
(very uncommonly used e.g. keep row for
logging/auditing purposes)
Entity Diagram

Visual representation of the Data Model


● Designed by the developer
● Partial view over the Data Model
● Represents
○ Entities
○ Relationship navigation
○ Relationship delete rules:
■ Protect
■ Delete
■ Ignore
Indexes

Speed up data retrieval over certain


attributes
● At the cost of additional writes/storage
space
● Reference attributes have automatically
created indexes
● Custom index can be applied to one or
more attributes

Avoid duplicates
● Define the Index as Unique
Modeling Data Relationships

You might also like