两个表具有相同的顺序

发布于 2024-12-02 14:27:19 字数 275 浏览 9 评论 0原文

是否可以有两个具有相同递增顺序的表?

我试图用 ID、NAME、ParentID 创建一棵树,并且必须连接两个表。 如果我有不同的 id,ID - ParentId 的树形方案将不起作用。

     Table A                Table B
  ID | NAME | PID       ID | NAME | PID
  1  | xpto | 0          1 | xpto | 1

Is possible to have two tables with the same incrementing sequence?

I was trying to do a tree with ID, NAME, ParentID and i have to join two tables.
If i have different id the tree scheme of ID - ParentId will not work.

     Table A                Table B
  ID | NAME | PID       ID | NAME | PID
  1  | xpto | 0          1 | xpto | 1

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

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

发布评论

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

评论(4

花之痕靓丽 2024-12-09 14:27:19

如果同时执行两个插入,则可以使用 SEQUENCE.NEXTVAL 插入到第一个表中以获取新 ID,然后使用 SEQUENCE.CURRVAL 插入到第二个表中以重用相同的 ID。

If you are doing both inserts at the same time, you can use SEQUENCE.NEXTVAL for the insert into the first table to get a new ID, and then SEQUENCE.CURRVAL for the insert into the second table to reuse the same ID.

爱的那么颓废 2024-12-09 14:27:19

我找到了答案:“序列号是独立于表生成的,因此相同的序列可以用于一个或多个表。”

http://download.oracle.com/docs/cd /B19306_01/server.102/b14200/statements_6015.htm

坦克为您提供帮助。

I found the answer: "Sequence numbers are generated independently of tables, so the same sequence can be used for one or for multiple tables."

http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/statements_6015.htm

Tanks for your help.

幸福丶如此 2024-12-09 14:27:19

您可以有一个主表,它只是序列 PK/FK,然后有两个子表。在主表中插入一行只是为了获取序列,然后使用该序列作为子表中的 PK。如果子表具有相同的顺序,那么为什么不是一张表呢?

You could have a master table that is nothing but the sequence PK/FK and then have two child tables. Insert a row in the master just to get the sequence and then use that sequence as the PK in the child tables. If the child tables have the same sequence then why is not one table?

〆一缕阳光ご 2024-12-09 14:27:19
create sequence v_seq
INCREMENT by 1
minvalue 1
maxvalue 10;

示例图片

create table v_t_s_emp(v_id number,vname varchar2(10));
insert into v_t_s_emp values(v_seq.nextval,'krishna');
create table v_t_s_emp1(v_id number,vname varchar2(10));
insert into v_t_s_emp1 values(v_seq.nextval,'RAMesh');
commit;

select * from v_t_s_emp
union all
select * from v_t_s_emp1;
create sequence v_seq
INCREMENT by 1
minvalue 1
maxvalue 10;

Sample Image

create table v_t_s_emp(v_id number,vname varchar2(10));
insert into v_t_s_emp values(v_seq.nextval,'krishna');
create table v_t_s_emp1(v_id number,vname varchar2(10));
insert into v_t_s_emp1 values(v_seq.nextval,'RAMesh');
commit;

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