mysql中foreign keys_MySQL与FOREIGN KEYS的多对多关系

bd96500e110b49cbb3cd949968f18be7.png

I am trying to create a many-to-many relationship in my MySQL database. I have three tables:

Films, Genres and Films_Genres. I am using the following code to set them up:

CREATE TABLE Films

(

id INT NOT NULL AUTO_INCREMENT,

PRIMARY KEY(id),

Title VARCHAR(255)

),

CREATE TABLE Genres

(

id INT NOT NULL AUTO_INCREMENT,

PRIMARY KEY(id),

Name VARCHAR(255)

),

CREATE TABLE Films_Genres

(

film_id INT NOT NULL,

genre_id INT NOT NULL,

PRIMARY KEY (film_id, genre_id),

FOREIGN KEY (film_id) REFERENCES Films(id) ON UPDATE CASCADE,

FOREIGN KEY (genre_id) REFERENCES Genres(id) ON UPDATE CASCADE

)

However, when I try to insert some values into the tables with:

INSERT INTO Films (Title) VALUES ('$title')

INSERT INTO Genres (Name) VALUES ('$genre')

I can see the new Film in Films table and the new Genre in Genres table but

the Films_Genres table doesn't update - there are no new rows (I'm checking through phpMyAdmin).

What am I doing wrong?

解决方案

You won't see anything in Films_Genres table until you explicitly insert something in it.

Referential integrity through PK and FK is not for populating your tables for you.

Your MySql code for inserting a new record in Films_Genres, if it's a new film which correspond to a new genre, might look like

INSERT INTO Films (Title) VALUES ('Title1');

SET @film_id = LAST_INSERT_ID();

INSERT INTO Genres (Name) VALUES ('Genre1');

SET @genre_id = LAST_INSERT_ID();

INSERT INTO Films_Genres (film_id, genre_id) VALUES(@film_id, @genre_id);

On php side to get a newly assigned id for an autoincremented field use $mysqli->insert_id.

Now if you want to create a new film and assign it to multiple genres at once you can do

INSERT INTO Films (Title) VALUES ('Title2');

SET @film_id = LAST_INSERT_ID();

-- if you get ids of genre from your UI just use them

INSERT INTO Films_Genres (film_id, genre_id)

SELECT @film_id, id

FROM Genres

WHERE id IN (2, 3, 4);

INSERT INTO Films (Title) VALUES ('Title3');

SET @film_id = LAST_INSERT_ID();

-- if you names of genres you can use them too

INSERT INTO Films_Genres (film_id, genre_id)

SELECT @film_id, id

FROM Genres

WHERE Name IN ('Genre2', 'Genre4');

Here is

这个错误信息是因为MySQL服务器版本的语法错误导致的。根据提供的引用内容,解决这个问题的方法有六个: 1. 在数据库表冲突字段前后加 ` 符号(键盘Tab键上面那个键),就不会再报语法错误了。 2. 修改数据库表冲突字段名称,修改为和SQL语句关键字不冲突的其他名称。 3. 使用命令行工具而不是navicat工具。 4. 检查关键字是否写错了,或者丢失了关键字。 5. 仔细检查SQL语句是否有问题。 6. 检查表名和表字段是否有问题,例如和SQL关键字冲突,或者和同库下的其他表冲突。 根据你提供的错误信息,检查"PRAGMA foreign_keys=OFF"附近的语法是否有错误。可能需要使用上述的方法之一来解决该问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [check the manual that corresponds to your MySQL server version for the right syntax to use完美解决](https://blog.csdn.net/yetaodiao/article/details/126656737)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [your MySQL server version for the right syntax to use near ‘(0) NOT NULL, `endTime` datetime(0) NO](https://download.csdn.net/download/weixin_38515573/13688019)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值