0

我试图在外键更改中获取操作(在删除/更新时)。

我试图检查它查看 INFORMATION_SCHEMA.key_column_usage :

SELECT * 
FROM `INFORMATION_SCHEMA`.`key_column_usage` 
WHERE `referenced_table_schema` = '{$this->dbName}'
AND `referenced_table_name` IS NOT NULL
AND `table_name` = '{$this->tableName}'

它显示了这样的内容:

0 => 
array (size=12)
  'CONSTRAINT_CATALOG' => string 'def' (length=3)
  'CONSTRAINT_SCHEMA' => string 'test' (length=4)
  'CONSTRAINT_NAME' => string 'profiles_ibfk_1' (length=15)
  'TABLE_CATALOG' => string 'def' (length=3)
  'TABLE_SCHEMA' => string 'test' (length=4)
  'TABLE_NAME' => string 'profiles' (length=8)
  'COLUMN_NAME' => string 'id' (length=2)
  'ORDINAL_POSITION' => string '1' (length=1)
  'POSITION_IN_UNIQUE_CONSTRAINT' => string '1' (length=1)
  'REFERENCED_TABLE_SCHEMA' => string 'test' (length=4)
  'REFERENCED_TABLE_NAME' => string 'users' (length=5)
  'REFERENCED_COLUMN_NAME' => string 'id' (length=2)

....

我也尝试过 INFORMATION_SCHEMA.TABLE_CONSTRAITS ,我得到了:

0 => 
array (size=6)
  'CONSTRAINT_CATALOG' => string 'def' (length=3)
  'CONSTRAINT_SCHEMA' => string 'test' (length=4)
  'CONSTRAINT_NAME' => string 'PRIMARY' (length=7)
  'TABLE_SCHEMA' => string 'test' (length=4)
  'TABLE_NAME' => string 'profiles' (length=8)
  'CONSTRAINT_TYPE' => string 'PRIMARY KEY' (length=11)

....

在 2 种情况下,没有关于删除/更新操作的信息。

我能得到它?如何?如果我当然可以。

4

2 回答 2

2

从信息模式中选择UPDATE_RULE和:DELETE_RULEREFERENTIAL_CONSTRAINTS

SELECT CONSTRAINT_NAME, UPDATE_RULE, DELETE_RULE
FROM   INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
WHERE  UNIQUE_CONSTRAINT_SCHEMA = '{$this->dbName}'
   AND REFERENCED_TABLE_NAME IS NOT NULL
   AND TABLE_NAME = '{$this->tableName}'

如文件所述:

UPDATE_RULEDELETE_RULE的可能值为CASCADE, SET NULL, SET DEFAULT, RESTRICT, NO ACTION

于 2013-04-29T15:05:21.447 回答
0

这取决于你想做什么。如果您只想在相关记录上更新外键,那么您正在寻找带有该CASCADE选项的外键约束。请参阅此页面并通读有关级联的部分。如果您想对记录的 UPDATE/DELETE 执行更复杂的操作,则需要触发器。有关创建触发器的语法,请参阅此页面。

于 2013-04-29T15:05:28.903 回答