I am trying to build a SQL 2005 database that has a Company data table and an Employee data table. Each company should have an Employee 1, Employee 2, and so on. I realize that each Employee record would have a PK of both their EmployeeID and their CompanyID, but I haven’t for the life of me determined the best way of doing this?
Any ideas?
Let me me clarify this a bit
Each company can have its own employee #1, #2, etc. Say I have a stored procedure that returns a specific employee by passing in the company id and the requested employee id.
exec dbo.GetEmployee @CompanyID = 1, @EmployeeID = 45
Keeping in mind that Company 1 can have an Employee 1 and Company 2 can have a different Employee 1, how can I do this? How can I have an incremental primary key for employees unique for each company?
The simple answer is you can’t.
You create a primary key on the employee table, over the company ID and the employee ID. That takes care of the unique constraint.
However you cannot have the employee ID increment automatically, instead in the insert you need to wrap it in a transaction and do something like