A collection of fragments of understanding in the pursuit of deeper questions.
Relational Database A relational database is an entity consisting of logical units known as tables (also called relations). A relational database is based on the relational model of data proposed by E. F. Codd in 1970. Data in a relational database systems can be accessed using SQL (Structured Query Language). An RDBMS (Relational Database Management System) is a software system used to run and maintain relational databases.
RDBMS Key features of RDBMS:
Tables A table is defined by the following metadata:
Fields & Records Columns or fields
Records or tuples
Data Types
String
Date
Numbers
Floating point
Fixed Decimals
Primary Key
The primary key constraint guarantees the record uniqueness. When we put the primary key constraint on a column, its values must be unique. If we try and insert a duplicate value the RDBMS rejects it and returns an error:
Creating a table using SQL We use the CREATE TABLE command, followed by the columns list (name, data type and NULL constraint).
Foreign Key As the primary key uniquely represents a record in a table, we can use the primary key value to make a reference to that record. For example: let's say that we have the sales table and we want to keep track of each safe, we could create the table like this:
We don't repeat all the data for each customer, but we just use the customer_id field (the primary key) to reference a customer. But what happens if we put a non existing customer_id into the sales table? We loose consistency, because we would have a reference to a customer that doesn't exist!! To avoid this situation we can create a foreign key constraint on the sales table. The foreign key links the customer_id column of the sales table to the customer_id column in the Customers table and doesn't allow the insertion of a non existing customer_id.
Data Integrity The primary key constraint guarantees the entity (table) integrity, which means no duplicate rows (or no duplicate keys). The foreign key protects the database against the violation of the referential integrity. In fact a field with a foreign key constraint:
The data type of a column and the NULL constraint specify and protect the domain integrity: all the values of the same column belong to the same domain. In addition the user can define other constraint, called CHECK CONSTRAINT (for example: quantity > 0).
Full SQL Example
Dropping tables We can delete a table using the DROP TABLE command:
Other Objects A Database contains many object types along with the tables:
Without indexes the RDBMS must scan the entire table even if we request a single record!
Database Normalization Database Normalization is a technique of organizing the data in the database. Normalization is a systematic approach of decomposing tables to eliminate data redundancy and possible anomalies in insert, update and delete operations. The normalization rules are divided into the following normal forms:
Denormalized Tables Customer Table
Employee Table
Problems In the Customer table we have:
In this case it's hard to use the address data (for example, getting the customers that live in NY). In the Employee table we have:
In this case there could be update anomalies: for example if the department phone changes we must update it for all the employees that belong to that department. If an employee goes to another department, we'd have to update all the dep. Columns (code, name and phone). It's really easy to make a mistake...
First Normal Form A table should only have single (atomic) valued columns. Values stored in a column should be of the same domain (same data type). All the columns in a table should have unique names. Records should be unique. A primary key constraint should be set in order to guarantee the record uniqueness.
Third Normal Form The table shouldn't have Transitive Dependencies. We have Transitive Dependency when a non-primary key attribute depends on other non-primary key attributes rather than depending upon the primary key.
OLTP On Line Transaction Processing. Transaction Oriented databases. They are fully normalized:
OLTP Example
Data Warehouse The data warehouse is an analysis oriented database. The purpose of DWH is to provide a simple data model for the analyst. The DWH is made of two types of tables:
Dimensions Dimensions contains all the attributes of a business entity. The dimension key in the DWH is called surrogate key. It's a progressive number created by the ETL process. The original key (coming from the operational source) is called business key and it's stored in the dimension table. Examples of dimensions (or business entities):
Fact Tables Fact tables contain:
More on DWH The DWH is fed with data coming from many operational databases. The process that feeds the DWH is called ETL (Extract Transform and Load):
Data Warehouse Example
SQL Language Intro SQL (Structured Query Language) allows us to interact with databases. Technically SQL is:
Why SQL for big data? Most of the Big Data and NoSQL tools have an SQL interface:
SQL consists of many types of statements, that can be grouped into 4 sublanguages:
Some definitions:
Some SQL Dialects
