i have a scenario, in which i have to create a table with following fields
department_name, ministry_name, domain_name, coordinator_name, coordinator_email
now, for each department_name there can be more than one coordinator_name and corresponding to it more than one coordinator_email.
i can’t do this in one table(department) because i can’t store two coordinator name and their coordinator email in a single row of mysql.
so i planned to make one table(coordinator) with department_name(primary key), ministry_name, domain_name
and second table with department_name(primary key), coordinator_name, coordinator_email with a foreign key reference to department(department_name)
now how can i use insert statement so that the data gets inserted in both tables at once. and similarly how can i use delete and update statement…
please help… thankyou…
Department A – Ministry – Domain – Coordinator A – Coordinator E-mail A
Department A – Ministry – Domain – Coordinator B – Coordinator E-mail B
Department A – Ministry – Domain – Coordinator C – Coordinator E-mail C
Department B – Ministry – Domain – Coordinator A – Coordinator E-mail A
If you just need Departments, you disregard coordinators’ information and group it by department? Unless you specifically want two tables?
You could use TRIGGERS to achieve that, trigger will happen every time INSERT, UPDATE, DELETE is executed.
Split it in two different tables
Coordinator: id, name, email, departmentId (Foreign Key to Departments.id)
Departments: id, name, ministry, domain
Then Foreign key will take care of updates and deletions (will remove all coordinators if department is deleted)
— edit
Then first you loop through departments:
SELECT DISTINCT(department_name) FROM your_table
and if then second loop is to display all coordinators for each department
SELECT * FROM your_table WHERE department_name = ”