Sql(Oracle):插入重复键

时间:2018-05-16 10:34:59

标签: sql oracle

哈利是巫师,他创造了一个名为“戒指团契”的团体。哈里的精灵号是77.

INSERT INTO group VALUES ('the fellowship of the ring','01');
INSERT INTO activity VALUES ('01','77','the fellowship of the ring’);

Gondor也想加入该组织和activity01。

从表中我们知道“wizardno”是向导表的主键。

“groupname”是组表的主键。“acitvityno”是组表的UF键。

“acitvityno”是活动表的主键。

“groupname”是活动表的外键。 “wizardno”是活动表的UF键.in

将Gondor加入Harry的小组。 Gondor的向导编号是99。

我用

insert into activity values (’01’,’99’,’the fellowship of the ring’);

得到错误: 原因:UPDATE或INSERT语句尝试插入重复键。 对于在DBMS MAC模式下配置的受信任Oracle,如果存在不同级别的重复条目,则可能会看到此消息。 *操作:删除唯一限制或不插入密钥。

但它必须仍然是唯一的限制。

drop table wizard;
drop table activity;
drop table group; 

CREATE TABLE wizard (
    wizardno          NUMBER(2) NOT NULL,
    wizardname        VARCHAR2(30) NOT NULL,
   );

ALTER TABLE wizard ADD CONSTRAINT wizard_pk PRIMARY KEY ( wizardno );
CREATE TABLE group (
    groupname        VARCHAR2(30) NOT NULL,
    activityno         NUMBER(2) NOT NULL
);

ALTER TABLE group ADD CONSTRAINT group_pk PRIMARY KEY ( groupname );


CREATE TABLE activity (
    activityno        NUMBER(2) NOT NULL,
    wizardno          CHAR(2) NOT NULL,
    groupname         VARCHAR2(30)
);   

ALTER TABLE activity 
   ADD CONSTRAINT activity_pk PRIMARY KEY ( activityno );

ALTER TABLE activity
    ADD CONSTRAINT activity_wizard_fk FOREIGN KEY ( wizardno )
        REFERENCES wizard ( wizardno );
ALTER TABLE  activity
    ADD CONSTRAINT activity_group_fk FOREIGN KEY ( groupname )
        REFERENCES group ( groupname );

2 个答案:

答案 0 :(得分:1)

  • 请勿使用保留字group作为表名
  • 对字符串类型变量使用正确的单引号''
  • 您需要为查找表插入相应的值( 在这种情况下wizard)你引用了正确的完整性 表格。

  • 对于外键和主键关系,两个引用列都在 两个表应该是相同的类型(您可以将列wizardno的类型从CHAR(2)转换为activity表中的数字(2)。

  • 主键列的值不应重复(此处为 约束名称为activity_pk

因此可以使用以下命令集:

drop table wizard;
drop table activity;
drop table group_; 

CREATE TABLE wizard (
    wizardno          NUMBER(2) NOT NULL,
    wizardname        VARCHAR2(30) NOT NULL
   );

ALTER TABLE wizard ADD CONSTRAINT wizard_pk PRIMARY KEY ( wizardno );
CREATE TABLE group_ (
    groupname        VARCHAR2(30) NOT NULL,
    activityno         NUMBER(2) NOT NULL
);

ALTER TABLE group_ ADD CONSTRAINT group_pk PRIMARY KEY ( groupname );


CREATE TABLE activity (
    activityno        NUMBER(2) NOT NULL,
    wizardno          NUMBER(2) NOT NULL,
    groupname         VARCHAR2(30)
);   

ALTER TABLE activity 
   ADD CONSTRAINT activity_pk PRIMARY KEY ( activityno );

ALTER TABLE activity
    ADD CONSTRAINT activity_wizard_fk FOREIGN KEY ( wizardno )
        REFERENCES wizard ( wizardno );
ALTER TABLE  activity
    ADD CONSTRAINT activity_group_fk FOREIGN KEY ( groupname )
        REFERENCES group_ ( groupname );
INSERT INTO wizard VALUES ( 77, 'Abc');
INSERT INTO wizard VALUES ( 99, 'Def');


INSERT INTO group_ VALUES ('the fellowship of the ring','01');
INSERT INTO activity VALUES ('01',77,'the fellowship of the ring');        
INSERT INTO activity VALUES ('02',99,'the fellowship of the ring'); 

答案 1 :(得分:1)

  • 您不应将<img src="Item"> <img src="{{Item}}"> <img [src]="Item"> <img [src]="'Item'"> <img [src]="{{Item}}"> <img [src]="'{{Item}}'"> 关键字用作表名。相反,您可以复数表名称(GROUPwizardsgroups)。
  • activities是向导表中的wizardno类型,当活动表中有NUMBER(2)时,它们之间存在外键引用。它们应该是相同的类型。
  • 您无需在活动表中复制CHAR(2)
  • 如果您不希望在每个组中允许重复的向导,那么活动表上的主键应该是向导和组表的主键上的复合键。
  • 您可以使用序列生成唯一的主键值。

像这样:

SQL Fiddle

Oracle 11g R2架构设置

groupname

查询1

CREATE TABLE wizards (
  wizardno    NUMBER(2)
              CONSTRAINT wizards_wizardno_pk PRIMARY KEY,
  wizardname  VARCHAR2(30)
              CONSTRAINT wizards_wizardname_nn NOT NULL
);

CREATE SEQUENCE wizards_wizardno_seq;

CREATE TABLE groups (
  groupname VARCHAR2(30)
            CONSTRAINT groups_groupname_u UNIQUE
            CONSTRAINT groups_groupname_nn NOT NULL,
  groupno   NUMBER(2)
            CONSTRAINT groups_groupno_pk PRIMARY KEY
);

CREATE SEQUENCE groups_groupno_seq;

CREATE TABLE activities (
    activityno NUMBER(2)
               CONSTRAINT activities_activityno_pk PRIMARY KEY,
    groupno    NUMBER(2)
               CONSTRAINT activities_groupno_nn NOT NULL
               CONSTRAINT activities_groupno_fk REFERENCES groups ( groupno ),
    wizardno   NUMBER(2)
               CONSTRAINT activities_wizardno_nn NOT NULL
               CONSTRAINT activities_wizardno_fk REFERENCES wizards ( wizardno ),
    CONSTRAINT activities_group_wizard_u UNIQUE ( groupno, wizardno )
);   

CREATE SEQUENCE activities_activityno_seq;

INSERT INTO wizards ( wizardno, wizardname )
VALUES ( wizards_wizardno_seq.NEXTVAL, 'Harry' );

INSERT INTO wizards ( wizardno, wizardname )
VALUES ( wizards_wizardno_seq.NEXTVAL, 'Gondor' );

INSERT INTO groups ( groupname, groupno )
VALUES ( 'the fellowship of the ring', groups_groupno_seq.NEXTVAL );

INSERT INTO activities ( activityno, groupno, wizardno )
VALUES ( activities_activityno_seq.NEXTVAL, 1, 1 );

INSERT INTO activities ( activityno, groupno, wizardno )
VALUES ( activities_activityno_seq.NEXTVAL, 1, 2 );

<强> Results

SELECT wizardname,
       groupname
FROM   wizards w
       INNER JOIN activities a
       ON ( w.wizardno = a.wizardno )
       INNER JOIN groups g
       ON ( a.groupno = g.groupno )