我正在将我所做的 ER 图转换为 SQL 中的表。
如何定义我的外键?
我尝试了两种方法,但我引用的一种方法无法运行,另一种方法也不起作用。
create table Employee (
employeeID int not null ,
employeeName varChar(20) not null ,
employeeAddress varChar(40) not null ,
employeeNumber int not null ,
hireDate date not null ,
employeePosition VarChar(30) not null ,
employeeStatus VarChar(15) not null ,
PRIMARY KEY (employeeID)
);
create table Memberships (
membershipID varChar(20) not null ,
joiningFee float not null ,
discount float not null ,
PRIMARY KEY (membershipID)
);
create table Member (
memberID int not null ,
memberName varChar(20) not null ,
memberAdress varChar(40) not null ,
memberPhone int not null ,
memberGender varChar(20) ,
birthDate date not null ,
dateJoined date not null ,
membershipID int not null ,
PRIMARY KEY (memberID) ,
KEY idx_fk_membershipID (membershipID)
);
create table PurchaseTransaction (
transactionNumber int not null ,
transactionDate date not null ,
itemPurchased varChar(40) ,
quantity int not null ,
pricePerUnit float not null ,
overallTotal float not null ,
PRIMARY KEY (transactionNumber) ,
FOREIGN KEY (memberID) REFERENCES Member (memberID) ,
FOREIGN KEY (employeeID) REFERENCES Employee (employeeID)
);
create table StoreItem (
itemID int not null ,
itemDescription varChar(40) not null ,
standardPrice float not null ,
quantity int not null ,
reorderPoint varChar(40) ,
PRIMARY KEY (itemID) ,
FOREIGN KEY (vendorID) REFERENCES Vendor (vendorID)
);
create table SupplyOrder (
orderNumber int not null ,
datePlaced date not null ,
dateReceived date ,
itemOrdered varChar(40) not null ,
quantityOrdered int not null ,
quantityReceived int ,
costCharged float not null ,
totalCost float not null ,
PRIMARY KEY (orderNumber) ,
FOREIGN KEY (employeeID) REFERENCES Employee (employeeID)
);
create table Vendor (
vendorID int not null ,
companyName varChar(40) not null ,
companyAddress varchar(40) not null ,
companyNumber int not null ,
faxNumber int ,
PRIMARY KEY (vendorID)
);
create table Class (
referenceNumberID int not null ,
employeeID int ,
startDate date not null ,
daysHeld varChar(30) not null ,
timePeriod varChar(30) not null ,
PRIMARY KEY (referenceNumber_ID) ,
FOREIGN KEY (employee_ID) REFERENCES Employee(employee_ID) ,
FOREIGN KEY (roomNumber) REFERENCES Room(roomNumber)
);
create table FitnessCourse (
courseNumberID int not null ,
courseName varChar(30) not null ,
courseDescription varChar(50) ,
courseCategory varChar(30) not null ,
courseDuration time not null ,
courseFee int not null ,
PRIMARY KEY (courseNumberID)
);
create table Room (
roomNumber int not null ,
classCapacity int not null ,
PRIMARY KEY (roomNumberID) ,
FOREIGN KEY (exerciseEquipment) REFERENCES EquipmentType(equipmentDecription)
);
create table EquipmentType (
identificationNumber int not null ,
equipmentDescription varChar(40) not null ,
manufacturerModelNumber int not null ,
maintenenceInterval varChar(40) ,
PRIMARY KEY (identifcationNumber)
);
create table ExerciseEquipment (
serialNumber int not null ,
purchaseDate date not null ,
lastMaintenance date ,
PRIMARY KEY (serialNumnber)
);
create table Manufacturer (
manufacturerID int not null ,
companyName varChar(40) ,
companyAddress varChar(40) ,
companyPhone int not null ,
faxNumber int ,
PRIMARY KEY (manufacturerID)
);
create table WorkOrder (
workOrderID int not null ,
orderDate date not null ,
serialNumber int not null ,
amountCharged float not null ,
PRIMARY KEY (workorderID) ,
FOREIGN KEY (manufacturerID) REFERENCES Manufacturer(manufactuerID)
);
建立外键约束的最简单方法是(可能):只需在编写 CREATE TABLE 语句时将 REFERENCES 内联写入,即与列定义在同一行,例如
create table oneside (
id int primary key
) ;
-- This still does not work in MySQL version 8 ...
create table manyside (
id int primary key
, oneside_id int references oneside( id )
) ;
但是,上述解决方案在 MySQL 中不起作用,尽管它不会导致任何错误(请参阅 Bill Karwin 的评论)。 确保先编写列定义,然后添加外键,例如
create table manyside (
id int primary key
, oneside_id int
, foreign key( oneside_id ) references oneside( id )
) ;
注意列名 oneside_id 在上面的示例中被提及两次。
您正在引用正确的列,例如在 CREATE TABLE PurchaseTransactions 中,但外键列本身不存在(可以说),即您的示例中的 PurchaseTransactions 没有memberID 列或employeeID 列。 因此,这两个外键存在困难。