在关系表上使用 INSERT 的更好方法是什么?

发布于 2024-08-17 00:41:09 字数 1700 浏览 4 评论 0原文

我正在经历我的第一次 mysql INSERT 测试,并且正在使用这些表:

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

我将数据存储在 table_employeetable_areas中table_agency 但我不必同时保存所有数据,因此我可以创建一名员工,然后创建一个机构或地址。

在单一数据插入的情况下,我应该使用这样的东西还是应该直接使用表node_employees,如果是,我该怎么做?

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')

为了将行相互链接,我创建了 node_employees,一个关系表。 我用它来将员工与某个区域或机构链接起来,那么我应该如何将数据与该关系表链接起来呢?

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')

我还有一个疑问,如果我需要将员工与某个区域关联起来该怎么办?我是否应该使用不同的查询,即针对每种可能性的查询?

// 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?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恬淡成诗 2024-08-24 00:41:09

我假设机构和区域是一对多的关系。换句话说,每个员工可以被分配到多个机构和区域。如果没有,那么我只需将 Agency_id 和area_id 字段添加到employees 表中,甚至不创建node_employees 表。

考虑到上述情况...

从您提供的描述来看,机构和区域似乎是相对静态的。因此,应在输入员工数据之前设置它们。

然后,在插入员工时,使用 mysql_insert_id() 返回您刚刚创建的记录的 id。 (我假设employee_id是一个自动增量字段)

所说的那样执行

SELECT agency_id FROM agencies WHERE agency_name = 'Google'

// I'll get their ids in php
$php_employee_id <== from mysql_insert_id() after inserting employee record
$php_agency_id <== from above agency Select

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

然后按照您对上一个查询

INSERT语句添加一条新记录,它不会替换现有记录。如果现有记录具有唯一索引,例如“employee_id”,它将失败并出现错误。您需要使用 UPDATE 来执行此操作,或者使用 REPLACE 语句来更新记录(如果存在)或 INSERT(如果记录不存在)。确保您仔细阅读了它,因为它通过唯一索引起作用。

直流

I am assuming the agencies and areas are one to many relationships. in other words each employee can be assigned to multiple agencies and areas. If not then I would simply add the agency_id and area_id fields to the employees table and not even create the node_employees table.

With the above in mind...

From the description you have provided it appears agencies and areas are relativally static. So they should be set up before the employee data is entered.

Then when inserting the employee use mysql_insert_id() to return the id of the record you just created. (I am assuming the employee_id is an auto increment field)

Then do as you have said

SELECT agency_id FROM agencies WHERE agency_name = 'Google'

// I'll get their ids in php
$php_employee_id <== from mysql_insert_id() after inserting employee record
$php_agency_id <== from above agency Select

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

for your last query

An INSERT statement adds a new record it will not replace the existing record. if the existing record has a unique index such as 'employee_id' it will fail with an error. You need to use UPDATE to do that OR the REPLACE statement which UPDATEs a record if one exists or INSERTs if the record doesnt exist. make sure you read up on it because it works via unique indexes.

DC

长安忆 2024-08-24 00:41:09

为了将行相互链接,我创建了node_employees,一个关系表。我用它来将员工与某个区域或机构链接起来,那么我应该如何将数据与该关系表链接起来呢?

这应该是两张表 - 一张用于将员工与机构相关联,另一张用于将员工与区域相关联。您没有提到任何将机构与区域相关的内容,给人的印象是它们彼此独立...

无论如何,node_employees.employee_id 应该是 EMPLOYEES.employee_id 的外键code>,以确保员工必须已作为系统中的有效值存在。对于 node_employeesagencies 表之间的 agency_id 也是如此。

由于这些关系,值必须先存在于 EMPLOYEESAGENCIES 表中,然后存在于 NODE_EMPLOYEES 表中。这使得 NODE_EMPLOYEES 表与其他两个表处于“子”关系,因此您的三个 INSERT 语句必须在子表之前插入父表,并使用子表中父表的值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文