简体   繁体   English

最佳实践MySQL UPDATE / INSERT

[英]Best practice MySQL UPDATE/INSERT

Disclaimer: I didn't design these tables, and am aware they're not the best way to store this data. 免责声明:我没有设计这些表,并且知道它们不是存储这些数据的最佳方法。 I need advice on the best way to handle the situation. 我需要有关处理这种情况的最佳方法的建议。

I have two MySQL tables: 我有两个MySQL表:

`students`
id        int; primary, unique
prof1_id  varchar 
prof2_id  varchar 
prof3_id  varchar 

`professors`
id         int; primary, unique, auto-increment
username   varchar
fname      varchar
fname      varchar
students_id int
comment    text

A professor's username may be in any of the last three columns in the students table. 教授的用户名可以在“ students表的后三列中的任何一列中。 Professors will need to provide one comment for each student who has them in their row in the students table. 教授将需要为在students表中的行中的每个学生提供一条评论。

The application that is the front end to this database expects two more columns in the professors table: student_id and comment. 作为该数据库前端的应用程序希望在professors表中再增加两列:student_id和comment。 Since each student may have any 3 professors in their row, new rows will need to be added to professors whenever they are listed for multiple students. 由于每个学生的行中可能有3位教授,因此只要列出了多个学生,就需要向professors添加新行。 This means that the professors id field will need to auto increment for each new student comment. 这意味着, professors ID字段将需要为每个新学生评论自动递增。

What is the best way to accomplish this data transfer with a MySQL query? 用MySQL查询完成此数据传输的最佳方法是什么? I've tried looking at UPDATE in combination with JOIN, but I'm not sure there is even a valid way to do this. 我尝试结合UPDATE来查看UPDATE,但是我不确定甚至没有有效的方法来执行此操作。 Also, do I need to create another primary key for professors since the multiple rows will have the same id? 另外,由于多个行具有相同的ID,我是否需要为professors创建另一个主键?

I suspect that a VIEW might be the best way to accomplish this, but the application on the front end expects the information to be stored in the professors table. 我怀疑使用VIEW可能是实现此目的的最佳方法,但是前端的应用程序希望信息存储在professors表中。

One other thing I was considering was that I could create a new table by joining the two tables and have a two-column primary key, students.id, professor.id. 我正在考虑的另一件事是,我可以通过连接两个表来创建一个新表,并具有一个两列主键,students.id,professor.id。

Thanks! 谢谢!

Also, do I need to create another primary key for professors since the multiple rows will have the same id? 另外,由于多个行具有相同的ID,我是否需要为教授创建另一个主键?

Yes. 是。 This is good idea. 这是个好主意。

I wrote simple query that merges data from first table into 2 columns. 我编写了简单的查询,将第一个表中的数据合并为2列。 This is not complete answer, but it can help you a lot: 这不是完整的答案,但可以为您提供很多帮助:

SELECT id, prof1id as profid
UNION
SELECT id, prof2id
UNION
SELECT id, prof3id
UNION;

You may use this for view, inserts, but im not familiar with specyfic MySQL syntax and i dont want to misslead you. 您可以将其用于查看,插入,但我不熟悉specyfic MySQL语法,我也不想误导您。 Please give feedback if it work. 如果有效,请提供反馈。

"UNION" removes duplicate rows, you may need to use "UNION ALL" to keep duplicates (like duplicated values in 2 or 3 professors columns). “ UNION”删除重复的行,您可能需要使用“ UNION ALL”来保留重复的内容(例如2或3个Professors列中的重复值)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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