mirror of
https://github.com/robindhole/fundamentals.git
synced 2025-03-15 16:50:10 +00:00
Finishes integrity section.
This commit is contained in:
parent
bed40913c2
commit
bdcb950fbf
BIN
database/media/duplicate-error.png
Normal file
BIN
database/media/duplicate-error.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 16 KiB |
BIN
database/media/fk-error.png
Normal file
BIN
database/media/fk-error.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 22 KiB |
BIN
database/media/students-table.png
Normal file
BIN
database/media/students-table.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 23 KiB |
BIN
database/media/type-error.png
Normal file
BIN
database/media/type-error.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 38 KiB |
@ -50,7 +50,8 @@ Some issues that we can encounter are
|
||||
* Entering 20 digits as a phone number instead of a 10-digit phone number
|
||||
|
||||
This is where DBMS excels. Data integrity is normally enforced in a database system by a series of integrity constraints or rules.
|
||||
Data integrity (or actually logical integrity) can be divided into four main categories:
|
||||
Data integrity (or actually logical integrity) can be divided into three main categories:
|
||||
|
||||
### Entity integrity
|
||||
Each row to be unique within its table. No two rows can be the same. To achieve this, a **primary key** can be defined.
|
||||
|
||||
@ -58,27 +59,78 @@ The primary key field contains a unique identifier – no two rows can contain t
|
||||
|
||||
> Every table must have a primary key and that the column or columns chosen to be the primary key should be unique and not null
|
||||
|
||||
Let us try it out on our database. Following is the `students` table.
|
||||
|
||||
```sql
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
students;
|
||||
```
|
||||

|
||||
|
||||
Let us now try to insert a duplicate row in our database. Since our PK is `id`, we will have to manually add a duplicate value.
|
||||
|
||||
```sql
|
||||
INSERT INTO STUDENTS (id,name,age,phone,email,address,batch_id)
|
||||
VALUES (3,"John Watson",30,123456789,"i.am@sherlock.ed","221B Baker Street", 3);
|
||||
```
|
||||

|
||||
As you can see, the database does not allow us to insert a duplicate row.
|
||||
|
||||

|
||||
|
||||
### Referential integrity
|
||||
> Referential integrity is a property of data stating that all its references are valid.
|
||||
|
||||
|
||||
When a foreign key value is used it must reference a valid, existing primary key in the parent table.
|
||||
For referential integrity to hold in a relational database, any column in a base table that is declared a foreign key can only contain either **null values or values from a parent table's primary key or a candidate key**.
|
||||
|
||||
> For instance, deleting a record that contains a value referred to by a foreign key in another table would break referential integrity.
|
||||
|
||||
> The referential integrity rule states that any foreign-key value can only be in one of two states. The usual state of affairs is that the foreign-key value refers to a primary key value of some table in the database. The other being NULL which means no relationship exists.
|
||||
|
||||
Let us try it on our students table again. The table is the same as above.
|
||||

|
||||
|
||||
We'll now try to enter a tuple which has a batch ID that does not exist in the batches table.
|
||||
```sql
|
||||
INSERT INTO STUDENTS (id,name,age,phone,email,address,batch_id)
|
||||
VALUES (4,"Zuck",30,123456789,"zuck@givemedata.com","someverse", 4);
|
||||
```
|
||||

|
||||
> **Note**
|
||||
> Additionally, MySQL requires that the referenced columns be indexed for performance reasons. However, the system does not enforce a requirement that the referenced columns be UNIQUE or be declared NOT NULL.
|
||||
|
||||
### Domain integrity
|
||||
Domain integrity concerns the validity of entries for a given column. Selecting the appropriate data type for a column is the first step in maintaining domain integrity. Other steps could include, setting up appropriate constraints and rules to define the data format and/or restricting the range of possible values.
|
||||
> all columns in a relational database must be declared upon a defined domain
|
||||
|
||||
### User-defined integrity
|
||||
Domain integrity concerns the validity of entries for a given column. For instance, if a column is declared to be of type `INTEGER`, then only integers can be entered.
|
||||
|
||||
User-defined integrity allows the user to apply business rules to the database that aren’t covered by any of the other three data integrity types.
|
||||
Another example is if a column has a `NOT NULL` constraint, then it cannot be null.
|
||||
|
||||
> Each attribute in the model should be assigned domain information which includes:
|
||||
> * **Data Type** - Basic data types are integer, decimal, or character. Most data bases support variants of these plus special data types for date and time.
|
||||
> * **Length** - This is the number of digits or characters in the value. For example, a value of 5 digits or 40 characters.
|
||||
> * **Date Format** - The format for date values such as dd/mm/yy or mm/dd/yyyy or yy/mm/dd.
|
||||
> * **Range** - The range specifies the lower and upper boundaries of the values the attribute may legally have.
|
||||
> * **Constraints** - Are special restrictions on allowable values. For example, the LeavingDate for an Employee must always be greater than the HireDate for that Employee.
|
||||
> * **Null support** - Indicates whether the attribute can have null values.
|
||||
> * **Default value** (if any) - The value an attribute instance will have if a value is not entered.
|
||||
|
||||
Let us try to enter a tuple with a `phone` field that is not an integer but a string. Following is our students table and the result of the query.
|
||||
```sql
|
||||
INSERT INTO STUDENTS (id,name,age,phone,email,address,batch_id)
|
||||
VALUES (4,"Zuck",30,123456789,"zuck@givemedata.com","someverse",NULL);
|
||||
```
|
||||

|
||||
|
||||
---
|
||||
|
||||
|
||||
## ER Diagrams
|
||||
---
|
||||
## References
|
||||
* [Data Integrity](https://en.wikipedia.org/wiki/Data_integrity)
|
||||
* [What is data integrity?](https://database.guide/what-is-data-integrity/)
|
||||
## Reading List
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user