I have a table (oracle database) where i store departments and their employees.
Table: DepartmentEmployee
╔══════════════╦═══════════╦═════════════╦═══════════════════════════╗
║ Column ║ Data type ║ Nullability ║ Description ║
╠══════════════╬═══════════╬═════════════╬═══════════════════════════╣
║ DepartmentID ║ INT ║ NOT NULL ║ Foreign Key to Department ║
║ EmployeeID ║ INT ║ NOT NULL ║ Foreign Key to Employee ║
║ StartDate ║ DATE ║ NOT NULL ║ Date the employee joined ║
║ EndDate ║ DATE ║ NULL ║ Date the employee left ║
╚══════════════╩═══════════╩═════════════╩═══════════════════════════╝
An employee should only have one (1) department he is currently working at. The department he is working would have no EndDate (= NULL).
So the query would be:
SELECT * FROM DepartmentEmployee WHERE EmployeeID = 1 AND EndDate IS NULL;
When an employee is no longer working at the company he would have no (0) departments with an EndDate.
So my question is: How can i make sure that only 1 or 0 rows in the table have an EndDate set to NULL for an EmployeeID? Thanks.
Active = Y/N- Moudiz