0

What is better approach to check existence of an object in database?

select count(id) as count from my_table where name="searchedName";

OR

select id from my_table where name="searchedName";

And then check if count > 0 or the object is not null (ORM logic)

EDIT:

select id to be valid for Oracle.

4
  • 1
    Which dbms are you using? Those queries are product specific. Commented Apr 10, 2018 at 9:10
  • H2 in dev and Oracle in prod Commented Apr 10, 2018 at 9:11
  • 1
    top 1 is not valid for Oracle. Commented Apr 10, 2018 at 9:20
  • 1
    You develope in H2, but production is Oracle? That's a very bad idea. Not every query that works in H2 works in Oracle too. (E.g. the standard SQL query 'select 1;' is invalid in Oracle.) Your queries are both invalid by the way; string delimiter is the single quote. Commented Apr 10, 2018 at 10:11

3 Answers 3

2

The idea should be to that we only need to find one record in order to say that such record exists. This can be done with an EXISTS clause in standard SQL.

select exists (select * from mytable where name = 'searchedName');

returns true if the table contains a record with 'searchedName' and false otherwise.

If you want 0 for false and 1 for true instead (e.g. if the DBMS does not support booleans):

select case when exists (select * from mytable where name = 'searchedName') 
  then 1 else 0 end as does_exist;

You say you want this for Oracle. In Oracle you can use above query, but you'd have to select from the table dual:

select case when exists (select * from mytable where name = 'searchedName') 
  then 1 else 0 end as does_exist
from dual;

But for Oracle we'd usually use rownum instead:

select count(*) as does_exist
from mytable 
where name = 'searchedName'
and rownum = 1; -- to find one record suffices and we'd stop then

This also returns 1 if the table contains a record with 'searchedName' and 0 otherwise. This is a very typical way in Oracle to limit lookups and the query is very readable (in my opinion).

Sign up to request clarification or add additional context in comments.

1 Comment

Although I like the answer, I still don't like the Oracle solution. Using count(*0 for this purpose just sends the wrong signal -- even with the rownum = 1.
1

I'd just call:

select id from my_table where name='searchedName';

Making sure there is an index for the name column. And then check whether or not the result is empty.

2 Comments

If you need to use the result logic for another SQL query, I'd go back to the back-end language (for instance Java or PHP) and then depending on the result, call another SQL query.
But if that's the case, you should also post that part of the SQL logic for us to optimize our answers (both queries could be combined)
0

Try with IF EXISTS (

if exists (select 1 from my_table where name = "searchedName")
begin
     ....
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.