php - Many to One relationship? -
the following mysql code...
create table employee ( id mediumint unsigned not null auto_increment primary key, departmentid tinyint unsigned not null comment "constraint foreign key (departmentid) references department(id)", firstname varchar(20) not null, lastname varchar(40) not null, email varchar(60) not null, ext smallint unsigned null, hiredate timestamp not null default current_timestamp, leavedate datetime null, index name (lastname, firstname), index (departmentid) ) create table department ( id tinyint unsigned not null auto_increment primary key, name varchar(40), unique (name) ) ... defines many 1 relationship between employee , department. in other words, employee can in 1 department, department can have many employees. can explain in more detail? how above code tell me that?
it easy discern relationship between 2 (or more) tables looking @ foreign key of referring table.
a simple rule is, if foreign key primary key in own table, relationship 1 1, whereas if foreign key not primary key in own table, relationship 1 many, table having foreign key @ "many" end.
in example, departmentid in employee, not primary key. therefore, in situation, department can referenced lot of employees @ same time, hence "1 many" relation can established.
however, in quoted example from mkyong.com, both tables (stock , stock_detail) use stock_id primary key. in situation, "1 1" relation can established.
consider this: both stock , stock_detail tables contain 1 single record having value stock_id. i made sample here.
Comments
Post a Comment