I have two tables in my SQL database:
Company: - ID (autoincrement) - name - address - ...
Employees: - ID (autoincrement) - Company_id - internal_id - name - lastname
The problem is that I would like to have a employee id (internal_id) that is relative to the company they belong to. I got this dilema since I'm really been searching what would be the cleanest way to implement it.
One option would be to just make a kind of SELECT MAX(internal_id) FROM employees WHERE company_id = X, but the problem would be that if I happen to delete the last employee the next one would be created with the ID of the next.
Any ideas or suggestions?
PD: The reason of why I want to do this is that i dont want a user from company X create an employee that is for example ID=2000, while the last employee created in his company was, say, 1532. this would normally happen in a system in wich Company Y and Z also create employees on the same system. I want this ID not to use as a foreign_key, but to have it for internal (even documents or reports) use.
PD2: In this case the employees will never have to change companies
UPDATE
Thanks to all, there is really good advice here... It was hard to choose which was the best answer to me.. I picked that one beacuse I think is the simplest.
Guillermo