SQL 外键

问题描述 投票:0回答:1

所以我目前正在做一个期末项目,创建一个我在 SQL 上的表中制作的 ERD 图。我这门课的老师并不是最好的,让我只能自己照顾自己。有人可以帮助我如何正确定义我的外键吗?我尝试了两种不同的方法,但我引用的方法并不有趣,而另一种方法则无法正常工作。如果这很愚蠢,我很抱歉,我对 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)
    );
sql mysql foreign-keys
1个回答
0
投票

建立外键约束的最简单方法是(可能):只需在编写 CREATE TABLE 语句时将 REFERENCES 内联写入,即与列定义在同一行,例如

create table oneside (
  id int primary key
) ;

create table manyside (
  id int primary key
, oneside_id int references oneside( id )
) ;

您正在引用正确的列,例如在 CREATE TABLE PurchaseTransactions 中,但外键列本身不存在(可以说),即您的示例中的 PurchaseTransactions 没有memberID 列或employeeID 列。 因此,这两个外键存在困难。

dbfiddle

© www.soinside.com 2019 - 2024. All rights reserved.