Constraints on Attributes

 

While Keys identify the row, Attribute Constraints focus on the specific data inside the columns. They are the rules that prevent “bad” data from entering the table.
Here are the four essential constraints applied to attributes:
 
1. NOT NULL
Ensures that a column cannot be empty. You use this for mandatory information like a username or an email address.
  • Example: An Employee_Name field must always have a value.
 
2. UNIQUE
Ensures that all values in a column are different from each other. Unlike a Primary Key, a table can have multiple UNIQUE columns, and they can (usually) accept a NULL value.
  • Example: A Phone_Number or Social_Media_Handle.
 
3. DEFAULT
Provides a pre-set value for the column if the user doesn’t provide one. It prevents the field from being empty by filling it with a standard entry.
  • Example: A Status column might default to 'Active' or Country to 'India'.
 
4. CHECK
Enforces a specific business rule by checking the data against a logical expression before allowing it to be saved.
  • Example: Age >= 18 or Salary > 0.