How to check term ID present in database table before updating
Your compound key you need to check for is for uid &amp; tid. You can check for a row's existance (thus, need to insert) with a db_query function call &amp; rowCount method call.

if(db_query('SELECT totalview FROM {custom_file_download_count} WHERE tid = :tid AND uid = :uid', array(':tid' =&gt; $tid, ':uid' =&gt; $GLOBALS['user']-&gt;uid))-&gt;rowCount() == 0) {
  db_insert('custom_file_download_count')
  -&gt;fields(array(
    'totalview' =&gt; 0,
    'tid' =&gt; arg(2),
    'timestamp' =&gt; REQUEST_TIME,
    'uid' =&gt; $GLOBALS['user']-&gt;uid,
  ))-&gt;execute();
}
db_update('custom_file_download_count')
-&gt;fields(array(
  'totalview' =&gt; 1,
  'tid' =&gt; arg(2),
  'timestamp' =&gt; REQUEST_TIME,
  'uid' =&gt; $GLOBALS['user']-&gt;uid,
))
-&gt;expression('totalview', 'totalview + 1')
-&gt;execute();