【问题标题】:Creating a table with a primary key that has one to may relationship with an existing table in oracle创建一个具有主键的表,该表与 oracle 中的现有表有一个可能的关系
【发布时间】:2013-10-21 17:09:58
【问题描述】:

我有一个表 t1,其中包含许多以下记录

ID NAME TITLE ....

1  abc  X1
2  xyz  X1
3  qwe  X1
4  asd  X2
5  pqr  X2

我想创建一个新表 t2,它有一个主键,它在 t1 中引用了一个新的外键。此外,“标题”列的信息在 t2 中是不同的,因此,在 t2 中添加的任何记录都应反映在 t1 中,删除应从 t1 级联到 t2。

我做了以下事情:

create table t2 AS SELECT distinct (title) FROM t1;

ALTER TABLE t2
ADD (s_id,column1,column 2);

alter table t2 add primary key (s_id);

create sequence s_seq
start with 1
increment by 1
nomaxvalue;

update t2 set s_id=s_seq.nextval;


ALTER TABLE t1
add constraint fk_s_id
FOREIGN KEY (f_t2)
REFERENCES t2(s_id) on delete cascade;

这不适用于级联和插入。 请帮忙。

【问题讨论】:

    标签: sql oracle foreign-keys alter


    【解决方案1】:

    我错过了您如何创建 f_t2,它需要在某一时刻添加到 t1。所以我添加并填充了f_t2 列,它起作用了。

    ALTER TABLE t1
    ADD (f_t2 number);
    update t1 set f_t2 = (select s_id from t2 where t2.title = t1.title);
    

    运行以下产生了预期的结果。

    drop table t1;
    create table t1 (id number,name varchar2(100), title varchar2(100));
    insert into t1 values (1, 'abc',  'X1');
    insert into t1 values (2, 'xyz',  'X1');
    insert into t1 values (3, 'qwe',  'X1');
    insert into t1 values (4, 'asd',  'X2');
    insert into t1 values (5, 'pqr',  'X2');
    drop table t2;
    create table t2 AS SELECT distinct (title) FROM t1;
    ALTER TABLE t2
    ADD (s_id number,column1 varchar2(10),column2 varchar2(20));
    drop sequence s_seq;
    create sequence s_seq
    start with 1
    increment by 1
    nomaxvalue;
    update t2 set s_id=s_seq.nextval;
    alter table t2 add primary key (s_id);
    ALTER TABLE t1
    ADD (f_t2 number);
    update t1 set f_t2 = (select s_id from t2 where t2.title = t1.title);
    select * from t1;
    select * from t2;
    ALTER TABLE t1
    add constraint fk_s_id
    FOREIGN KEY (f_t2)
    REFERENCES t2(s_id) on delete cascade;
    delete from t2 where title='X2';
    select * from t1;
    select * from t2;
    

    输出:

    table T1 dropped.
    table T1 created.
    1 rows inserted.
    1 rows inserted.
    1 rows inserted.
    1 rows inserted.
    1 rows inserted.
    table T2 dropped.
    table T2 created.
    table T2 altered.
    sequence S_SEQ dropped.
    sequence S_SEQ created.
    2 rows updated.
    table T2 altered.
    table T1 altered.
    5 rows updated.
            ID NAME                                                                                                 TITLE                                                                                                      F_T2
    ---------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ----------
             1 abc                                                                                                  X1                                                                                                            1 
             2 xyz                                                                                                  X1                                                                                                            1 
             3 qwe                                                                                                  X1                                                                                                            1 
             4 asd                                                                                                  X2                                                                                                            2 
             5 pqr                                                                                                  X2                                                                                                            2 
    
    TITLE                                                                                                      S_ID COLUMN1    COLUMN2            
    ---------------------------------------------------------------------------------------------------- ---------- ---------- --------------------
    X1                                                                                                            1                                 
    X2                                                                                                            2                                 
    
    table T1 altered.
    1 rows deleted. --- DELETE in T2 affects T1
    -- THIS IS T1:
                ID NAME                                                                                                 TITLE                                                                                                      F_T2
        ---------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ----------
                 1 abc                                                                                                  X1                                                                                                            1 
                 2 xyz                                                                                              
    
        X1                                                                                                            1 
                 3 qwe                                                                                                  X1                                                                                                            1 
    -- THIS IS T2:    
        TITLE                                                                                                      S_ID COLUMN1    COLUMN2            
        ---------------------------------------------------------------------------------------------------- ---------- ---------- --------------------
        X1                                                                                                            1                                 
    

    【讨论】:

    • 谢谢..但问题已部分解决。我已经填充了表 t1..所以来自 t2 的插入不会返回到 t1。删除确实有效,但是。
    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 2011-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多