I'm trying to come up with an efficient way to generate/validate unique access keys for an application I'm writing. The issue I'm having is because of how the SQL database is set up.
Basically, there are two tables in question. A Users table and a Faculty table. There is a one to many relationship between the Users and Faculty tables. Every user will create an account using just a personal email and password. That will create an entry in the Users table.
Now, I want to allow admins to enter a domain email and name, and generate a setup key. The user will then be able to enter that access key which will link their row in the Users table to a row in the Faculty table with a user_id foreign key.
The way I've thought about doing it is that when an admin generates the setup key it will create a row in the faculty table with a null foreign key. The user would then have to enter the setup key AND the domain email that the admin entered. Assuming it all matched, it would then add their user_id to that foreign key column.
Here's where the issues/questions lie. Would it be more efficient to use a separate table containing the setup keys and emails as sort of a lookup table and only searching a table with two columns before adding a row to the faculty table? I think this may be faster as rows could be deleted once they were validated leaving fewer rows to search every time. Also, I'm not sure if there is an issue with leaving the foreign key null as in my first method.
Assuming a unique key/email pair, would that be secure enough, or would I also need some way to check against the signed in user?
Any resources or ideas are greatly appreciated.