Use snhpinventorytest;
CREATE TABLE Persons (
PersonID INT(11) unsigned NOT NULL auto_increment,
PRIMARY KEY (PersonID),
FirstName Varchar (50),
Lastname varchar(50)
) ENGINE=INNODB;
Use snhpinventorytest;
CREATE TABLE if not exists Locations (
LocationID INT(11) UNSIGNED NOT NULL auto_increment,
PRIMARY KEY (LocationID),
PersonID int(11) unsigned,
INDEX PersonID_IDX (PersonID),
BuildingName varchar(50),
LocationType varchar(50),
RoomNumber varchar (20),
FOREIGN KEY (FK_PersonID) REFERENCES Person(PersonID)
ON DELETE CASCADE
ON UPDATE CASCADE
) ENGINE=INNODB;
Use snhpinventorytest;
CREATE TABLE if not exists assets (
AssetID INT(11) UNSIGNED NOT NULL auto_increment,
PRIMARY KEY (AssetID),
PersonID int(11) unsigned,
INDEX PersonID_IDX (PersonID),
FOREIGN KEY (FK_PersonID) REFERENCES Person(PersonID)
ON DELETE CASCADE
ON UPDATE CASCADE
) Engine=INNODB;
我是MySQL新手。使用Workbench 6.2 我可以创建Person表,但不能创建Locations或Assets表 1005和(错误150)建议表示赞赏。我认为围绕外键的语句顺序可能不正确,但不确定如何?
答案 0 :(得分:0)
您必须更改代码才能正常工作。
您需要一个CONSTRAINT名称并使其成为独特的名称。然后你需要提供正确的FOREIGN KEY而不是任何名字。
例如,FOREIGN KEY
(FK_PersonID)应为FOREIGN KEY (PersonID)
也
你不需要使用USE snhpinventorytest;
3次:)你可以使用它一次。
我希望这会有所帮助
试试此代码
USE snhpinventorytest;
CREATE TABLE Persons (
PersonID INT(11) unsigned NOT NULL auto_increment,
PRIMARY KEY (PersonID),
FirstName Varchar (50),
Lastname varchar(50)
) ENGINE=INNODB;
CREATE TABLE locations (
LocationID int(11) unsigned NOT NULL AUTO_INCREMENT,
PersonID int(11) unsigned DEFAULT NULL,
BuildingName varchar(50) DEFAULT NULL,
LocationType varchar(50) DEFAULT NULL,
RoomNumber varchar(20) DEFAULT NULL,
PRIMARY KEY (LocationID),
KEY PersonID_IDX (PersonID),
CONSTRAINT FK_PersonID1 FOREIGN KEY (PersonID) REFERENCES persons (PersonID) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE IF NOT EXISTS assets (
AssetID INT(11) UNSIGNED NOT NULL auto_increment,
PRIMARY KEY (AssetID),
PersonID int(11) unsigned,
INDEX PersonID_IDX (PersonID),
CONSTRAINT FK_PersonID2 FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)
ON DELETE CASCADE
ON UPDATE CASCADE
) Engine=INNODB;