SQL 带外键的语法
SQL syntax with FOREIGN KEY
我有以下表格:
CREATE TABLE
IF NOT EXISTS {}.roles_permissions
(
role_id INT
(12) NOT NULL,
permission_id INT
(12) NOT NULL,
UNIQUE KEY
(role_id,permission_id)
CONSTRAINT `fk-rprole` FOREIGN KEY
(`role_id`)
REFERENCES `roles`
(`id`)
ON
DELETE CASCADE
ON
UPDATE CASCADE
CONSTRAINT (`fs_rppermission`) FOREIGN KEY
(`permission_id`)
REFERENCES `permissions`
(`id`)
ON
DELETE CASCADE
ON
UPDATE CASCADE
)
它报错了:- 你的 SQL 语法有误;查看与您的 MariaDB 服务器版本对应的手册,了解在 'CONSTRAINT fs_rppermission
附近使用的正确语法
外键
有什么问题?
考虑:
CREATE TABLE IF NOT EXISTS roles_permissions (
role_id INT(12) NOT NULL,
permission_id INT(12) NOT NULL,
UNIQUE KEY (role_id,permission_id),
CONSTRAINT `fk-rprole` FOREIGN KEY (`role_id`)
REFERENCES `roles`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fs_rppermission` FOREIGN KEY (`permission_id`)
REFERENCES `permissions` (`id`)
ON DELETE CASCADE ON UPDATE CASCADE
);
理由:
你的陈述中缺少逗号
外国名字不用括号
...适当的格式使语句更易于编写和阅读
我有以下表格:
CREATE TABLE
IF NOT EXISTS {}.roles_permissions
(
role_id INT
(12) NOT NULL,
permission_id INT
(12) NOT NULL,
UNIQUE KEY
(role_id,permission_id)
CONSTRAINT `fk-rprole` FOREIGN KEY
(`role_id`)
REFERENCES `roles`
(`id`)
ON
DELETE CASCADE
ON
UPDATE CASCADE
CONSTRAINT (`fs_rppermission`) FOREIGN KEY
(`permission_id`)
REFERENCES `permissions`
(`id`)
ON
DELETE CASCADE
ON
UPDATE CASCADE
)
它报错了:- 你的 SQL 语法有误;查看与您的 MariaDB 服务器版本对应的手册,了解在 'CONSTRAINT fs_rppermission
附近使用的正确语法
外键
有什么问题?
考虑:
CREATE TABLE IF NOT EXISTS roles_permissions (
role_id INT(12) NOT NULL,
permission_id INT(12) NOT NULL,
UNIQUE KEY (role_id,permission_id),
CONSTRAINT `fk-rprole` FOREIGN KEY (`role_id`)
REFERENCES `roles`(`id`)
ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fs_rppermission` FOREIGN KEY (`permission_id`)
REFERENCES `permissions` (`id`)
ON DELETE CASCADE ON UPDATE CASCADE
);
理由:
你的陈述中缺少逗号
外国名字不用括号
...适当的格式使语句更易于编写和阅读