Mysql foreign key by non unique key — how is that possible?

后端 未结 4 1947
南笙
南笙 2021-01-12 08:50

I was migrating mysql database to postgres and stumbled across the following block in DDL (Note: This is what I got from mysqldump):

CREATE TABLE `catalog_pr         


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-12 09:27

    From the manual:

    Deviation from SQL standards: A FOREIGN KEY constraint that references a non-UNIQUE key is not standard SQL. It is an InnoDB extension to standard SQL.

    So it looks like InnoDB allows non-unique indexes as candidates for foreign key references. Elsewhere the manual states that you can reference a subset of columns in the referenced index as long as the referenced columns are listed first and in the same order as the primary key.

    Therefore, this definition is legal in InnoDB, although it's not standard SQL and leaves me, at least, a little confused as to the original designer's intentions.

    Manual page here.

提交回复
热议问题