六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 37|回复: 0

FOREIGN KEY Constraints

[复制链接]

升级  25.33%

78

主题

78

主题

78

主题

举人

Rank: 3Rank: 3

积分
276
 楼主| 发表于 2013-1-26 14:18:51 | 显示全部楼层 |阅读模式
<span style="color: #555555; font-size: 13px; line-height: 23px;">InnoDB supports foreign key constraints. The syntax for a foreign key constraint definition in InnoDB looks like this:
[CONSTRAINT [symbol]] FOREIGN KEY    [index_name] (index_col_name, ...)    REFERENCES tbl_name (index_col_name,...)    [ON DELETE reference_option]    [ON UPDATE reference_option]reference_option:    RESTRICT | CASCADE | SET NULL | NO ACTIONindex_name represents a foreign key ID. If given, this is ignored if an index for the foreign key is defined explicitly. Otherwise, if InnoDB creates an index for the foreign key, it uses index_name for the index name.
Foreign keys definitions are subject to the following conditions:
<div style="font-size: 14px; vertical-align: baseline; background-color: transparent; padding: 0px; margin: 0px;" class="itemizedlist">

  • Both tables must be InnoDB tables and they must not be TEMPORARY tables.
  • Corresponding columns in the foreign key and the referenced key must have similar internal data types insideInnoDB 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. For nonbinary (character) string columns, the character set and collation must be the same.
  • InnoDB requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. 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. (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.) index_name, if given, is used as described previously.
  • InnoDB permits a foreign key to reference any index column or group of columns. However, 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.
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表