Introduction of MySQL InnoDB
Mysql supports many storage engines for the table which define the internal working and support for different features of the tables. Some of the storage engines supported by MySQL include InnoDB, MyISAM, Memory, CSV, Merge, Archive, Federated, Blackhole, and Example. Out of them, Innodb is the most widely used storage engine while MyISAM is one of the original storage engines that was being used in some of the internal tables used for manipulating MySQL database belonging to information_schema and MySQL databases.
MyISAM engine is non-transactional and has table-level locking while InnoDB is a transactional storage engine with a row-level locking feature. InnoDB storage engine provides excellent performance and reliability balance and is one of the most preferred and suggested storage engines by oracle. In this article, we will learn about the InnoDB storage engine of MySQL, supported features, and working and example to create a table with an InnoDB storage engine by explicitly specifying the same.
Advantages of InnoDB Storage Engine
There are many advantages of InnoDB storage engine out of which some of them are listed below –
- All the data manipulation language statements and operations support the ACID(Atomicity, Consistency, Isolation, and Durability) properties of the transaction and other transactional features of rollback, commit, and crash-recovery support for securing the user data.
- Concurrent access by multiple users with high performance and consistent reads in oracle-style is achieved because of the row-level locking feature of the InnoDB storage engine.
- The data is arranged in such a way on the disk that will lead to optimizing retrieval of data when used primary keys in your query. The clustered index that is defined on the primary key of InnoDB tables leads to the organization of the data in a way that will minimize the input/output operation for lookups of the primary key.
- To avoid inconsistency in the tables that are related and dependent on each other, the InnoDB storage engine supports the usage of the foreign keys that help in the consistent update, insert and delete operations and helps in maintaining integrity and consistency in the contents of tables of the database.
Features of MySQL InnoDB
Features of InnoDB include B-tree indexes, Backup/point-in-time recovery, Clustered indexes, Compressed data, Data caches, Encrypted data, Foreign key support, Full-text search indexes for MySQL 5.6 and later versions, Geospatial data type support, Geospatial indexing support for MySQL 5.7 and later versions, Index caches, Locking granularity at the Row level, MVCC, Replication support, Storage limits up to 64TB, Transactions, Update statistics for the data dictionary. For the Adaptive Hash Index feature, InnoDB uses hash indexes internally but the hash index is not supported for InnoDB tables while manipulating.
Examples of MySQL InnoDB
Whenever we create a table using create table statement in MySQL 5.5 and above versions, the default storage engine of the created table is always Innodb while for the versions before MySQL 5.5, MyISAM is the default storage engine for the table. We can mention the storage engine of the table while creating it by using the ENGINE= clause if we want to create a table of any other storage engine or in case if we want to explicitly specify the engine type as InnoDB.
Firstly, we will see how we can use the ENGINE= clause to specify the storage engine with the help of an example. We will create a table named educba_innodb using the following create table statement –
CREATE TABLE educba_innodb (id INTEGER, description VARCHAR(20), PRIMARY KEY (id)) ENGINE=InnoDB;
while creating the table with InnoDB storage engine it is necessary to use a primary key column which has the following properties –
- This column should be used and referenced in most of the crucial queries.
- This column should be never left blank.
- This column should never have duplicate values.
- This column should not change the value once they are inserted in the table.
We can check the status of the table and all the related information by using the SHOW TABLE STATUS statement in MySQL. For example, if we want to check the status of the table we created earlier named educba_innodb then we will use the following query statement –
SHOW TABLE STATUS FROM educba LIKE 'educba_innodb' \G;
that gives the following output displaying all the details related to the educba_innodb table –
We can even see the Engine of the table in this status which is InnoDB in our case.
Let us try creating a table with different storage engines, say MyISAM with the name educba_myisam. For this, we will mention the ENGINE= MyISAM clause to create a query as shown below –
CREATE TABLE educba_myisam (id INTEGER, description VARCHAR(20), PRIMARY KEY (id)) ENGINE=MyISAM;
that gives following output –
After checking the status usig the following query for educba_myisam table –
SHOW TABLE STATUS FROM educba LIKE 'educba_myisam' \G;
gives following output saying the engine of the table is MyISAM –
To check the default storage engine that your database server that is installed, you can execute the following command –
Executing the above command gives the following output:
Let us try creating a table without specifying ENGINE= clause in CREATE TABLE statement. We will create a table named educba_default using following query –
CREATE TABLE educba_default (id INTEGER, description VARCHAR(20), PRIMARY KEY (id));
that give the output as follows after execution –
Now, let us check the status of the table to see which storage engine is used while creating the table by default. We queried earlier and saw that the default storage engine for mysql installed on my machine is InnoDB. Hence, the educba_default should have InnoDB engine. Here’s the output –
SHOW TABLE STATUS FROM educba LIKE 'educba_default' \G;
The InnoDB storage engine is the default engine assigned to the table while creation in Mysql 5.5 and above versions. For explicitly mentioning the storage engine of the table while creating, you can use the ENGINE= clause. Innodb engine gives high performance due to many of the features such as row-level locking and transaction support and indexing on primary keys. InnoDB storage engine also provides the feature to use foreign keys that help in maintaining the consistency and integrity in MySQL databases. Concurrent access by multiple users and each of them retrieving the consistent data is possible using the InnoDB storage engine.
This is a guide to MySQL InnoDB. Here we also discuss the introduction and advantages of innodb storage engine along with examples and its code implementation. You may also have a look at the following articles to learn more –