Skip to main content

Constraints in database

What is Constraints in SQL ?:
• Constraints are restriction or conditions that are used on the columns of the table
to preserve the data correctness.
• A constraint is a condition which restricts the invalid data in the table.
• A constraint can be provided for the column of a table.
Type of Constraint:
1. NOT NULL.
2. UNIQUE.
3. PRIMARY KEY.
4. FOREIGN KEY.
5. CHECK.
1) NOT NULL:
• Ensures that at least some values should be present for attribute or columns.
• In a table we can have more than one not null.
• Null is nothing, it is neither zero nor blank.
• Null will not occupy any space in memory.
• Two null are never some in oracle.
• Null is used to represent the empty values.
2. UNIQUE:
• It is used to have only unique values inside a column.
• Unique constraint doesn’t allow you to enter duplicate values into some column
3. PRIMARY KEY:
• It is used to identify each record uniquely.
• It is combination of Not Null and Unique.
• Only one Primary key is allowed in table.
4. FOREIGN KEY:
• It is a referential integrity constraint which creates relationship between tables.
• It is created in child table.
• FK column can have both duplicate and null values
• Masters table should have a primry key defined a column in the master table to create FK
• We can have more than 1 FK in table
5. CHECK:
• Is used for enforcing some additional condition with respect to business requirements.
• Is used to provide additional validation as per the customers’ requirements.