mysql - How to make a relation between two tables without foreign key -
i'm total newbie , i'm trying mysql database, using xampp linux 1.8.1.
i want make relation between 2 tables , b. for know foreign keys create bijection, or one-to-one relation. must not have such strict relation, so created column inside table stores id of table b.
is correct? there's not way enforce it? mean, this way delete row of table b referenced in table a. you store value inside doesn't correspond id of row of b. how prevent this?
the main problem me prevent deletion of row of table b if row id referenced row of table a.
create table table_b ( b_id integer primary key ); create table table_a ( b_id integer primary key references table_b (b_id) ); insert table_b values (1); insert table_a values (1);
the following statement fail.
delete table_b b_id = 1;
if you'd built postgresql, error message
error: update or delete on table "table_b" violates foreign key constraint "table_a_b_id_fkey" on table "table_a" detail: key (b_id)=(1) still referenced table "table_a".
that structure gives "1 0 or 1" relationship between 2 tables. "1 0 or many", add 1 or more columns table_a's primary key.
create table table_b ( b_id integer primary key ); create table table_a ( b_id integer references table_b (b_id), checkout_date date not null default current_date, primary key (b_id, checkout_date) );
that structure let table_a store multiple rows 1 value of b_id, each of rows must have different checkout_date.
Comments
Post a Comment