mysql - ALTER TABLE 添加约束

标签 mysql sql ddl alter-table

用户和属性表已正确创建

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 不起作用。

ALTER TABLE User 
(
    ADD CONSTRAINT userPropertie
    FOREIGN KEY(properties)
    REFERENCES Properties(ID)
)

不知道为什么?

我以此为引用 http://www.w3schools.com/sql/sql_foreignkey.asp

Error 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '( ADD CONSTRAINT userPropertie FOREIGN KEY(properties) REFERENCES Properties(' at line 2

最佳答案

省略括号:

ALTER TABLE User 
    ADD CONSTRAINT userProperties
    FOREIGN KEY(properties)
    REFERENCES Properties(ID)

关于mysql - ALTER TABLE 添加约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10193153/

相关文章:

sql - SQL 表与结构数组有何不同? (就用法而言,而不是实现)

php - 将 SQL 语句从 USING 重写为 ON

php - 创建简单的 MySQL 更新触发器时出现问题

sql - 具有优先级属性的表连接

mysql - 如何创建MySQL分层递归查询

sql - 增加 SQL Server 中的 NVARCHAR 列大小会导致索引重建吗?

php - 使用单个查询更新 MySQL 表中的所有行

MySQL查询、计数问题

mysql - 插入一个值如果在连接表中找到匹配项

mysql - SQL 约束检查 <>