CREATE TABLE Properties
(
ID int AUTO_INCREMENT,
language int,
stonecolor int,
gamefield int,
UserID int,
PRIMARY KEY(ID),
FOREIGN KEY(language) REFERENCES Language(ID),
FOREIGN KEY(stonecolor) REFERENCES StoneColor(ID),
FOREIGN KEY(gamefield) REFERENCES GameField(ID)
) ENGINE = INNODB;
CREATE TABLE User
(
ID int AUTO_INCREMENT,
vorname varchar(30) NOT NULL,
name varchar(30) NOT NULL,
email varchar(40) NOT NULL,
password varchar(40) NOT NULL,
nickname varchar(15) NOT NULL,
score int,
isadmin int DEFAULT 0,
gamesPlayed int,
properties int NOT NULL,
PRIMARY KEY(ID),
UNIQUE (email),
UNIQUE (nickname)
) ENGINE = INNODB;
ALTER TABLE User
(
ADD CONSTRAINT userPropertie
FOREIGN KEY(properties)
REFERENCES Properties(ID)
)
表格已正确创建,但ALTER TABLE不起作用。无法弄清楚为什么?
我用它作为参考http://www.w3schools.com/sql/sql_foreignkey.asp
错误1064 - 您的SQL语法出错;查看与您的MySQL服务器版本对应的手册,以便在'附近使用正确的语法'( 添加CONSTRAINT userPropertie FOREIGN KEY(属性) REFERENCES属性('第2行
答案 0 :(得分:73)
省略括号:
ALTER TABLE User
ADD CONSTRAINT userProperties
FOREIGN KEY(properties)
REFERENCES Properties(ID)
答案 1 :(得分:0)
ALTER TABLE `User`
ADD CONSTRAINT `user_properties_foreign`
FOREIGN KEY (`properties`)
REFERENCES `Properties` (`ID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
答案 2 :(得分:0)
alter table User
add constraint userProperties
foreign key (properties)
references Properties(ID)