Basics MySQL Questions & Answer
In relation to PHP programming language, MySQL is the old database driver, and MySQLi is the Improved driver. MySQLi takes advantage of the newer features of MySQL 5. Lifted verbatim from the php.net site:
- Object-oriented interface
- Support for Prepared Statements
- Support for Multiple Statements
- Support for Transactions
- Enhanced debugging capabilities
- Embedded server support
A storage engine (or "database engine") is the underlying software component that a database management system (DBMS) uses to create, read, update and delete (CRUD) data from a database.
- InnoDB is newer while MyISAM is older.
- InnoDB is more complex while MyISAM is simpler.
- InnoDB is more strict in data integrity while MyISAM is loose.
- InnoDB implements row-level lock for inserting and updating while MyISAM implements table-level lock.
- InnoDB has transactions while MyISAM does not.
- InnoDB has foreign keys and relationship contraints while MyISAM does not.
- InnoDB has better crash recovery while MyISAM is poor at recovering data integrity at system crashes.
- MyISAM has full-text search index while InnoDB has not.(In latest MySQL version InnoDB also support)
Advantages of MyISAM
- Simpler to design and create, thus better for beginners. No worries about the foreign relationships between tables.
- Faster than InnoDB on the whole as a result of the simpler structure thus much less costs of server resources.
- Full-text indexing.
- Especially good for read-intensive (select) tables.
Disadvantages of MyISAM
No data integrity (e.g. relationship constraints) check, which then comes a responsibility and overhead of the database administrators and application developers.
Doesn't support transactions which is essential in critical data applications such as that of banking.
Slower than InnoDB for tables that are frequently being inserted to or updated, because the entire table is locked for any insert or update.
Advantages of InnoDB :-
- InnoDB should be used where data integrity comes a priority because it inherently takes care of them by the help of relationship constraints and transactions.
- Faster in write-intensive (inserts, updates) tables because it utilizes row-level locking and only hold up changes to the same row thatâ€™s being inserted or updated.
- Because InnoDB has to take care of the different relationships between tables, database administrator and scheme creators have to take more time in designing the data models which are more complex than those of MyISAM.
- Consumes more system resources such as RAM. As a matter of fact, it is recommended by many that InnoDB engine be turned off if thereâ€™s no substantial need for it after installation of MySQL.
Automicity - transactions are atomic and should be treated as one in case of rollback.
Consistency - the database should be in consistent state between multiple states in transaction.
Isolation - no other queries can access the data modified by a running transaction.
Durability - system crashes should not lose the data.
MyISAM stores each table on disk with three files whose names begin with same as table name. These files have different extensions to differentiate their purpose. A .frm files stores the table format, and a .MYD (MYData) file stores the data of the table. If the table has indexes then these are stored in the .MYI (MYIndex) files.
- In Procedure parameter as IN, OUT & INOUT used but for a function always IN parameter works.
- A FUNCTION is always returns a value using the return statement. PROCEDURE may return one or more values through parameters or may not return at all.
- Functions are normally used for computations where as procedures are normally used for executing business logic.
- A Function returns 1 value only. Procedure can return multiple values (max 1024).
- Stored procedure returns always integer value by default zero. Whereas function returns type could be scalar or table or table values.
- Stored procedure is precompiled execution plan where as functions are not.
- A function can call directly by SQL statement like select func_name from dual while procedure cannot.
- Stored procedure has the security and reduces the network traffic and also we can call stored procedure in any no. of applications at a time.
- A Function can be used in the SQL Queries while a procedure cannot be used in SQL queries .that cause a major difference b/w function and procedures.
Mysql trigger is a database object that is associated with a table. It will be activated when a defined action is executed for the table. The trigger can be executed when we run one of the following MySQL statement : INSERT, UPDATE & DELETE.
How to Get the Size of all Tables in a database in MySQL?