Employee Table Design and Constraints
Question
Consider a table named Employee with the following columns:
- employee_id (integer)
- employee_name (varchar)
- email (varchar)
- salary (integer)
- department_id (integer)
Answer the following:
- Identify and explain three different types of constraints that should be applied to this table to ensure data integrity.
- Write the SQL CREATE TABLE statement for the Employee table including these constraints:
employee_idas the primary key,emailmust be unique,salarycannot be negative,department_idcannot be null.
Answer
i) Three Types of Constraints for the Employee Table
- Primary Key Constraint: Ensures each record is uniquely identifiable. Applied on
employee_id. - Unique Constraint: Ensures no duplicate emails. Applied on
email. - Check and Not Null Constraints: Enforce valid data:
salarymust be non-negative,department_idmust not be null.
ii) SQL CREATE TABLE Statement with Constraints
CREATE TABLE Employee (
employee_id INT PRIMARY KEY,
employee_name VARCHAR(100),
email VARCHAR(255) UNIQUE,
salary INT CHECK (salary >= 0),
department_id INT NOT NULL
);
Explanation:
employee_idis set as the primary key for unique identification.emailis unique to avoid duplicate email entries.salaryhas a CHECK constraint ensuring it is zero or positive.department_idis NOT NULL to guarantee assignment to a department.
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন