Using CHECK Constraint to Validate Data in SQL
📖 Scenario: You are creating a simple database table to store information about employees in a company. You want to make sure that the age of each employee is a reasonable number, between 18 and 65 years old.
🎯 Goal: Build a SQL table called Employees with columns for EmployeeID, Name, and Age. Use a CHECK constraint to ensure that the Age is always between 18 and 65.
📋 What You'll Learn
Create a table named
Employees with columns EmployeeID (integer), Name (text), and Age (integer).Add a
CHECK constraint on the Age column to allow only values between 18 and 65 inclusive.Use standard SQL syntax compatible with common databases like PostgreSQL or MySQL.
💡 Why This Matters
🌍 Real World
Companies use CHECK constraints to make sure data like ages, salaries, or IDs stay within valid ranges, preventing mistakes.
💼 Career
Database administrators and developers use CHECK constraints to enforce business rules directly in the database, improving data quality and reliability.
Progress0 / 4 steps