0

我一次创建了几个表,我不断收到错误(150):

#1005 - 无法创建表“waget.tour”(错误号:150)

尽管我知道错误是什么,但我根本无法修复它。表“tour”中的所有表引用都存在并且存在,其中引用了外键的键。我已经检查了很多次,根本找不到任何东西。谁能看到我做错了什么?

(创建表“tours”时出错)

CREATE TABLE IF NOT EXISTS tourPayment(
    tourPaymentNumber int,
    tourCost int(7),
    PRIMARY KEY (tourPaymentNumber),
    KEY (tourCost)
);

CREATE TABLE IF NOT EXISTS hotel(
    hotelID int AUTO_INCREMENT,
    hotelName varchar(30),
    PRIMARY KEY (hotelID),
    KEY (hotelName)
);

CREATE TABLE IF NOT EXISTS salutation(
    salutationID int AUTO_INCREMENT,
    salutation varchar(4),
    KEY (salutation)
);

CREATE TABLE IF NOT EXISTS customer(
    custID int AUTO_INCREMENT,
    custSalutation varchar(4),
    custLname varchar(30),
    custAdd varchar(100),
    custPcode varchar(4),
    custState varChar(20),
    custPhone varchar(10),
    custHotel varchar(30),
    PRIMARY KEY (custID),
    FOREIGN KEY (custHotel) REFERENCES hotel(hotelName),
    FOREIGN KEY (custSalutation) REFERENCES salutation(salutation)
);

CREATE  TABLE IF NOT EXISTS bus(
    busID int AUTO_INCREMENT,
    busMake varchar(30),
    busSeats varchar(3),
    PRIMARY KEY (busID)
);

CREATE TABLE IF NOT EXISTS busDriver(
    driverID int AUTO_INCREMENT,
    driverName varchar(20),
    driverEmail varchar(40),
    busID int,
    PRIMARY KEY (driverID),
    FOREIGN KEY (busID) REFERENCES bus(busID)
);

CREATE TABLE IF NOT EXISTS tour(
    DailyTourID int AUTO_INCREMENT,
    NoOfPeople int(3),
    tourDate DATE,
    tourTime TIME,
    tourName varchar(30),
    tourCost int(7),
    tourDriverID varchar(20),
    PRIMARY KEY (DailyTourID),
    FOREIGN KEY (tourDriverID) REFERENCES busDriver(driverID),
    FOREIGN KEY (tourCost) REFERENCES tourPayment(tourCost)
);

CREATE TABLE IF NOT EXISTS TourCustLink(
    TourCustLinkID int AUTO_INCREMENT,
    TourID int,
    custID int,
    PRIMARY KEY (TourCustLinkID),
    FOREIGN KEY (TourID) REFERENCES tour(DailyTourID),
    FOREIGN KEY (custID) REFERENCES customer(custID)
);
4

4 回答 4

3

这是你的问题:

FOREIGN KEY (tourDriverID) REFERENCES busDriver(driverID)

引用一个 INT 但被声明为 varchar(20)

于 2013-04-19T11:31:33.153 回答
0

AUTO_INCREMENT 字段都需要是 PRIMARY KEY 的,所以尝试将称呼改为

CREATE TABLE IF NOT EXISTS salutation(
    salutationID int AUTO_INCREMENT,
    salutation varchar(4),
    PRIMARY KEY (salutationID),
    KEY (salutation)
);

此外,在游览中,您的 tourDriverID 数据类型错误,它应该是 INT 以匹配引用的键

CREATE TABLE IF NOT EXISTS tour(
    DailyTourID int AUTO_INCREMENT,
    NoOfPeople int(3),
    tourDate DATE,
    tourTime TIME,
    tourName varchar(30),
    tourCost int(7),
    tourDriverID INT,
    PRIMARY KEY (DailyTourID),
    FOREIGN KEY (tourDriverID) REFERENCES busDriver(driverID),
    FOREIGN KEY (tourCost) REFERENCES tourPayment(tourCost)
);

完整的 SQL 小提琴示例可以在这里找到:http ://sqlfiddle.com/#!2/840b9

于 2013-04-19T11:29:53.513 回答
0

实际上,尝试一张一张地创建表格很容易,您会收到更好的错误消息。您的问题是表格salutation,因为您必须定义 AUTO_INCREMENT 字段(在您的情况下是salutationID) as KEY and not the field称呼`

CREATE TABLE IF NOT EXISTS salutation(
   salutationID int AUTO_INCREMENT,
   salutation varchar(4),
   KEY (salutationID) -- here is your error
);
于 2013-04-19T11:29:54.993 回答
0

这是你运行的代码吗?如果是这样,则错误是关于需要成为键的 AUTO_INCREMENT。

CREATE TABLE IF NOT EXISTS salutation(
    salutationID int AUTO_INCREMENT,
    salutation varchar(4),
    KEY (salutation)
);

尝试:

CREATE TABLE IF NOT EXISTS salutation(
    salutationID int AUTO_INCREMENT,
    salutation varchar(4),
    KEY (salutationID)
);

并根据需要制作其他外键。

干杯。

于 2013-04-19T11:30:24.943 回答