Constraint and Validation
Integrity Constraints
Integrity constraints guard against accidental damage to the database by ensuring the authorized changes to the database do not result in a loss of data consistency.
They include
- Domain Constraint
- Referential Integrity
- Assertions
- Triggers
Domain Constraints
They define valid values for attributes, including string, character, integer, time, date, currency, etc.
Type of Constraints
| Constraints | Key |
|---|---|
| Entity Constraint is given within one table. | PRIMARY KEY, FOREIGN KEY, UNIQUE, NULL |
| Referential Constraint is used for referring other tables to enforce conditions on the data. | FOREIGN KEY |
| Semantic Constraint which typically refer to data types. | Data types |
Key Constraint
There are 5 types of key constraints in DBMS:
NOT NULL: Ensure the column is not aNULLvalue.UNIQUE: Ensure values in the column are not duplicated.DEFAULT: Provide a default value for a column.CHECK: Check for the predefined conditions before inserting data into the table.PRIMARY KEY: It uniquely identifies a row in a table.FOREIGN KEY: Ensure referential integrity of the relationship.
Use CHECK
CREATE TABLE my_table (
id INT PRIMARY KEY,
age INT CHECK (age >= 18)
);
INSERT INTO my_table (id, age) VALUES (1, 5);The statement above will fail since the age inserted is less than .
Trigger
A trigger is a statement that is executed automatically by the system when modifying the database.
To design a trigger, two things should be specified:
- Conditions under which the trigger is to be executed.
- Actions to be taken when the trigger executes.
Benefits of Trigger
- Generating some derived column values automatically
- Enforcing referential integrity
- Event logging and storing information on table access
- Auditing
- Synchronous replication of tables
- Imposing security authorizations
- Preventing invalid transactions
Last updated on