Dropping Constraints:: Alter Table Employees Drop Constraint Employees - PK
Dropping Constraints:: Alter Table Employees Drop Constraint Employees - PK
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.
NOT NULL Constraint: Ensures that a column cannot have NULL value.
DEFAULT Constraint: Provides a default value for a column when none is specified.
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:
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.
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
1NF Exmple
Table 1 : In 1NF Form
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.
A primary is a single
column values used to
uniquely identify a
database record.
It has following
attributes
CUSTOMERS
1 Sally Thompson
2 Sally Henderson
3 Harry Henderson
4 Sandra Wellington
ORDERS
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.
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
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 ?
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!
Consider the table 1. Changing the non-key column Full Name , may change Salutation.
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.