简体   繁体   English

以非关系配置删除记录

[英]Deleting records in a non-relational configuration

I am using MySQL and it is not a relational database (foreign keys constraints) so when I perform a delete on a record I want to check whether it is used in any other table, if so, don't delete. 我使用的是MySQL,它不是关系数据库(外键约束),因此当我对记录执行删除操作时,我想检查是否在其他任何表中都使用了它,如果这样,请勿删除。

I assume I would have to perform a database-wide search on all tables except it's own. 我假设我必须对所有表进行数据库范围的搜索,除非它自己拥有。 I keep each records id uniform throughout the database. 我在整个数据库中保持每个记录ID的统一。

Example: 例:

Assets
id | date_created | type_id

History
asset_id | date_recorded | store_id

I found a script to find all the table that have the records id: 我找到了一个脚本来查找所有具有记录ID的表:

SELECT
    DISTINCT TABLE_NAME,
    TABLE_NAME.COLUMN_NAME
FROM
    INFORMATION_SCHEMA.COLUMNS
WHERE
    COLUMN_NAME IN ('desander_id')
    AND
    TABLE_SCHEMA='emp'

But I get an error on the TABLE_NAME.COLUMN_NAME part where it says COLUMN_NAME is unknown. 但是我在TABLE_NAME.COLUMN_NAME部分收到一个错误,提示“ COLUMN_NAME”未知。 Is there a way I can do this? 有办法吗? Am I doing this the right way? 我这样做正确吗?

your from table is INFORMATION_SCHEMA.COLUMNS and you are selecting TABLE_NAME.COLUMN_NAME which not possible in MySQL. 您的from表是INFORMATION_SCHEMA.COLUMNS ,您正在选择TABLE_NAME.COLUMN_NAME ,这在MySQL中是不可能的。 It should be like table_name.column_name . 它应该像table_name.column_name try this: 尝试这个:

SELECT DISTINCT COLUMNS.TABLE_NAME,
                COLUMNS.COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN ('desander_id') AND
      TABLE_SCHEMA='emp';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM