我想調試一個簡單的SQL數據庫。我不斷收到寫着外鍵約束錯誤
SQL query:
CREATE TABLE Groups (
groupId int(11) NOT NULL AUTO_INCREMENT,
leaderId int(11) NOT NULL COLLATE utf8_unicode_ci,
sportId int(11) NOT NULL COLLATE utf8_unicode_ci,
groupName varchar(255) COLLATE utf8_unicode_ci,
membersName varchar(255) COLLATE utf8_unicode_ci,
groupDes TEXT CHARACTER SET latin1 COLLATE latin1_general_cs,
dateCreated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (groupId),
FOREIGN KEY (sportId) REFERENCES Sports(sportId),
FOREIGN KEY (leaderId) REFERENCES Users(userId)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
MySQL said: Documentation
#1215 - Cannot add foreign key constraint
我知道,這是外鍵(sportId)參考文獻體育(sportId),行,因爲當我刪除它,我沒有錯誤,一切都很好錯誤。我的運動桌上可能有些問題,或者只是語法錯誤,但我似乎無法看到它。有人請告訴我我瘋了,它是一個簡單的語法問題。這是我正在寫的完整數據庫文件。
DROP DATABASE if EXISTS sqlfile;
CREATE DATABASE sqlfile;
USE sqlfile;
DROP TABLE if EXISTS Users;
CREATE TABLE Users (
userId int(11) NOT NULL AUTO_INCREMENT,
userName varchar (255) UNIQUE NOT NULL COLLATE utf8_unicode_ci,
password varchar(255) COLLATE utf8_unicode_ci,
firstName varchar(255) COLLATE utf8_unicode_ci,
lastName varchar(255) COLLATE utf8_unicode_ci,
tel char(10) COLLATE utf8_unicode_ci,
dateCreated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (userId)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE Groups (
groupId int(11) NOT NULL AUTO_INCREMENT,
leaderId int(11) NOT NULL COLLATE utf8_unicode_ci,
sportId int(11) NOT NULL COLLATE utf8_unicode_ci,
groupName varchar(255) COLLATE utf8_unicode_ci,
membersName varchar(255) COLLATE utf8_unicode_ci,
groupDes TEXT CHARACTER SET latin1 COLLATE latin1_general_cs,
dateCreated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (groupId),
FOREIGN KEY (sportId) REFERENCES Sports(sportId),
FOREIGN KEY (leaderId) REFERENCES Users(userId)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE Members (
memberId int(11) NOT NULL AUTO_INCREMENT,
groupId int(11) NOT NULL COLLATE utf8_unicode_ci,
userId int(11) NOT NULL COLLATE utf8_unicode_ci,
dateCreated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (memberId),
FOREIGN KEY (groupId) REFERENCES Groups(groupId),
FOREIGN KEY (userId) REFERENCES Users(userId)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE Sports (
sportId int(11) NOT NULL AUTO_INCREMENT,
sportName varchar(255) COLLATE utf8_unicode_ci,
dateCreated TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (sportId)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
工作。我對sql很新,謝謝! –