What is MySql?

                        MySql is a open source Relational Database Management System (RDBMS) that uses Structured Query Language (SQL).                                       
  • MySQL is a database system that runs on a server.
  •                     
  • MySQL is developed, distributed, and supported by Oracle Corporation.
  •                     
  • MySQL database are stored in tables. A table is a collection of related data, and it consists of columns and rows.
  •                                         Types MySQL table types or storage engines.     

    MySQL provides various storage engines for its tables as below:

           
  • MyISAM Storage Engine
  •     
  • InnoDB Storage Engine
  •     
  • MERGE Storage Engine
  •        
  • ARCHIVE Storage Engine
  •        
  • CSV Storage Engine
  •        
  • MEMORY (HEAP) Storage Engine
  •        
  • FEDERATED Storage Engine
  •        
  • BLACKHOLE Storage Engine
  • Each storage engine has its own advantages and disadvantages.

    MyISAM

    MyISAM extends the former ISAM storage engine. The size of MyISAM table can be up to 256TB, which is huge.The MyISAM tables are not transaction-safe. Before MySQL version 5.5, MyISAM is the default storage engine when you create a table without specifying the storage engine explicitly. From version 5.5, MySQL uses InnoDB as the default storage engine                     

    InnoDB

    The InnoDB tables fully support ACID-compliant and transactions. They are also optimal for performance. InnoDB table supports foreign keys, commit, rollback, roll-forward operations. The size of an InnoDB table can be up to 64TB.
    Like MyISAM, the InnoDB tables are portable between different platforms and operating systems. MySQL also checks and repairs InnoDB tables, if necessary, at startup.
                        

    MERGE

    A MERGE table is a virtual table that combines multiple MyISAM tables that have a similar structure into one table. The MERGE storage engine is also known as the MRG_MyISAM engine. The MERGE table does not have its own indexes; it uses indexes of the component tables instead.

    Using MERGE table, you can speed up performance when joining multiple tables. MySQL only allows you to perform SELECT, DELETE, UPDATE and INSERT operations on the MERGE tables. If you use DROP TABLE statement on a MERGE table, only MERGE specification is removed. The underlying tables will not be affected.

    Memory

    The memory tables are stored in memory and use hash indexes so that they are faster than MyISAM tables. The lifetime of the data of the memory tables depends on the uptime of the database server. The memory storage engine is formerly known as HEAP.

    CSV

    The CSV storage engine stores data in comma-separated values (CSV) file format. A CSV table brings a convenient way to migrate data into non-SQL applications such as spreadsheet software.

    CSV table does not support NULL data type. In addition, the read operation requires a full table scan.

           

    Database Queries :-


                                   
  • A query is a question or a request for particular table or record.
  •                                                                

    What is difference between  InnoDB and MyISAM

                         
  • InnoDB has row-level locking, MyISAM can only do full table-level locking.
  •                     
  • InnoDB has better crash recovery.

  •                     
  • MyISAM has FULLTEXT search indexes, InnoDB did not until MySQL 5.6 (Feb 2013)
  •                     
  • InnoDB implements transactions, foreign keys and relationship constraints, MyISAM does not.
  • The main mechanism used is the InnoDB Buffer Pool. It caches data and index pages from InnoDB tables accessed. To size your InnoDB Buffer Pool.

  • The main mechanism used is the key cache. It only caches index pages from .MYI files. To size your key cache.

  • InnoDB stores data in a file(s) called a tablespace. In newer versions of MySQL, you can store the data and indexes in separate files. MyISAM stores the data and indexes in two files (.MYD, .MYI). InnoDB is extremely critical about its tablespace and log files (They normally should be backed up together). You can't just backup your data by copying files like you would with MyISAM. In some situations you can only restore a table to the server from which you backed it up!

  • InnoDB are built on clustered indexes and uses MVCC to achieve high concurrency. This provides very fast primary key lookups. MyISAM doesn't support transactions, FK contraints, or row-level locks. MyISAM uses shared and exclusive locks on the entire table. However, concurrent reads & inserts for new rows are allowed.
  • InnoDB is crash safe (Assuming your flushes are truly durable on disk and not on some volatile cache). MyISAM is no where close to being crash safe. If you care about your data, use InnoDB. It might be OK to use MyISAM for read-only workloads.

  • InnoDB repairs are reasonably fast. MyISAM is slow and you might not get all your data back.

  • InnoDB requires a lot of memory (buffer pool). The data and indexes are cached in memory. Changes are written to the log buffer (physical memory) and are flushed every second to the log files (method depends on innodb_flush_log_at_trx_commit value). Having the data in memory is a huge performance boost. MyISAM only caches indexes (key_buffer_size) so that's where you would allocate most of your memory if you're only using MyISAM.

  •                     

                        

                            PHP 5 and later can work with a MySQL database using:

  • MySQLi extension (the "i" stands for improved)

  • PDO (PHP Data Objects)

  • Earlier versions of PHP used the MySQL extension. However, this extension was deprecated in 2012.


                        
                        


                        


    Should I Use MySQLi or PDO? :-


    MySQLi and PDO have their advantages:

  • PDO will work on 12 different database systems, where as MySQLi will only work with MySQL databases.
  • if you have to switch your project to use another database, PDO makes the process easy. You only have to change the connection string and a few queries. With MySQLi, you will need to rewrite the entire code - queries included.
  • Both are object-oriented, but MySQLi also offers a procedural API.
  • Both support Prepared Statements. Prepared Statements protect from SQL injection, and are very important for web application
  • security.


                        

                        
                        

    Both MySQLi and PDO Syntax


                                           


  • MySQLi (procedural)

  • MySQLi (object-oriented)
  • PDO

                   

    Open a Connection to MySQL

    First of all we need to create a connection to the server.

                     

    Example (MySQLi procedural):-

    $servername = "localhost";
    $username = "username";
    $password = "password";
    // Create connection
    $conn = mysqli_connect($servername, $username, $password);
    // Check connection

    if (!$conn) {

        die("Connection failed: " . mysqli_connect_error());
    }

    echo "Connected successfully";
    mysqli_close($conn);

         
           /* ********************** OR ***************** */

    $host = "localhost";
    $username ="root";
    $password = "";
    $db_name = "gmax";
    $con = mysqli_connect($host,$username,$password)or die(mysqli_error());
    mysqli_select_db($con,$db_name)or die(mysqli_error($con));
    mysqli_close($conn);

    Create a db connection using define constant

    define ( 'DB_HOST', 'localhost' );

    define ( 'DB_USER', 'gmax' );

    define ( 'DB_PASSWORD', 'gmaxpass' );

    define ( 'DB_NAME', 'gmaxdb' );



    $conn = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD) or die('Could not connect to database server.');

    mysqli_select_db($conn,DB_NAME) or die('Could not select database.');

    ...

    /********************** OR *****************/



    define("host", "localhost");
    define("user", "root");
    define("pass", "");
    define("db_name", "iwebbies_apps");

    $conn = mysqli_connect(host,user,pass,db_name) or die(mysqli_error());
    mysqli_select_db($conn, db_name) or die(mysqli_error($conn));
    if(!$conn)
    {
        echo "database error connection";
    }
    else {

        echo "connection successfully created.";

    }





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here

    Ittutorial