如何在一个数据库字段中插入许多条目

时间:2022-09-15 17:57:54

I have a Java webproject with servlets, jsp, java and so on. There I have many projects inside, which I can choose in a combobox like this:

我有一个带有servlets,jsp,java等的Java webproject。我在里面有很多项目,我可以在这样的组合框中选择:

Project [Combobox]    

Priority[Combobox]     Stage[Combobox]

For each project I can select other values in the combobox Priority and Stage. So we can say, that the values in priority and stage are always in relation with the chosen project.

对于每个项目,我可以在组合框优先级和阶段中选择其他值。所以我们可以说,优先级和阶段的值总是与所选项目有关。

In the database I have tables like this:

在数据库中我有这样的表:

Table project:
projectid    
projectname

Table priority
id 
priorityname 
projectname

Know if i have the value prio1 for the priority and i want to give this value Project 1 and Project 2, i have to do two entries in the database in the table priority: like this

知道我是否具有优先级的值prio1,并且我想给这个值项目1和项目2,我必须在数据库中的表优先级做两个条目:像这样

id priorityname projectname
1  prio1        project1 
1  prio1        project2

But i want to do only one entrie the database: So my idea in the database was like this:

但我想只做一个数据库:所以我在数据库中的想法是这样的:

id priorityname projectname
1  prio1        project1,project2

I don't have much experience in this subject. So I wanted to tell you this, maybe my idea isn't good and I hope there is a better solution.

我对这个课程没有多少经验。所以我想告诉你这个,也许我的想法不好,我希望有更好的解决方案。

1 个解决方案

#1


2  

You can't insert multiple values in one database-field.

您不能在一个数据库字段中插入多个值。

Depending on your used Database there should be different ways to reach your goal. Try using Foreign Keys if your Database supports them (e.g. MySQL InnoDB databases do).

根据您使用的数据库,应该有不同的方法来实现您的目标。如果您的数据库支持外键,请尝试使用外键(例如MySQL InnoDB数据库)。

With foreign Keys you'd need to point from your Project table entries to a Priority entry. You then can do a SELECT query, like:

使用外键,您需要从Project表条目指向Priority条目。然后,您可以执行SELECT查询,例如:

SELECT * 
FROM projects, 
     priorities 
WHERE projects.proj_priority = priorities.prio_id 
  AND priorities.prio_name = "prio1"

(assuming)

(假设)

The tables would look like this:

表格如下所示:

projects (proj_id, proj_name, proj_priority)
priorities (prio_id, prio_name)

where proj_id and prio_id are the primary keys and proj_priority is the foreign key.

其中proj_id和prio_id是主键,proj_priority是外键。

#1


2  

You can't insert multiple values in one database-field.

您不能在一个数据库字段中插入多个值。

Depending on your used Database there should be different ways to reach your goal. Try using Foreign Keys if your Database supports them (e.g. MySQL InnoDB databases do).

根据您使用的数据库,应该有不同的方法来实现您的目标。如果您的数据库支持外键,请尝试使用外键(例如MySQL InnoDB数据库)。

With foreign Keys you'd need to point from your Project table entries to a Priority entry. You then can do a SELECT query, like:

使用外键,您需要从Project表条目指向Priority条目。然后,您可以执行SELECT查询,例如:

SELECT * 
FROM projects, 
     priorities 
WHERE projects.proj_priority = priorities.prio_id 
  AND priorities.prio_name = "prio1"

(assuming)

(假设)

The tables would look like this:

表格如下所示:

projects (proj_id, proj_name, proj_priority)
priorities (prio_id, prio_name)

where proj_id and prio_id are the primary keys and proj_priority is the foreign key.

其中proj_id和prio_id是主键,proj_priority是外键。