mysql中的外來鍵

realji發表於2007-06-25

Foreign keys definitions are subject to the following conditions:

  • 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 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. For non-binary (character) string columns, the character set and collation must be the same.

  • 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.

參照mysql中的說明,建外來鍵時一定要注意2個表欄位之間的關係,一定要一致,mysql中的資料型別相對要比oracle的多,我遇到了int ,unsigned的問題。1個表的id欄位是int,令1個表的id欄位是int,unsigned。結果建了很多次外來鍵,都沒成功,找了半天才看到。看來還是太粗心。。

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7425608/viewspace-921062/,如需轉載,請註明出處,否則將追究法律責任。

相關文章