哈利是巫师,他创造了一个名为“戒指团契”的团体。哈里的精灵号是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 );
答案 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}}'">
关键字用作表名。相反,您可以复数表名称(GROUP
,wizards
,groups
)。activities
是向导表中的wizardno
类型,当活动表中有NUMBER(2)
时,它们之间存在外键引用。它们应该是相同的类型。CHAR(2)
。像这样:
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 )