简体   繁体   English

带有外键的 SQL 语法

[英]SQL syntax with FOREIGN KEY

I have the following tables:我有以下表格:

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
)

and it thow a err:- You have an error in your SQL syntax;它有一个错误:-您的 SQL 语法有错误; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'CONSTRAINT fs_rppermission FOREIGN KEY检查与您的 MariaDB 服务器版本相对应的手册,了解在 'CONSTRAINT fs_rppermission FOREIGN KEY 附近使用的正确语法

what's the problem?有什么问题?

Consider:考虑:

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
);

Rationale:理由:

  • there are missing commas all over your statement您的语句中缺少逗号

  • the name of the foreign should not be surrounded with parentheses外国人的名字不应该被括号包围

  • ... proper formatting makes the statement easier to write and read ... 正确的格式使语句更易于编写和阅读

Demo on DB Fiddle DB Fiddle 上的演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM