jennifer
jennifer

Reputation: 8261

Encrypt a value in MYSQL DB

I have 2 tables employee_master & user_master

Employee_id is a foreign key in user_master.

I need to encrypt the value of Employee_id while the value is being inserted into user_master table.

but the foreign key relationship should be kept... How can we do that

Upvotes: 0

Views: 335

Answers (2)

ace
ace

Reputation: 6825

I know you can't as if you will get an error.

Cannot add or update child row: a foreign key constraint fail......

The foreign key should have same value and type to its referenced field.

I'm not pretty sure with this, as an alternative you can refer to Bhushan post. Another is, make the foreign key as a normal field and let the query handle if the field is related to referenced field. But of course foreign key has other uses which a normal field doesn't have.

Upvotes: 1

Sachin
Sachin

Reputation: 18747

You can use some sort of intermediate table, which will have 2 columns: Employee_id and Encrypted_empi_id and then your user_master can use the corresponding Encrypted_empi_id as a key. I am not sure if this is a feasible model for you or not, just a thought.

You can use MD5 for getting encrypted version of emp id and can reverse the encrypted version to get back the exact original emp id.

Upvotes: 1

Related Questions