简体   繁体   English

MyISAM引擎表关系(MySQL)

[英]MyISAM Engine table relations (MySQL)

I'm using a host which only supports MyISAM tables engines for MySQL. 我正在使用只支持MySQL的MyISAM表引擎的主机。 I'm trying to create a CMS using php and MySQL, however, I'm having issues working out how to create relationships between the tables. 我正在尝试使用php和MySQL创建CMS,但是,我在解决如何在表之间创建关系时遇到问题。 For example, one of the features within this system is being able to assign tags to an article/blogpost, similar to how stack overflow has tags on their questions. 例如,该系统中的一个功能是能够为文章/博客文章分配标签,类似于堆栈溢出在其问题上有标签的方式。

My question is, as I cannot change my tables to use InnoDB, how can I form a relationship between the two tables? 我的问题是,由于我无法更改我的表以使用InnoDB,我如何在两个表之间形成关系? I am unable to use foreign keys as they are not supported in MyISAM, or at least not enforced. 我无法使用外键,因为它们在MyISAM中不受支持,或者至少没有强制执行。

So far, all I've found when searching is keeping track of it through PHP by ensuring that I update multiple tables at a time, but there must be a way of doing this on the MySQL side. 到目前为止,我在搜索时发现的是通过确保我一次更新多个表来通过PHP跟踪它,但必须有一种方法可以在MySQL端执行此操作。

Below are examples of the Article and Tag tables. 以下是文章和标签表的示例。

+---------------------------+  +---------------------------+
    |         Article           |  |            Tags           |
    +---------------------------+  +---------------------------+
    | articleID    int(11)      |  | tagID         int(11)     |
    | title        varchar(150) |  | tagString     varchar(15) |
    | description  varchar(150) |  +---------------------------+
    | author       varchar(30)  |  
    | content      text         |  
    | created      datetime     |  
    | edited       datetime     |  
    +---------------------------+

I've found loads of related questions on this site, but most of them InnoDB, which I cannot do as my host does not support it. 我在这个网站上发现了很多相关问题,但其中大部分都是InnoDB,我作为主持人不能支持它。

I've found a solution (kind of). 我找到了解决方案(种类)。 I've added another table called ArticleTags 我添加了另一个名为ArticleTags的表

+---------------------------+
    |        ArticleTags        |
    +---------------------------+
    | articleID    int(11)      |
    | tagID        int(11)      |
    +---------------------------+

This query returns the correct result, but I'm not sure if it's a bit of a hack, or if there is a better way to do it. 此查询返回正确的结果,但我不确定它是否有点破解,或者是否有更好的方法来执行此操作。

SELECT `tagString` 
FROM `Tags` 
WHERE id 
IN (
     SELECT `tagID` 
     FROM `ArticleTags` 
     WHERE `articleID` = :id
   ) 
ORDER BY  `Tags`.`tagString`

Can someone tell me if this this right? 谁能告诉我这是否正确?

Try TRIGGERs: 试试TRIGGER:

Example MyIsam with Foreign-Key: 示例MyIsam with Foreign-Key:

Create parent table: 创建父表:

CREATE TABLE myisam_parent
(
 mparent_id INT NOT NULL,
 PRIMARY KEY (mparent_id)
) ENGINE=MYISAM;

Create child table: 创建子表:

CREATE TABLE myisam_child
(
 mparent_id INT NOT NULL,
 mchild_id INT NOT NULL,
 PRIMARY KEY (mparent_id, mchild_id)
) ENGINE = MYISAM;

Create trigger (with DELIMITER): 创建触发器(使用DELIMITER):

DELIMITER $$
CREATE TRIGGER insert_myisam_child
BEFORE INSERT ON myisam_child
FOR EACH ROW
BEGIN
    IF (SELECT COUNT(*) FROM myisam_parent WHERE mparent_id=new.mparent_id)=0 THEN
        INSERT error_msg VALUES ('Foreign Key Constraint Violated!');//Custom error
    END IF;
END;$$
DELIMITER ;

Test case: 测试用例:

Try insert (create 3 lines in myisam_parent and 6 lines in myisam_child ): 尝试插入件(在创建3行myisam_parent和6行中myisam_child ):

INSERT INTO myisam_parent VALUES (1), (2), (3);
INSERT INTO myisam_child VALUES (1,1), (1,2), (2,1), (2,2), (2,3), (3,1);

Try insert: 尝试插入:

INSERT INTO myisam_child VALUES (7, 1);

Returns this error: 返回此错误:

ERROR 1062 (23000): Duplicate entry 'Foreign Key Constraint Violated!' ERROR 1062(23000):重复输入'外键约束违反!' for key 'PRIMARY' 关键'PRIMARY'


Note: 注意:

This example is for INSERT , for "triggers" with DELETE and UPDATE read link (at the beginning the question) 这个例子用于INSERT ,用于带有DELETE和UPDATE读取链接的“触发器”(在问题的开头)

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

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