尝试运行以下安装脚本时出现150错误。 怎么了?
serverPermissions似乎触发了错误,但我不确定为什么。 据我所知,我认为外键约束都已正确映射。我可能遗漏了一些明显的东西。
-- Setup my members database
DROP DATABASE IF EXISTS mymembers;
CREATE DATABASE IF NOT EXISTS mymembers;
/*
* The list of all members in the community
*/
DROP TABLE IF EXISTS members;
CREATE TABLE members
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30),
steamID3 INT NOT NULL UNIQUE,
PRIMARY KEY (id)
);
/*
* Lists all servers in operation
*/
DROP TABLE IF EXISTS servers;
CREATE TABLE servers
(
sid INT NOT NULL AUTO_INCREMENT,
sname VARCHAR(30), -- name of server (short name)
PRIMARY KEY(sid)
);
/*
* Maps a member and a server together along with a flag.
* Record (0, 12, 1, 0) indicates member 12 has flag 0 on server 1.
*/
DROP TABLE IF EXISTS serverPermissions;
CREATE TABLE serverPermissions
(
mid INT,
sid INT,
flagid INT,
PRIMARY KEY(mid, sid, flagid),
FOREIGN KEY (mid) REFERENCES members(id),
FOREIGN KEY (sid) REFERENCES servers(sid),
FOREIGN KEY (flagid) REFERENCES flags(id)
);
/*
* flags are used to give custom permissions to players.
* For example a record may be: (0, "VIP", "This play is very important")
*/
DROP TABLE IF EXISTS flags;
CREATE TABLE flags
(
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(10),
description VARCHAR(100),
PRIMARY KEY(id)
);
答案 0 :(得分:3)
更改表创建的顺序。您应该在创建flags
之前创建serverPermissions
表。请参阅Demo Here。
答案 1 :(得分:2)
在添加外键约束时,这两个表的存储类型应为InnoDb。将两个表的类型从MyISAM更改为InnoDB
答案 2 :(得分:0)
此链接(https://mariadb.org/mariadb-innodb-foreign-key-constraint-errors/)提供了支持外键的引擎类型的快照。
据我所知,MariaDB和/或MySQL的以下存储引擎支持外键:
InnoDB (both innodb_plugin and XtraDB)
PBXT (https://mariadb.com/kb/en/mariadb/about-pbxt/)
SolidDB for MySQL (http://sourceforge.net/projects/soliddb/)
ScaleDB (https://mariadb.com/kb/en/mariadb/scaledb/ and http://scaledb.com/pdfs/TechnicalOverview.pdf)
MySQL Cluster NDB 7.3 or later (https://dev.mysql.com/doc/refman/5.6/en/mysql-cluster-ndb-innodb-engines.html)