0

Staff 表引用了 branch 表

CREATE TABLE Staff(
StaffNo VARCHAR(5) NOT NULL,
firstName VARCHAR(15) NOT NULL UNIQUE,
lastName VARCHAR(15) NOT NULL,
position VARCHAR(10) NOT NULL,
salary INTEGER
DEFAULT 3000,
CHECK (salary BETWEEN 3000 AND 25000),
email VARCHAR(25),
branchNo CHAR(6) NOT NULL,
PRIMARY KEY (StaffNo),
FOREIGN KEY (branchNo) REFERENCES Branch (branchNo));

同时分支表引用了 Staff 表

create table Branch(
branchNo char(6) not null primary key,
street varchar(30) not null,
city varchar(20),
postCode char(5) not null,
ManagerNo varchar(5) not null,
foreign key (ManagerNo) references Staff(StaffNo)); 
4

2 回答 2

5

由于您的表在外键中相互引用,如果尚未创建另一个表,则在创建表时都会出错。我建议您删除创建 FOREIGN KEYs 以分隔 ALTER TABLE 语句:

CREATE TABLE Staff(
  StaffNo VARCHAR(5) NOT NULL,
  firstName VARCHAR(15) NOT NULL UNIQUE,
  lastName VARCHAR(15) NOT NULL,
  position VARCHAR(10) NOT NULL,
  salary INTEGER
  DEFAULT 3000,
  CHECK (salary BETWEEN 3000 AND 25000),
  email VARCHAR(25),
  branchNo CHAR(6) NOT NULL,
  PRIMARY KEY (StaffNo)
);


create table Branch(
  branchNo char(6) not null primary key,
  street varchar(30) not null,
  city varchar(20),
  postCode char(5) not null,
  ManagerNo varchar(5) not null
); 

alter table staff
  add constraint fk1_branchNo foreign key (branchNo) references Branch (branchNo);

alter table branch
  add constraint fk1_ManagerNo foreign key (ManagerNo) references Staff (StaffNo);

请参阅带有演示的 SQL Fiddle

于 2013-05-02T13:22:48.800 回答
1

您可以从一个表中删除一个引用并保留另一个。然后您可以使用剩余的引用检索数据。这有什么问题吗?

于 2013-05-02T13:06:38.003 回答