Search code examples
phpmysqldatabaserelationalinsert-id

What are the better ways with INSERT on relational tables?


I'm experiencing my first mysql INSERT tests and I'm using these tables:

table employees
-> employee_id
-> employee_name
-> employee_surname
-> employee_url

table areas
-> area_id
-> area_city
-> area_address
-> area_country

table agencies
-> agency_id
-> agency_name
-> agency_url

table node_employees
-> node_id
-> employee_id
-> area_id
-> agency_id

I would store data in table_employee, table_areas and table_agency but I'm not forced to save all the data simultaneously, so I could create an employee, and subsequently an agency or an address.

In a case of singular data insert, should I use something like this or shoud I use directly the table node_employees, if yes, how can I do it?

INSERT INTO employees (employee_name, employee_surname, employee_url)
VALUES ('Roger', 'Waters', 'http://pinkfloyd.com')

INSERT INTO agencies (agency_name, agency_url)
VALUES ('Google', 'http://google.com')

INSERT INTO areas (area_city, area_address, area_country)
VALUES ('Rome', 'Via Roma, 123', 'Italy')

To link rows each other I've created node_employees, a relational table. I use it to link an employee with an area or an agency, so what I should do to link data with this relational table?

SELECT employee_id FROM employees WHERE employee_name = 'Roger'
SELECT agency_id FROM agencies WHERE agency_name = 'Google'

// I'll get their ids in php
$php_employee_id
$php_agency_id

// and then
INSERT INTO node_employees (employee_id, agency_id)
VALUES ('$php_employee_id', '$php_agency_id')

I have also another doubt, what I should do if I need to link an employee with an area? shoud I use a different query, so a query for every possibility?

// so, not this
$php_employee_id = 12;
$php_agency_id = 7;
$php_area_id = null;
INSERT INTO node_employees (employee_id, agency_id, area_id)
VALUES ('$php_employee_id', '$php_agency_id', '$php_area_id') // will this remove the previous data with null in area_id?

Solution

  • To link rows each other I've created node_employees, a relational table. I use it to link an employee with an area or an agency, so what I should do to link data with this relational table?

    That should be two tables - one for relating employees to agencies, and a separate one for relating employees to areas. You haven't mentioned anything relating agencies to areas, giving the impression they are independent of one another...

    Regardless, the node_employees.employee_id should be a foreign key to EMPLOYEES.employee_id, in order to ensure that the employee must already exist as a valid value in the system. Likewise for the agency_id between node_employees and agencies tables.

    Because of those relationships, values have to exist in the EMPLOYEES and AGENCIES tables before they exist in the NODE_EMPLOYEES table. That makes the NODE_EMPLOYEES table in a "child" relationship with the other two tables, so your three INSERT statements would have to insert into the parents before the child, and use the values from the parents in the child.