简体   繁体   中英

create two tables linked with foreign key, insert,delete,and update in mysql

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...

Just unclear about your question. You wrote that table Coordinator will have department_name as primary key. How is this possible? Shouldnt you have department_name as foriegn key in Coordinator table? I am sorry if i misinterpreted your question

  1. You could use one table

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?

  1. You could use TRIGGERS to achieve that, trigger will happen every time INSERT, UPDATE, DELETE is executed.

  2. 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 = ''

First off, don't use names for a primary key, use an id as the key instead. Take your department, make an id (auto increment) and no value so that it does this itself. Then generate a unique id that is attached to that department name, then you can use this unique id to call up later if needed. However, you will need this id to associate the other names with this department. Keep the department name in one table that you created the unique id with, and have another table that has these names with this id for each of the values used. And you need more than one query to do this.


dept. table: id(primary|auto inc) uid dept_name

coord. table: id(primary|auto inc) uid(from dept. table) min_name coord_name coord_email

<?php
    mysql_query("
        insert into `dept` values(
            NULL,
            `$uid`,
            `$dept_name`
        )
    ");
    mysql_query("
        insert into `coord` values (
            NULL,
            `$uid`,
            `$coord_name`,
            `$coord_email`,
            `$ministry_name`
        )
    ");
?>

Maybe this solved it for you.

if you use 2 tables Table "Department" Columns : id, name, ministry, domain

Table "Coordinator" Columns : id, name, email, departmentId

Assuming you relation between Department and Coordinator table. Below query should return records based on department name

select * from Coordinator 
     join Department on Coordinator.departmentId = Department.Id
Where Department.name = '' --- pass any name you want in single quotes

Let me know if this works for you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM