想要一种在单行的单列中插入多个数据的最佳解决方案

发布于 2024-12-21 09:53:58 字数 251 浏览 1 评论 0原文

DROP TABLE District_Info;

CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Boundary_dist VARCHAR2(20)
);

对于此代码,对于每个 Dname,都有多个 Boundary_dist。如果我可以使用 varchar2(20) 数组并根据需要插入尽可能多的 border_dist ,那就更好了。想要一些有用的建议。

DROP TABLE District_Info;

CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Boundary_dist VARCHAR2(20)
);

for this code, for each Dname there are more than one Boundary_dist. it would be better if i could use an array of varchar2(20) and insert as much boundary_dist as i need to. want some helpful suggestions.

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

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

发布评论

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

评论(3

梦亿 2024-12-28 09:53:58

正如已经提到的,执行此操作的理想方法是为每个 DName/Boundary_Dist 组合存储一个附加行。对于如此简单的结构,最简单的方法就是更改主键:

CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Boundary_dist VARCHAR2(20) primary key);

如果您需要该表中与 District 具有 1:1 相关性的其他数据,则最好将 BoundayDist 拆分为单独的数据表:

CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Other_info VARCHAR2(20)
);

CREATE TABLE District_Boundary(
Dname VARCHAR2(20) primary key,
Boundary_dist VARCHAR2(20) primary key);

如果您确实坚持每行存储多个值,则可以使用用户定义的数据类型:

create type varchar_20_list as table of varchar2(20);

CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Boundary_dist varchar_20_list);

As already mentioned, the ideal way to do this would be to store an additional row for each DName/Boundary_Dist combination. The simplest way to do with a structure this simple is to just change your primary key:

CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Boundary_dist VARCHAR2(20) primary key);

If you're going to need other data in that table that has a 1:1 correlation to District, you would be better off splitting BoundayDist into a separate table:

CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Other_info VARCHAR2(20)
);

CREATE TABLE District_Boundary(
Dname VARCHAR2(20) primary key,
Boundary_dist VARCHAR2(20) primary key);

If you really insist on storing more that one value per row, you can use a user-defined datatype:

create type varchar_20_list as table of varchar2(20);

CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Boundary_dist varchar_20_list);
安稳善良 2024-12-28 09:53:58

您需要为 Boundary_dist 创建一个单独的表,并创建一个引用 District_Info 表的外键。

you need to create a separate table for Boundary_dist and create a foreign key which references the District_Info table.

吾性傲以野 2024-12-28 09:53:58

同意,最好将 Boundary_dist 放在第二个表中,并将它们与 ID 连接起来,但如果您必须这样做,我通常会在代码中将其存储为“1,2,3,4,5”,我可以很容易地使用PHP使用explode或使用MySQL使用IN来操作它。

Agreed, it'd be best to have Boundary_dist in a second table and connect them with an ID, but if you must typically what I do is store it as "1,2,3,4,5" that way in the code I can manipulate that pretty easily with PHP using explode or with mysql using IN.

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