search

Home  >  Q&A  >  body text

MySQL, check if foreign key is assigned to any table

<p>I have a table called <code>files</code> Additionally, I have over 100 tables in which I use associations/foreign keys <code>file_id</code></p> <p>How do I get all <code>files</code> that do not have an existing association (that is, the file is not attached to any table)? </p>
P粉993712159P粉993712159587 days ago605

reply all(1)I'll reply

  • P粉566048790

    P粉5660487902023-08-18 00:11:53

    I usually do this in bash (it might be possible to use SQL, but I'm not sure which table)

    table_names=`echo "show tables"|mysql database_name -q -s|grep -v "show tables"`
    for table in $table_names
    do
      echo $table
      mysqldump database_name $table |grep file_id
    done;

    Then you will see something related to file_id in each table.

    reply
    0
  • Cancelreply