正确指定时,外键约束不正确

时间:2016-02-18 14:18:05

标签: mysql sql database

所以我创建了一个数据库系统但由于某种原因我得到了这个错误:

  

静态分析:

     

在分析过程中发现了3个错误。

     

预计会有逗号或结束括号。 (靠近"检查"在位置   145)声明开始意外。 (附近" consignmentType" at   156)无法识别的陈述类型。 (靠近" IN"在172位)   SQL查询:

     

CREATE TABLE寄售(trackingID整数NOT NULL PRIMARY KEY   AUTO_INCREMENT,dispatchDate date NOT NULL,输入varchar(10)NOT NULL   CHECK(consignmentType IN(' Collection',' Delivery')),   deliveryAddressID integer NOT NULL,returnAddressID integer NOT NULL,   包整数NOT NULL,FOREIGN KEY(deliveryAddressID)REFERENCES   地址(addressID),​​FOREIGN KEY(returnAddressID)REFERENCES   地址(addressID),​​FOREIGN KEY(包)参考   PackageIDs(packagesID))ENGINE = InnoDB

     

MySQL说:文档

     

。#1005 - 无法创建表courierdbconsignment(错误:150"外键约束形成错误")(详情......)

我到处寻找,我仍然无法找出原因!

这是我的代码:

# Create Category table
CREATE TABLE Category (
    categoryID integer NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    name varchar(255) NOT NULL
) ENGINE=InnoDB;

# Create Package table
CREATE TABLE Package (
    packageID integer NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    price integer NOT NULL, 
    itemName varchar(255) NOT NULL,
    category integer NOT NULL,
    FOREIGN KEY (category)REFERENCES Category(categoryID)
) ENGINE=InnoDB;

# Create DeliveryMethod table
CREATE TABLE DeliveryMethod (
    methodID integer NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    deliveryMethod varchar(7) NOT NULL CHECK (
        deliveryMethod IN ('Bicycle', 'Car', 'Van')
    )
) ENGINE=InnoDB;

# Create Address table
CREATE TABLE Address (
    addressID integer NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    buildingName varchar(255) NOT NULL, 
    streetName varchar(255) NOT NULL, 
    county varchar(255) NOT NULL, 
    postcode varchar(255) NOT NULL
) ENGINE=InnoDB;

/* NORMALIZATION 1 FOR PACKAGES START */
# Create Package table
CREATE TABLE PackageIDs (
    packagesID integer,
    package integer,
    FOREIGN KEY (package)REFERENCES Package(packageID)
) ENGINE=InnoDB;
/* NORMALIZATION END */

# Create Consignment table
CREATE TABLE Consignment (
    trackingID integer NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    dispatchDate date NOT NULL, 
    type varchar(10) NOT NULL CHECK (
        consignmentType IN ('Collection', 'Delivery')
    ), 
    deliveryAddressID integer NOT NULL,
    returnAddressID integer NOT NULL,
    packages integer NOT NULL,
    FOREIGN KEY (deliveryAddressID)REFERENCES Address(addressID),
    FOREIGN KEY (returnAddressID)REFERENCES Address(addressID),
    FOREIGN KEY (packages)REFERENCES PackageIDs(packagesID)
) ENGINE=InnoDB;

/* NORMALIZATION FOR DELIVERY METHOD START */
# Create dMethood table
CREATE TABLE dMethod (
    dMethodID integer,
    dMethods integer,
    FOREIGN KEY (dMethods)REFERENCES DeliveryMethod(methodID)
) ENGINE=InnoDB;
/* NORMALIZATION END */

# Create Branch table
CREATE TABLE Branch (
    branchID integer NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    branchName varchar(255) NOT NULL, 
    headOfficeID integer NOT NULL, 
    managerID integer NOT NULL, 
    deliveryMethods integer NOT NULL,
    address integer NOT NULL,
    FOREIGN KEY (headOfficeID)REFERENCES Branch(branchID),
    /* Cannot add this yet because there is no Employee table! See next queries... */
    FOREIGN KEY (managerID)REFERENCES Employee(staffNo),
    FOREIGN KEY (deliveryMethods)REFERENCES dMethod(dMethodID),
    FOREIGN KEY  (address)REFERENCES Address(addressID)
) ENGINE=InnoDB;

# Create Employee table
CREATE TABLE Employee (
    staffNo integer NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    NIN varchar(9) NOT NULL unique, 
    firstName varchar(255) NOT NULL, 
    lastName varchar(255) NOT NULL, 
    dateOfBirth date NOT NULL, 
    emailAddress varchar(255) NOT NULL, 
    mobileNo integer NOT NULL, 
    salary integer, 
    branchID integer NOT NULL, 
    supervisorID integer NOT NULL,
    address integer NOT NULL,
    FOREIGN KEY (branchID) REFERENCES Branch(branchID),
    FOREIGN KEY (supervisorID) REFERENCES Employee(staffNo),
    FOREIGN KEY (address) REFERENCES Address(addressID)
) ENGINE=InnoDB;

# Add Branch foreign key constraint for managerID
ALTER TABLE Branch ADD FOREIGN KEY (managerID) REFERENCES Employee(staffNo);

# Create Customer table
CREATE TABLE Customer (
    customerID integer NOT NULL PRIMARY KEY AUTO_INCREMENT, 
    firstName varchar(255) NOT NULL, 
    lastName varchar(255) NOT NULL, 
    dateOfBirth date NOT NULL, 
    emailAddress varchar(255) NOT NULL, 
    mobileNo integer NOT NULL, 
    customerBranchID integer NOT NULL, 
    address integer NOT NULL,
    FOREIGN KEY (customerBranchID) REFERENCES Branch(branchID),
    FOREIGN KEY (address) REFERENCES Address(addressID)
) ENGINE=InnoDB;

2 个答案:

答案 0 :(得分:0)

"类型"是MySQL的保留字,你尝试将它用作列名,我建议你使用不同的名称。

答案 1 :(得分:0)

试试这个

A) SELECT nvl(add_months(end_date, 1), SYSDATE)
from programs;

B) SELECT to_date(nvl(SYSDATE - end_date, SYSDATE))
from programs;

C) SELECT nvl(months_between(start_date, end_date), 'ongoing')
from programs;

D) SELECT nvl(to_char(months_between(start_date, end_date)), 'ongoing')
from programs;