0% found this document useful (0 votes)
13 views

Constraints 1

The document discusses different types of constraints used to maintain data integrity in databases including domain constraints, entity integrity constraints, referential integrity constraints, and key constraints. Domain constraints define valid values for attributes and include checks for null, unique, primary and foreign keys. Entity integrity constraints require primary keys to be non-null. Referential integrity constraints define relationships between primary and foreign keys in different tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Constraints 1

The document discusses different types of constraints used to maintain data integrity in databases including domain constraints, entity integrity constraints, referential integrity constraints, and key constraints. Domain constraints define valid values for attributes and include checks for null, unique, primary and foreign keys. Entity integrity constraints require primary keys to be non-null. Referential integrity constraints define relationships between primary and foreign keys in different tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CONSTRAINTS

Constraints are the set of rules used to maintain the quality of information. In this context,
integrity constraints ensure that the data insertion, updating, and other processes have to be
performed in such a way that data integrity is not affected. Thus, integrity constraint is used to
guard against accidental damage to the database.
CONSTRAINT TYPES

1. Domain constraints
 Domain constraints can be defined as the definition of a valid set of values for an
attribute.
 The data type of domain includes string, character, integer, time, date, currency, etc. The
value of the attribute must be available in the corresponding domain. Example:

Domain Constraint = data type(integer / character/date / time / string


/ etc.) + Constraints(NOT NULL / UNIQUE / PRIMARY KEY / FOREIGN KEY /
CHECK / DEFAULT)
Type of domain constraints:
There are two types of constraints that come under domain constraint are as follows:
1. Domain Constraints – Not Null: Null values are the values that are unassigned or we can
also say that which are unknown or the missing attribute values and by default, a column can
hold the null values. Now as we know that the Not Null constraint restricts a column to not
accept the null values which means it only restricts a field to always contain a value which
means you cannot insert a new record or update a record without adding a value into the field.
Example: In the ’employee’ database, every employee must have a name associated with them.
Create table employee (employee_id varchar(30),employee_name varchar(30) not null, salary
NUMBER);
2. Domain Constraints – Check: It defines a condition that each row must satisfy which means
it restricts the value of a column between ranges or we can say that it is just like a condition or
filter checking before saving data into a column. It ensures that when a tuple is inserted inside
the relation must satisfy the predicate given in the check clause.
Example: We need to check whether the entered id number is greater than 0 or not for the
employee table.
Create table employee (employee_id varchar(30) not null
check(employee_id > 0), employee_name varchar(30), salary
NUMBER);
 The above example creates CHECK constraints on the employee_id column and specifies
that the column employee_id must only include integers greater than 0.
2. Entity integrity constraints
 The entity integrity constraint states that primary key value can't be null.
 This is because the primary key value is used to identify individual rows in relation and if the
primary key has a null value, then we can't identify those rows.
 A table can contain a null value other than the primary key field.
Example:
Primary keys may be defined at the column level for a single column key or at the table level if
multiple columns make up the primary key.
First Method:
CREATE TABLE Persons (
ID int PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int
);
Second Method:
CREATE TABLE table_name
(
column1 datatype null/not null,
column2 datatype null/not null,
...
CONSTRAINT constraint_name PRIMARY KEY (column1, column2, ...
column_n)
);
CREATE TABLE supplier
(
supplier_id numeric(10) not null,
supplier_name varchar2(50) not null,
contact_name varchar2(50),
CONSTRAINT supplier_pk PRIMARY KEY (supplier_id)
);

Drop Primary Key

ALTER TABLE table_name DROP CONSTRAINT constraint_name;


Example: ALTER TABLE supplier DROP CONSTRAINT supplier_pk;
3. Referential Integrity Constraints

 A referential integrity constraint is specified between two tables.


 In the Referential integrity constraints, if a foreign key in Table 1 refers to the Primary
Key of Table 2, then every value of the Foreign Key in Table 1 must be null or be
available in Table 2.
Example:

Example 1:
CREATE TABLE Orders (
OrderID int NOT NULL,
OrderNumber int NOT NULL,
PersonID int,
PRIMARY KEY (OrderID),
FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
);
4. Key constraints
 Keys are the entity set that is used to identify an entity within its entity set uniquely.
 An entity set can have multiple keys, but out of which one key will be the primary key. A
primary key can contain a unique and null value in the relational table.

You might also like