惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 司徒正美
Jina AI
Jina AI
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 三生石上(FineUI控件)
宝玉的分享
宝玉的分享
MyScale Blog
MyScale Blog
I
InfoQ
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
T
Tailwind CSS Blog
D
DataBreaches.Net
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Blog of Author Tim Ferriss
B
Blog
阮一峰的网络日志
阮一峰的网络日志
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
月光博客
月光博客
雷峰网
雷峰网
Recent Announcements
Recent Announcements
量子位
B
Blog RSS Feed

MySQL Forums

MySQL :: Workbench update to 26.7.0 MySQL :: Upgrade Problems Starting a Discussion About Database Migration Best practices for indexing large tables with frequent updates? Discrepancy in Compatibility statement with MySQL and Connector/J 26.7 Announcing August 2026 Releases featuring MySQL Server, MySQL NDB Cluster, Shell and Connector/ODBC How to Troubleshoot Slow MySQL Queries Using EXPLAIN and Indexes The future of the timestamp data type Linking 2 cells in a table MySQL :: MySQL syntax error replication from 8.0 to 8.4 error How should I design a MySQL schema for storing construction material calculations? (no replies) Can AI analyze market trends faster than traditional trading (no replies) This CMD window bothers me. Announcing July 2026 Releases featuring MySQL Server 26.7.0, 9.7.2 and 8.4.11 Can I purge binlog files after migration ? Can't update expired password in Mysql Workbench Assistance with MySql application and Sql server MySQL :: MySQL Community Governance Model Announcing June 2026 Releases featuring MySQL Server 9.7.1 and 8.4.10 GoHighLevel Webhook Data in MySQL I'm troubleshooting MySQL query performance (no replies) What could cause .frm and .ibd mismatches across multiple tables? Problem with creating a connection to a remote database on local network Best MySQL schema for storing image processing jobs? APT Repository not working on Ubuntu 26.04 How do you create a remote connection to a database (no replies) MySQL :: storing and reading blob ODBC Connector almost always alters it's name and path MySQL :: Connector/Windows
MySQL :: QUESTION ABOUT FOREIGN KEY
Herve Anselm · 2026-05-04 · via MySQL Forums

QUESTION ABOUT FOREIGN KEY

Posted by: Herve Anselme
Date: April 28, 2026 02:52AM


Hello,

I use MySQL Workbench 8.0 CE for Windows.

I have a table of parameters : table PARAMS :

ParamTyp ParamNum ParamDescription
Type1 1 Description 1-1
Type1 2 Description 1-2
Type2 1 Description 2-1
Type2 2 Description 2-2
Type2 3 Description 2-3

on this table there is an index (just an ordinary index, not a primary key) on (ParamTyp, ParamNum).

I have another table of activity : table ACTIVITY :

create table ACTIVITY
(
Info1 <type>,
Info2 <type>,
MyTypeNum <same type as ParamNum in PARAMS table>
) ;

Now, what I’d like to do, is :

alter table ACTIVITY add constraint FK_ACTIVITY foreign key (MyTypeNum) references PARAMS(ParamNum) where ParamTyp = 'Type1' ;

in order to make sure that ACTIVITY.MyTypeNum has a value referenced in the « Type1 » parameters ; so that ACTIVITY.MyTypeNum could only have value 1 or 2 ; but not value 3 which does not exist for PARAMS.ParamTyp = 'Type1'.

But when I try, the alter table query makes an error :

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where ParamTyp = 'Type1'' at line 1

Is it because it’s possible, but the syntax I used is wrong (= the alter table query should be written differently) ?
Or is it just impossible to create a foreign key on filtered datas ?

Thanks for your answer and advices.

Regards,
Herve


Subject

Views

Written By

Posted

QUESTION ABOUT FOREIGN KEY

163

April 28, 2026 02:52AM

70

April 28, 2026 11:20AM

Sorry, only registered users may post in this forum.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.