|
| 1 | +# 1. Relational Databases |
| 2 | + |
| 3 | +A **relational database** is a type of **database** that stores and provides access to data points that are _related to one another_. **Relational databases** are based on the _relational model_, which is a way of representing data in **tables**. Each row in the **table** is a record with a unique ID called **key**. The columns of the **table** hold attributes of the data, and each record usually has a value of each attribute, making it easy to establish relationships among data points. |
| 4 | + |
| 5 | +**SQL** or **Structured Query Language** is used to write and query data in the **database**. **SQL** allows for four types of commands: _create_, _read_, _update_, and _delete_, also known as _CRUD_. |
| 6 | + |
| 7 | +Although **SQL** is the standard _ANSI_/_ISO_, there are different versions of the language. Most of the **SQL** **database** programs have their own proprietary extensions in addition to the SQL standard. |
| 8 | + |
| 9 | +A **relational database management system** or **RDBMS** is a program used to create, update and manage such **databases**. |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +## 1.1. Database Schema |
| 14 | + |
| 15 | +The **database schema** is the skeleton structure of the **database** described in a _formal language_ supported by the **database management system**. The term _schema_ refers to the organization of data as a blueprint of how the database is constructed (divided into database tables). |
| 16 | + |
| 17 | +--- |
| 18 | + |
| 19 | +## 1.2. Relational Model |
| 20 | + |
| 21 | +The **relational database model** allows any table to be related to another table using a common attribute. Instead of using hierarchical structures to organize data, the data is stored, accessed and related in tables without reorganizing the tables that contain them. |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +### 1.2.1. Types of Relational Models |
| 26 | + |
| 27 | +There are three types of relational models in **DBMS**. |
| 28 | + |
| 29 | +1. **One-to-Many Model** |
| 30 | + |
| 31 | +In this type of relationship, one entity is related to multiple other entities. The key feature of the model is that the relationship between two entities is not reciprocal. For example, a customer can have multiple orders on a website, but that order cannot have multiple customers. |
| 32 | + |
| 33 | +2. **One-to-One Model** |
| 34 | + |
| 35 | +The **one-to-one model** states that there is a direct and unique relationship between two specific objects. In other words, each object can only be related to one other object and vice versa. This type of relationship is often used to represent relations between physical things, such as people and their driver's license number. |
| 36 | + |
| 37 | +3. **Many-to-Many Model** |
| 38 | + |
| 39 | +In a **many-to-many model**, each record in the primary table can be linked to multiple records in the foreign table and vice versa. This type of relationship is less common. One such example migh be between students and courses. A student can take multiple courses and a course can have multiple students enrolled. |
| 40 | + |
| 41 | +--- |
| 42 | + |
| 43 | +## 1.3. Primary Key & Foreign Key |
| 44 | + |
| 45 | +The **primary key** constraint uniquely identifies each record in a table. **Primary keys** must be _unique_ and cannot contain _null values_. A table can only have one **primary key** and the **key** can consist of single or multiple columns (fields). |
| 46 | + |
| 47 | +The **foreign key** constraint is used to prevent actions that would destroy links between tables, so it's a field or a collection of fields in one table, that refers to the **primary key** in another table. |
| 48 | + |
| 49 | +The table with the **foreign key** is called the _child_ table, and the table with the **primary key** is the _referenced_ or _parent_ table. |
| 50 | + |
| 51 | +--- |
| 52 | + |
| 53 | +## 1.4. Example |
| 54 | + |
| 55 | +### **Employees Table** |
| 56 | + |
| 57 | +| PersonID | LastName | FirstName | Age | |
| 58 | +|----------|----------|-----------|-----| |
| 59 | +| 1 | Halpert | Jim | 28 | |
| 60 | +| 2 | Scott | Michael | 42 | |
| 61 | +| 3 | Beesly | Pam | 26 | |
| 62 | +| 4 | Schrute | Dwight | 34 | |
| 63 | +| 5 | Malone | Kevin | 39 | |
| 64 | + |
| 65 | +### **Sales Table** |
| 66 | + |
| 67 | +| SaleID | SaleNumber | PersonID | |
| 68 | +|--------|------------|----------| |
| 69 | +| 1 | 4985 | 1 | |
| 70 | +| 2 | 3455 | 4 | |
| 71 | +| 3 | 2321 | 2 | |
| 72 | +| 4 | 6752 | 1 | |
| 73 | +| 5 | 8795 | 3 | |
| 74 | + |
| 75 | +- the _PersonID_ column in the **Sales Table** points to the _PersonID_ in the **Employees Table**; |
| 76 | +- the _PersonID_ in the **Employees Table** is the **Primary Key** in the **Employees Table**; |
| 77 | +- the _PersonID_ in the **Sales Table** is the **Foreign Key** in the **Sales Table**; |
| 78 | +- the relationship between the two is **one-to-many**, an employee can have multiple sales, but that sale cannot have multiple employees, in this case. |
0 commit comments