Sei sulla pagina 1di 15

Constraints are the rules enforced on data columns on table.

These are
used to limit the type of data that can go into a table. This ensures the
accuracy and reliability of the data in the database.

Constraints could be column level or table level. Column level constraints


are applied only to one column, whereas table level constraints are applied
to the whole table.

Following are commonly used constraints available in SQL. These


constraints have already been discussed in SQL - RDBMS Concepts chapter
but its worth to revise them at this point.

 NOT NULL Constraint: Ensures that a column cannot have NULL value.

 DEFAULT Constraint: Provides a default value for a column when none is specified.

 UNIQUE Constraint: Ensures that all values in a column are different.

 PRIMARY Key: Uniquely identified each rows/records in a database table.

 FOREIGN Key: Uniquely identified a rows/records in any another database table.

 CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy
certain conditions.

 INDEX: Use to create and retrieve data from the database very quickly.

Constraints can be specified when a table is created with the CREATE TABLE
statement or you can use ALTER TABLE statement to create constraints
even after the table is created.

Dropping Constraints:
Any constraint that you have defined can be dropped using the ALTER
TABLE command with the DROP CONSTRAINT option.

For example, to drop the primary key constraint in the EMPLOYEES table,
you can use the following command:

ALTER TABLE EMPLOYEES DROP CONSTRAINT EMPLOYEES_PK;


Some implementations may provide shortcuts for dropping certain
constraints. For example, to drop the primary key constraint for a table in
Oracle, you can use the following command:

ALTER TABLE EMPLOYEES DROP PRIMARY KEY;

Some implementations allow you to disable constraints. Instead of


permanently dropping a constraint from the database, you may want to
temporarily disable the constraint and then enable it later.

Integrity Constraints:
Integrity constraints are used to ensure accuracy and consistency of data in
a relational database. Data integrity is handled in a relational database
through the concept of referential integrity.

There are many types of integrity constraints that play a role in referential
integrity (RI). These constraints include Primary Key, Foreign Key, Unique
Constraints and other constraints mentioned above.

Functional dependency in DBMS.: DBMS. The attributes of a


table is said to be dependent on each other when an attribute of a
table uniquely identifies another attribute of the same table.

In relational database theory, a functional dependency is


a constraint between two sets of attributes in a relation from a database.
In other words, functional dependency is a constraint that describes the
relationship between attributes in a relation.

Given a relation R, a set of attributes X in R is said to functionally


determine another set of attributes Y, also in R, (written X → Y) if, and only
if, each X value in R is associated with precisely one Y value in R; R is then
said to satisfy the functional dependency X → Y.

In database theory, a multivalued dependency is a full constraint between


two sets of attributes in a relation. ... The multivalued dependency plays a
role in the 4NF database normalization.

Normalisation or Schema Refinement is a technique of organizing the


data in the database. It is a systematic approach of decomposing tables to
eliminate data redundancy and undesirable characteristics like Insertion,
Update and Deletion Anomalies

Anomalies are problems that can occur in poorly planned, un-normalised databases where all the
data is stored in one table (a flat-file database).

Insertion Anomaly - The nature of a database may be such that it is not possible to add a
required piece of data unless another piece of unavailable data is also added. E.g. A library
database that cannot store the details of a new member until that member has taken out a book.

Deletion Anomaly - A record of data can legitimately be deleted from a database, and the
deletion can result in the deletion of the only instance of other, required data, E.g. Deleting a
book loan from a library member can remove all details of the particular book from the database

such as the author, book title etc.

Modification Anomaly - Incorrect data may have to be changed, which could


involve many records having to be changed, leading to the possibility of some
changes being made incorrectly.

An entailment is a deduction or implication, that is, something that follows


logically from or is implied by something else. In logic, an entailment is the
relationship between sentences whereby one sentence will be true if all the
others are also true.
Symbol Symbol Name Meaning / definition
A⊂B proper subset / strict subset subset has fewer elements than the set
A⊄B not subset left set not a subset of right set
set A has more elements or equal to the
A⊇B superset
set B
proper superset / strict
A⊃B set A has more elements than set B
superset

subset has fewer


A⊆B subset elements or equal to the {9,14,28} ⊆ {9,14,28
set

set A has more


A⊇B superset elements or equal to the {9,14,28} ⊇ {9,14,28}
set B

Normalization in DBMS: 1NF, 2NF, 3NF and BCNF in Database.


... Normalizationis a process of organizing the data in database to avoid
data redundancy, insertion anomaly, update anomaly & deletion anomaly

Database Normal Forms


Now let's move in to 1st Normal Forms

1NF (First Normal Form) Rules


 Each table cell should contain single value.
 Each record needs to be unique.
The above table in 1NF-

1NF Exmple
Table 1 : In 1NF Form

Before we proceed lets understand a few things --

What is a KEY ?
A KEY is a value used to uniquely identify a record in a table. A KEY could be a single column
or combination of multiple columns

Note: Columns in a table that are NOT used to uniquely identify a record are called non-key
columns.

What is a primary Key?

A primary is a single
column values used to
uniquely identify a
database record.

It has following
attributes

 A primary key cannot


be NULL
 A primary key value
must be unique
 The primary key
values can not be
changed
 The primary key must
be given a value when
What is the primary key, super key, foreign key and a new record is
candidate key in the DBMS? inserted.

A primary key is a column (or columns) in a table that uniquely


identifies the rows in that table.

CUSTOMERS

CustomerNo FirstName LastName

1 Sally Thompson

2 Sally Henderson

3 Harry Henderson

4 Sandra Wellington

For example, in the table above, CustomerNo is the primary


key.

The values placed in primary key columns must be unique for


each row: no duplicates can be tolerated. In addition, nulls are
not allowed in primary key columns.

Super keys are to be avoided.

Foreign keys are columns that point to primary key columns.


So, for example, OrderNo is the primary key of the table
ORDERS below and CustomerNo is a foreign key that points to
the primary key in the CUSTOMERS table.

ORDERS

OrderNo EmployeeNo CustomerNo Supplier Price Item

1 1 42 Harrison $235 Desk

2 4 1 Ford $234 Chair

3 1 68 Harrison $415 Table

4 2 112 Ford $350 Lamp

5 3 42 Ford $234 Chair

6 2 112 Ford $350 Lamp

7 2 42 Harrison $235 Desk

If we want to be really pedantic, foreign keys don't have to point


to a primary key. The only true requirement of the column at
which a foreign key points is that it must contains unique
values. Imagine, for example, that our employee tables looked
like this:
EMPLOYEES

SSecurityNo EmployeeNo FirstName LastName DateOfBirth DateEmployed

AF- 1 Manny Tomanny 12 Apr 01 May 1999


23432334 1966

DQ- 2 Rosanne Kolumns 21 Mar 01 Jan 2000


65444444 1977

GF- 3 Cas Kade 01 May 01 Apr 2002


54354543 1977

JK- 4 Norma Lyzation 03 Apr 01 Apr 2002


34333432 1966

VB- 5 Juan Tomani 12 Apr 01 Apr 2002


48565444 1966

FG- 6 Del Eats 01 May 01 May 2004


23566553 1967

The social security number is actually the primary key of the


table but we issue each employee with a number which is also
unique. Under these circumstances, ORDERS.EmployeeNo
can be a foreign key pointing to EMPLOYEES.EmployeeNo;
even though that column is not a primary key. So the actual rule
is slightly more subtle. A foreign key must point to a candidate
key.

A candidate key is a column that meets all of the requirements


of a primary key. In other words, it has the potential to be a
primary key.

Definition - What does Candidate Key mean?


A candidate key is a column, or set of columns, in a table
that can uniquely identify any database record without
referring to any other data. Each table may have one or
more candidate keys, but one candidate key is special,
and it is called the primary key. This is usually the best
among the candidate keys.

When a key is composed of more than one column, it is


known as a composite key.

What is Composite Key?


A composite key is a primary key composed of multiple columns used to identify a record
uniquely

In our database , we have two people with the same name Robert Phil but they live at different
places.

Hence we require both Full Name and Address to uniquely identify a record. This is a composite
key.

Let's move into second normal form 2NF


2NF (Second Normal Form) Rules
 Rule 1- Be in 1NF
 Rule 2- Single Column Primary Key
It is clear that we can't move forward to make our simple database in 2nd Normalization form
unless we partition the table above.

Table 1

Table 2

We have divided our 1NF table into two tables viz. Table 1 and Table2. Table 1 contains
member information. Table 2 contains information on movies rented.

We have introduced a new column called Membership_id which is the primary key for table 1.
Records can be uniquely identified in Table 1 using membership id

Database - Foreign Key


In Table 2, Membership_ID is the foreign Key
Foreign Key references primary key of another Table!It helps
connect your Tables

 A foreign key can have a different name from its primary key
 It ensures rows in one table have corresponding rows in another
 Unlike Primary key they do not have to be unique. Most often
they aren't
 Foreign keys can be null even though primary keys can not
Why do you need a foreign key ?

Suppose an idiot inserts a record in Table B such as

You will only be able to insert values into your foreign key that exist in the unique key in the
parent table. This helps in referential integrity.
The above problem can be overcome by declaring membership id from Table2 as foreign key of
membership id from Table1

Now , if somebody tries to insert a value in the membership id field that does not exist in the
parent table , an error will be shown!

What is a transitive functional dependencies?


A transitive functional dependency is when changing a non-key column , might cause any of the
other non-key columns to change

Consider the table 1. Changing the non-key column Full Name , may change Salutation.

Let's move ito 3NF

3NF (ThirdNormal Form) Rules


 Rule 1- Be in 2NF
 Rule 2- Has no transitive functional dependencies
To move our 2NF table into 3NF we again need to need divide our table.
3NF Example

TABLE 1

Table 2

Table 3

We have again divided our tables and created a new table which stores
Salutations.
There are no transitive functional dependencies and hence our table is in 3NF
In Table 3 Salutation ID is primary key and in Table 1 Salutation ID is foreign to
primary key in Table 3
Now our little example is in a level that cannot further be decomposed to attain
higher forms of normalization. In fact it is already in higher normalization forms.
Separate efforts for moving in to next levels of normalizing data are normally
needed in complex databases. However we will be discussing about next levels of
normalizations in brief in the following.

Boyce Codd Normal Form (BCNF)


Even when a database is in 3rd Normal Form, still there would be anomalies
resulted if it has more than one Candidate Key.
Sometimes is BCNF is also referred as 3.5 Normal Form.
4NF (Fourth Normal Form) Rules
If no database table instance contains two or more, independent and multivalued
data describing the relevant entity , then it is in 4th Normal Form.
5NF (Fifth Normal Form) Rules
A table is in 5th Normal Form only if it is in 4NF and it cannot be decomposed in to
any number of smaller tables without loss of data.
6NF (Sixth Normal Form) Proposed
6th Normal Form is not standardized yet however it is being discussed by database
experts for some time. Hopefully we would have clear standardized definition for
6th Normal Form in near future.
That's all to Normalization!!!

Potrebbero piacerti anche