好的,所以我目前正在为大学作业开发 Oracle 11G 速成版。我遇到了一个关于如何在以下父表中自动递增和更新的问题。所以我有一个地址表,然后是一个城市表。这里是地址SQL代码
create table address(
addressid int primary key,
cityid int,
countyid int,
streetnameid int,
postcodeid int,
doornumid int,
natid int,
foreign key (doornumid) references doornum,
foreign key (postcodeid) references postcode,
foreign key (streetnameid) references streetname,
foreign key (countyid) references county,
foreign key (cityid) references city,
foreign key (natid) references nat);
如您所见,我将城市表作为外键引用,这是下面的城市 SQL 表代码:
create table city(
cityid int primary key,
city varchar(45));
插入东西时,城市代码使用序列自动递增:
create SEQUENCE seq_cityID
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 11;
所以很简单,我通过这样的城市 ID 自动递增所有输入:
INSERT INTO city (cityid, city)
values(seq_cityID.nextval, Oxford);
INSERT INTO city (cityid, city)
values(seq_cityID.nextval, Oxford);
但我的问题是,当我在其父表(例如地址表)中引用它时,我如何引用该行数据的 ID 并确保提取正确的 ID 而无需手动输入?
INSERT INTO address (addressid, cityid, countyid, streetnameid, postcodeid, doornumid, natid)
values(seq_addressID.nextval, seq_cityID.nextval, seq_countyID.nextval, seq_streetnameID.nextval, seq_postcodeID.nextval, seq_doornumID.nextval, seq_natID.nextval);
INSERT INTO address (addressid, cityid, countyid, streetnameid, postcodeid, doornumid, natid)
values(seq_addressID.nextval, seq_cityID.nextval, seq_countyID.nextval, seq_streetnameID.nextval, seq_postcodeID.nextval, seq_doornumID.nextval, seq_natID.nextval);
这是简单的插入地址,目前仅引用 nextval,但我不相信它会从该 ID 中提取该行数据。我如何有效地从子表中提取该 ID 并自动将其正确放入父表中?