Table Design
https://github.com/yoosuf/Messenger
Constraints
Key words
- NOT NULL
- Ensures that a column cannot have a NULL value
- UNIQUE
- Ensures that all values in a column are different
PRIMARY KEY
- A combination of a NOT NULL and UNIQUE. Uniquely identifies each row in a table
- A table can have only one primary key, which may consist of single or multiple fields
FOREIGN KEY
- Uniquely identifies a row/record in another table
- A FOREIGN KEY is a field (or collection of fields) in one table that refers to the PRIMARY KEY in another table.
- The table containing the foreign key is called the child table, and the table containing the candidate key is called the referenced or parent table.
- CHECK
- Ensures that all values in a column satisfies a specific condition
- DEFAULT
- Sets a default value for a column when no value is specified
- INDEX
- Used to create and retrieve data from the database very quickly
Examples
|
|
Star Schema
- Denormalized dimensions
- Fact table surrounded by dimension tables
- may have data integrity and storage issues
Star design vs. snowflake design
Star
- denormalized
- redundancy (more storage space)
- simple joins
- faster performance
- may run into data integrity issues
- safe to use when facts constitute 80% or more
Snowflake
- normalized
- not redundant (low storage space)
- complex and multiple joins
- can be relatively slow because of more joins
- may run into performance issues
- use when dimensions are huge in size
Examples
Share Economy Data Model
(e.g. Uber, airbnb)
Airbnb
The data model consists of five subject areas:
- Countries & cities
- Users & roles
- Services & documents
- Requests
- Provided services
OLTP:
OLAP
Star model:
- user table
- property table
- service table
- activity table
|
|