MySQL Database Object Existance Check SQL Example

database object existance check by optimize sql example in a single mysql/mssql/oracle query

check an object in database is exist or not example

user table structure

  user
  (                                    
    `USER_ID` int(10) NOT NULL auto_increment,             
    `USER` varchar(255) default NULL,                      
    `EMAIL` varchar(255) default NULL,                     
    `PASSWORD` varchar(20) default NULL,                   
    `USER_TYPE_ID` int(10) NOT NULL,                       
    `STATUS` int(1) default '1',                           
    `PUBLISH` int(1) default '0',                          
    `DATE_CREATED` datetime default NULL,                  
    `DATE_UPDATED` datetime default NULL,                  
    PRIMARY KEY  (`USER_ID`)                               
  ) 
 

Insert some sample data then try to retrive as follows by using PHP

      <?php
       
        /*
        |-------------------------------------------------
        | CASE :
        | a new user want to register in your database.
        | you need to check whether a user holding the
        | information is already exist or not.
        |-------------------------------------------------
        */
        $sql = " SELECT * FROM user WHERE EMAIL = 'www@www.www' AND PASSWORD = 'password' ";
        /*
        |-------------------------------------------------
        | this query works fine but use more resource.
        | let's optimize it.
        |-------------------------------------------------
        */
        $sql = " SELECT EMAIL FROM user WHERE EMAIL = 'www@www.www' AND PASSWORD = 'password' ";
        /*
        |-------------------------------------------------
        | still it can be optimized for better performance
        |-------------------------------------------------
        */
        $sql = " SELECT 'nothing' FROM user WHERE EMAIL = 'www@www.www' AND PASSWORD = 'password' ";
        /*
        |-------------------------------------------------
        | this time db server doesnt select anything but
        | a string and better performance is obtained.
        |-------------------------------------------------
        */
       ?>
       
 

Related Tutorial Examples

  1. Aliase Column Name In SQL Command MySQL Example
  2. Operator Precedence In SQL Command MySQL Example
  3. Arithmetic Operators In SQL Command MySQL Example
  4. Describe Table Structure MySQL Example
  5. SELECT Specific Columns MySQL Example
  6. SELECT Distinct... SQL Command MySQL Example
  7. SELECT Specific Row(s) SQL Command MySQL Example
  8. 19 Examples to Learn MySQL
  9. 28 Basic Tutorials to Learn Oracle
  10. Email Address Validation PHP Regular Expression Example

No comments:

Post a Comment

leave your comments here..