I'm grappling to design table relation. Version is Mysql 5.6.10.
My case is like below
bill table, payment table, contract table, recruitment tables… are existing.
Currently we're considering new function it will use bill or payment table. So I have designed new_function_table.
CREATE TABLE new_function_table`(
id int unsigned not null auto_increment,
bill_id int unsigned null default null,
payment_id int unsigned null default null,
primary key(id),
index fk_new_function_table_bill_id_idx (bill_id asc),
index fk_new_function_table_payment_id_idx (payment_id asc),
constraint fk_new_function_table_bill_id foreign key (bill_id) references bill (id) on delete no action on update cascade,
constraint fk_new_function_table_payment_id foreign key (payment_id) references payment (id) on delete no action on update cascade
)engine=innodb
Personally now is ok, but contract, recruitment, etc table be included into new_function_table in the future.
If new_function_table use 10 tables(bill table, payment table, contract table, recruitment tables…), then new_function_table has 10 index.
So i have designed like below It ignore data integrity.
CREATE TABLE new_function_table`(
id int unsigned not null auto_increment,
category tinyint unsigned not null comment '1: bill, 2: payment...',
reference_table_id int unsigned not null comment 'bill_id or payment_id...';
)engine=innodb
What is the good design for this case? If you have an any idea please help me! Thank you.
Best Answer
I have designed with @Walter Mitty opinion.
If it is wrong, then please feedback! Thank you.