Techno Cracker
Techno Cracker

Reputation: 453

MYSQL- Create and store primary key in master and child table

I got stuck while insertion into database tables.
I have below tables.

Table Relations


I dont have any fields which can be make as Primary key.
Here "eme_request" and "eme_attachment" is master tables.

I want to know that how can I insert into tables ?
These tables(eme_request, eme_gf_relevent, eme_non_gf_relevent, eme_attachment) are getting insertion on one form request.


So I am not getting how to generate primary key in master table and how to insert into child table with that primary key as a Foreign key ?

Upvotes: 1

Views: 542

Answers (1)

mleko
mleko

Reputation: 12243

In eme_request table REQ_ID (your PK) is type of varchar. If you can change it to INT and add flag AUTOINCREMENT, you will get automatically generated key.

If you can't change type of REQ_ID (you need it to be char), just add abstract PRIMARY_KEY int column and use it.

After inserting into table with autoincrement column you can receive assigned key with https://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_last-insert-id so it can be used as FOREIGN KEY for other tables.

Upvotes: 1

Related Questions