Database Keys Constraints
Database Keys Constraints
Keys are very important part of Relational database model. They are used to
establish and identify relationships between tables and also to uniquely identify any
record or row of data inside a table.
A Key can be a single attribute or a group of attributes, where the combination may
act as a key.
Why a Key?
In real world applications, number of tables required for storing the data is huge, and
the different tables are related to each other as well.
Also, tables store a lot of data in them. Tables generally extends to thousands of
records stored in them, unsorted and un-organized.
Now to fetch any particular record from such dataset, you will have to apply some
conditions, but what if there is duplicate data present and every time you try to fetch
some data by applying certain condition, you get the wrong data. How many trials
before you get the right data?
To avoid all this, Keys are defined to easily identify any row of data in a table.
Let's try to understand about all the keys using a simple example.
Let's take a simple Student table, with fields student_id, name, phone and age
1 Sumit 9876723452 17
2 Alok 9991165674 19
3 Neeraj 7898756543 18
4 Sumit 8987867898 19
5 Dinesh 9990080080 17
.
Super Key
Super Key is defined as a set of attributes within a table that can uniquely identify
each record within a table. Super Key is a superset of Candidate key.
In the table defined above super key would include student_id, (student_id, name),
phone etc.
The first one is pretty simple as student_id is unique for every row of data, hence it
can be used to identity each row uniquely.
Next comes, (student_id, name), now name of two students can be same, but their
student_id can't be same hence this combination can also be a key.
Similarly, phone number for every student will be unique, hence again, phone can
also be a key.
So they all are super keys.
Candidate Key
Candidate keys are defined as the minimal set of fields which can uniquely identify
each record in a table. It is an attribute or a set of attributes that can act as a
Primary Key for a table to uniquely identify each record in that table. There can be
more than one candidate key.
In our example, student_id and phone both are candidate keys for table Student.
A candidate key can never be NULL or empty. And its value should be unique.
There can be more than one candidate keys for a table.
A candidate key can be a combination of more than one columns(attributes).
Primary Key
Primary key is a candidate key that is most appropriate to become the main key for
any table. It is a key that can uniquely identify each record in a table.
For the table Student we can make the student_id column as the primary key.
Composite Key
Key that consists of two or more attributes that uniquely identify any record in a
table is called Composite key. But the attributes which together form the
Composite key are not a key independently or individually.
In the above picture we have a Score table which stores the marks scored by a
student in a particular subject.
In this table student_id and subject_id together will form the primary key, hence it
is a composite key.
Secondary or Alternative key
The candidate key which are not selected as primary key are known as secondary
keys or alternative keys.
Non-key Attributes
Non-key attributes are the attributes or fields of a table, other than candidate key
attributes/fields in a table.
Non-prime Attributes
Non-prime Attributes are attributes other than Primary Key attribute(s)..
Foreign Key
A foreign key is an attribute (or set of attributes) that appears (usually) as a non key
attribute in one relation and as a primary key attribute in another relation. I
say usually because it is possible for a foreign key to also be the whole or part of a
primary key:
SQL Constraints
SQL constraints are used to specify rules for the data values to be permitted in a
table.
If there is any violation between the constraint and the data values, the action is
aborted by the constraint.
Syntax
The NOT NULL constraint enforces a column to NOT accept NULL values.
The NOT NULL constraint enforces a field to always contain a value. This
means that you cannot insert a new record, or update a record without
adding a value to this field.
Example
CREATE TABLE student
(
Roll number(5) NOT NULL,
Name char(40) NOT NULL,
Address varchar2 (100),
course varchar2 (20)
);
UNIQUE Constraint
The UNIQUE constraint uniquely identifies each record in a table. The UNIQUE
and PRIMARY KEY constraints both provide a guarantee for uniqueness for a
column or set of columns. A PRIMARY KEY constraint automatically has a
UNIQUE constraint defined on it.
Note You can have many UNIQUE constraints per table, but only one PRIMARY
KEY constraint per table.
Example
Example:
CHECK Constraint
The CHECK constraint is used to limit the value range that can be placed in a
column of a table.
If you define a CHECK constraint on a single column it allows only certain values
for this column.
example
DEFAULT Constraint
The default value will be added to all new records, if no other value is specified.