DingoDile DingoDile - 1 year ago 94
MySQL Question

Check the database for a specific variable in a table

Hi can you check a database for a variable in a specific table and if it already exist it will deny the entry.

quick edit sorry :(
I have a database with tables with the same attributes(id,lastname,etc)
they all have the same set of it but in different tables they have different datas on it.

Ex. table1 has "1" as an Id and Table 2 has "2" on an id and so on.

Im Wondering if for example im going to check if the ID has already a "2" set on a ID on a Table inside a database and if it already has it can deny the table that im creating.

sorry for bad english...

Answer Source

Not so much information in your question to provide a nice answer,

but take a look at MySQL documentation and this link, maybe it can help you.

There is a UNIQUE constraint to guarantee what you say. If you insert a value wich already exists, it is denied.

After you edit:

You can get all your tables of the database with SHOW TABLES instruction. Take a look at this doc. You can call them all with a pattern, and use the LIKE clause.

Then you just have to iterate over this tables. This would be something like

for (String table : myTables) {
  if ( ( SELECT COUNT(*) FROM table WHERE id = "2" ) > 0 )
  then return deny;
return accept;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download