Foreign key (FKADC4355E513C75C1:TBL_EVA_INFO [ENTER_ID])) must have same number of columns as the referenced primary key (TBL_EVA [EVA_ID,ENTER_ID])
是联合主键的问题.建议取消联合主键,再新建一个键作为主键.
Here is a simple example that relates parent and child tables through a single-column foreign key:
CREATE TABLE parent (id INT NOT NULL,
PRIMARY KEY (id)
) ENGINE=INNODB;
CREATE TABLE child (id INT, parent_id INT,
INDEX par_ind (parent_id),
FOREIGN KEY (parent_id) REFERENCES parent(id)
ON DELETE CASCADE
) ENGINE=INNODB;
A more complex example in which a product_order table has foreign keys for two other tables.
One foreign key references a two-column index in the product table.
The other references a single-column index in the customer table:
CREATE TABLE product (category INT NOT NULL, id INT NOT NULL,
price DECIMAL,
PRIMARY KEY(category, id)) ENGINE=INNODB;
CREATE TABLE customer (id INT NOT NULL,
PRIMARY KEY (id)) ENGINE=INNODB;
CREATE TABLE product_order (no INT NOT NULL AUTO_INCREMENT,
product_category INT NOT NULL,
product_id INT NOT NULL,
customer_id INT NOT NULL,
PRIMARY KEY(no),
INDEX (product_category, product_id),
FOREIGN KEY (product_category, product_id)
REFERENCES product(category, id)
ON UPDATE CASCADE ON DELETE RESTRICT,
INDEX (customer_id),
FOREIGN KEY (customer_id)
REFERENCES customer(id)) ENGINE=INNODB;
-----------
InnoDB also supports foreign key constraints. The syntax for a foreign key constraint definition in InnoDB looks like this:
[CONSTRAINT symbol] FOREIGN KEY [id] (index_col_name, ...)
REFERENCES tbl_name (index_col_name, ...)
[ON DELETE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
[ON UPDATE {RESTRICT | CASCADE | SET NULL | NO ACTION}]
Foreign keys definitions are subject to the following conditions:
Both tables must be InnoDB tables and they must not be TEMPORARY tables.
In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist.
In the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.
Index prefixes on foreign key columns are not supported. One consequence of this is that BLOB and TEXT columns cannot be included in a foreign key, because indexes on those columns must always include a prefix length.
If the CONSTRAINT symbol clause is given, the symbol value must be unique in the database. If the clause is not given, InnoDB creates the name automatically.
InnoDB rejects any INSERT or UPDATE operation that attempts to create a foreign key value in a child table if there is no a matching candidate key value in the parent table. The action InnoDB takes for any UPDATE or DELETE operation that attempts to update or delete a candidate key value in the parent table that has some matching rows in the child table is dependent on the referential action specified using ON UPDATE and ON DELETE subclauses of the FOREIGN KEY clause. When the user attempts to delete or update a row from a parent table, and there are one or more matching rows in the child table, InnoDB supports five options regarding the action to be taken:
CASCADE: Delete or update the row from the parent table and automatically delete or update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported. Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column in the parent table or in the child table.
SET NULL: Delete or update the row from the parent table and set the foreign key column or columns in the child table to NULL. This is valid only if the foreign key columns do not have the NOT NULL qualifier specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.
NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary key value is not allowed to proceed if there is a related foreign key value in the referenced table. InnoDB rejects the delete or update operation for the parent table.
RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as omitting the ON DELETE or ON UPDATE clause. (Some database systems have deferred checks, and NO ACTION is a deferred check. In MySQL, foreign key constraints are checked immediately, so NO ACTION and RESTRICT are the same.)
SET DEFAULT: This action is recognized by the parser, but InnoDB rejects table definitions containing ON DELETE SET DEFAULT or ON UPDATE SET DEFAULT clauses.
Note that InnoDB supports foreign key references within a table. In these cases, “child table records” really refers to dependent records within the same table.
InnoDB requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. The index on the foreign key is created automatically. This is in contrast to some older versions, in which indexes had to be created explicitly or the creation of foreign key constraints would fail.
Corresponding columns in the foreign key and the referenced key must have similar internal data types inside InnoDB so that they can be compared without a type conversion. The size and sign of integer types must be the same. The length of string types need not be the same. If you specify a SET NULL action, make sure that you have not declared the columns in the child table as NOT NULL.
If MySQL reports an error number 1005 from a CREATE TABLE statement, and the error message refers to errno 150, table creation failed because a foreign key constraint was not correctly formed. Similarly, if an ALTER TABLE fails and it refers to errno 150, that means a foreign key definition would be incorrectly formed for the altered table. You can use SHOW ENGINE INNODB STATUS to display a detailed explanation of the most recent InnoDB foreign key error in the server.
Note: InnoDB does not check foreign key constraints on those foreign key or referenced key values that contain a NULL column.
Note: Currently, triggers are not activated by cascaded foreign key actions.
You cannot create a table with a column name that matches the name of an internal InnoDB column (including DB_ROW_ID, DB_TRX_ID, DB_ROLL_PTR and DB_MIX_ID). In versions of MySQL before 5.1.10 this would cause a crash, since 5.1.10 the server will report error 1005 and refers to errno -1 in the error message.
Deviation from SQL standards: If there are several rows in the parent table that have the same referenced key value, InnoDB acts in foreign key checks as if the other parent rows with the same key value do not exist. For example, if you have defined a RESTRICT type constraint, and there is a child row with several parent rows, InnoDB does not allow the deletion of any of those parent rows.
InnoDB performs cascading operations through a depth-first algorithm, based on records in the indexes corresponding to the foreign key constraints.
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.
Deviation from SQL standards: If ON UPDATE CASCADE or ON UPDATE SET NULL recurses to update the same table it has previously updated during the cascade, it acts like RESTRICT. This means that you cannot use self-referential ON UPDATE CASCADE or ON UPDATE SET NULL operations. This is to prevent infinite loops resulting from cascaded updates. A self-referential ON DELETE SET NULL, on the other hand, is possible, as is a self-referential ON DELETE CASCADE. Cascading operations may not be nested more than 15 levels deep.
Deviation from SQL standards: Like MySQL in general, in an SQL statement that inserts, deletes, or updates many rows, InnoDB checks UNIQUE and FOREIGN KEY constraints row-by-row. According to the SQL standard, the default behavior should be deferred checking. That is, constraints are only checked after the entire SQL statement has been processed. Until InnoDB implements deferred constraint checking, some things will be impossible, such as deleting a record that refers to itself via a foreign key.
分享到:
相关推荐
MySQL的MyISAM存储引擎不支持外键约束,因此在设置外键时,需要确保使用支持外键的存储引擎,如InnoDB。 8. **外键约束检查时机**: 默认情况下,MySQL在`DEFERRED`模式下,只在事务提交时检查外键约束。若需在...
在 MySQL 中,外键是指在一个表中的一列或多列,引用另一个表中的主键或唯一索引。外键可以强制实施数据的一致性和完整性,使得数据更加可靠。外键在 MySQL 中的实现主要是通过 InnoDB 存储引擎来实现的。只有 ...
1. **存储引擎限制**:MySQL 的外键约束只支持 InnoDB 存储引擎,不支持 MyISAM 等其他类型的存储引擎。 2. **命名规则**:外键名称可以使用 `constraint 'fk_name'` 或 `constraint "fk_name"` 形式指定。 3. **...
3. SET NULL:被父面的外键关联字段被 update、delete 时,子表的外键列被设置为 null。 4. NO ACTION:不进行任何操作。 四、创建外键的注意事项 创建外键时,需要注意以下几点: 1. 所有 tables 必须是 InnoDB ...
MySQL外键关联是数据库设计中的一个重要特性,它用于在两个表之间建立引用完整性约束,以确保数据的一致性和准确性。本文通过深入分析MySQL中外键关联问题,旨在帮助读者理解并掌握外键的创建、修改以及删除等操作的...
- 在电商系统中,订单表(Orders)可以通过外键关联用户表(Users),以标识下单的用户。 - 在论坛系统中,帖子表(Threads)可以引用用户表(Users)的主键,表示发帖人。 总结来说,MySQL中的外键是实现关系...
- **SET NULL**:当父表的外键关联列被更新或删除时,子表中的外键值将被设置为NULL。 #### 四、外键的注意事项 - **前提条件**: - 所有涉及外键的表必须是InnoDB类型,因为只有InnoDB支持外键约束。 - 外键...
在MySQL中,创建外键关联时遇到"errno:150"错误,通常是由于数据类型不匹配、缺少索引或不正确的约束设置等原因导致的。以下是对这些常见问题的详细解释: 1. **数据类型不匹配**:外键字段与主键字段的数据类型...
这条语句将在ss_accesscode表中添加一个外键约束,該外键约束将vccId字段与ss_vcc表中的vccId字段关联起来,并且在删除ss_vcc表中的记录时,相应的ss_accesscode表中的记录也将被删除(ON DELETE CASCADE)。...
两个表必须是InnoDB表,MyISAM表暂时不支持外键(据说以后的版本有可能支持,但至少目前不支持); 2.外键列必须建立了索引,MySQL 4.1.2以后的版本在建立外键时会自动创建索引,但如果在较早的版本则需要显示...
MySQL数据库在设计关系型数据库时,外键是一个关键的概念,它用于维护数据库中表与表之间的关联性。这个“mysql外键教程.zip”压缩包包含了关于MySQL外键使用的多个方面,下面将详细解释这些知识点。 首先,我们...
在MySQL中,创建数据表并设置主外键关系需遵循以下规则: 1. 存储引擎必须是InnoDB,因为MyISAM引擎不支持外键约束。 2. 外键列和参照列的数据类型应相同或可隐式转换,以确保数据一致性。 3. 外键列必须有索引,...
在MySQL中,只有使用InnoDB存储引擎的表才能支持外键约束,因为MyISAM存储引擎不支持这一特性。以下是对MySQL外键的详细解释: 1. **外键的定义**: 外键(Foreign Key)是一种数据库约束,它在一个表(称为“子表...
通过外键,可以定义表之间的关联,使得数据的一致性得以维护。 书中的内容还会涵盖InnoDB的物理存储结构,包括表空间、段、区、页等层次,以及如何通过这些结构优化查询性能。另外,InnoDB的缓冲池(Buffer Pool)...
例如在论坛的场景中,`reply` 表的 `thread_id` 字段是外键,它引用了 `thread` 表的 `id` 主键,这样当尝试删除一个帖子(主键记录)时,如果设置了外键约束,MySQL会自动检查该帖子是否有相关的回帖,如果有,就会...
外键约束确保了在一个表中的数据(子表)与另一个表(父表)中的数据之间的关联性。在MySQL中,外键约束主要用于InnoDB存储引擎,因为它支持事务处理和行级锁定,这在处理并发操作和数据完整性时至关重要。 在MySQL...