⌘BABAKOV.NET Let's code!
100% [dark] [ru]

Types and Structure of Tables in MySQL

MySQL database supports several different types of tables, each with its own unique features and limitations. I will start my description with the "native" type of tables for MySQL, MyISAM. If the type of table is not specified in the create table operator, it will be assigned to MyISAM. Each MyISAM table is stored on disk in three files (in the subdirectory of the mysql/data catalog), the names of which match the name of the table, and the extension can take one of the following values: .frm - the structure of the table, this file contains information about the names and types of columns and indexes; .MYD - in this file, the data of the table is stored; .MYI - in this file, the indexes of the table are stored. Tables of the MyISAM type have several features: 1. Data is stored in a cross-platform format, which allows for the transfer of databases from one server to another by direct copying of files, bypassing intermediate forms. 2. The maximum number of indexes is 64 (from version 4.1.2). Each index can be a maximum of 16 columns. 3. From MySQL 4.1, a specific encoding can be set for each text column. 4. Indexing of text columns, including variable-length columns, is allowed. 5. Full-text search is supported. 6. Tables have a special flag indicating the correctness of closing the table. If the server was stopped abnormally, when loading, the flags of the closed tables will signal the possible damage to the tables and the server will attempt to automatically check and restore them.

Creating a MyISAM table with explicit specification of the type:

create table teble1 (i INT) ENGINE = MyISAM;
//Или
create table teble1 (i INT) TYPE = MyISAM;


Using the ENGINE keyword is preferred, but it is only available from MySQL 4.0.18.

MERGE

MySQL has a table type called MERGE, which allows combining several MyISAM tables into one. This type is mainly used to remove the limitation on the size of MyISAM tables, in cases where the operating system or file system does not allow creating files larger than a certain size (e.g. 4gb for FAT32, etc.). The tables to be merged into one table of type MERGE must have the same structure, i.e. the same columns, indexes, and their order.
For a table of type MERGE, files with the structure of the table will be created with the extension .rfm and files with the extension .MRG will be created, which contain the list of index files, which must be treated as a single file.
The combined table can be used with the SELECT, DELETE, and UPDATE commands. If you try to delete a table of type MERGE using the DROP TABLE command, only the MERGE table will be deleted, and the original MyISAM tables will not be affected.

Example of creating a table of type MERGE:

create table table_one (
id int not null auto_increment primary key,
name char(20));

create table table_two (
id int not null auto_increment primary key,
name char(20));

insert into table_one (name) values (\'11\'), (\'22\'), (\'33\');
insert into table_two (name) values (\'aa\'), (\'bb\'), (\'cc\');

create table summ_table (
id int not null auto_increment,
name char(20), index(id))
engine=merge union=(table_one,table_two) insert_method=last;


The result of the query from the table summ_table looks like this:

select * from summ_table;
 ID | NAME
 1  | 11
 2  | 22
 3  | 33
 1  | aa
 2  | bb
 3  | cc


In the combined table summ_table, the field id is no longer unique! (Tables of type MERGE cannot contain unique keys.)

The example shows that the parameter UNION is used to specify the tables to be combined, and the parameter INSERT_METHOD is used to specify the method of inserting new records into the MyISAM tables (in this case, one of the three constants is passed).

FIRST - When inserting a new record into the table MERGE, the record is placed in the first table in the list of the UNION parameter.
LAST - The record is placed in the last table in the list.
NO - This value is essentially identical to the absence of the parameter, and means that inserting into a MERGE table is not possible, and using the INSERT operator will result in an error.

MEMORY (HEAP)

The next type of table is MEMORY (HEAP), which is stored in RAM, which is why all queries to such tables are executed very quickly. The disadvantage of such tables is the complete loss of data in case of a server crash. Therefore, such tables are used to store temporary data that can be easily restored.
HEAP is a synonym for MEMORY. However, it is recommended to use MEMORY, which is a later version.
When creating a table of type MEMORY, a single file with the extension .frm is created, which contains the structure of the table. When the server is stopped or restarted, the data structure of the table remains, but all information contained in the table is lost, since it is stored only in RAM. When the server is restarted, the table does not need to be recreated, its structure remains.
Tables of type MEMORY have several restrictions: 1. Indexes are used only in comparison operations with the operators "=" and "<=>", with other operators, such as ">" or "<", indexing columns does not make sense. 2. Like MERGE tables, only non-unique indexes can be used. 3. TEXT and BLOB columns are not allowed. 4. Until MySQL 4.1, AUTO_INCREMENT was not supported in tables of this type.

Example of creating a table of type MEMORY:

create table tbl1 (field1 INT) ENGINE=MEMORY;
//или
create table tbl1 (field1 INT) ENGINE=HEAP;



EXAMPLE

The type EXAMPLE is a placeholder, a table of this type can be created, but it is not possible to obtain or transfer data. When creating a table of this type, only one file with the extension .frm is created, which contains the structure of the table.
This type was introduced for convenience and demonstrates how to create your own tables. In my personal opinion, this type has the least practical value.

Example of creating a table and attempting to modify data:

create table tab1 (field1 INT) engine=example;
//При попытке сделать вставку...
insert into tab1 values (1),(2),(3);
ERROR 1031 (HY000): Table storage engine for \'tab1\' doesn\'t have this option



BDB (BerkeleyDB)

Tables of type BDB are managed by the BerkeleyDB transaction handler, developed by the company Sleepycat. When creating a table of this type, two files are formed. One with the extension .frm and contains the structure of the table, and the other with the extension .db contains the data and indexes.

Features of tables BDB: 1. A journal is kept for tables, which allows for increased resilience of the database and increased probability of successful recovery in case of a crash. 2. Tables of type BDB are stored in binary trees. This method of storage slows down scanning the table (e.g. for selecting all rows of the table) and increases the space occupied by the table on disk. However, searching for individual values in the table becomes faster. All other tables store their indexes in binary trees. 3. All tables BDB must have a primary key, if absent, a hidden primary key with the attribute AUTO_INCREMENT is created. 4. Transactions are supported at the page level. 5. Counting the number of rows in the table using the COUNT() function is slower, due to the fact that counting rows for tables BDB (unlike MyISAM) is not supported on the server side and a full recalculation is performed at each access. 6. Keys are not packed, like in MyISAM tables, and occupy more space. 7. If a table BDB occupies all available space on the disk, a transaction is rolled back and an error message is displayed. Unlike BDB, MyISAM tables simply wait for free space to appear, which will cause the server to hang. 8. Files of tables BDB cannot be transferred between systems by simple copying, since when creating them, the path to the table file is saved. To transfer the database, the mysqldump utility must be used.

Example of creating a table of type BDB:

create table tab1 (field1 INT) engine=BDB;



InnoDB

The type of tables InnoDB was developed by the company Innobase Oy. Tables of this type provide high performance and reliable data storage in tables with a volume of up to 1 TB and a server load of up to 800 insertions/updates per second.

Features of the type InnoDB: 1. All tables are stored in a single tablespace, so table names must be unique. 2. Data storage in a single tablespace allows removing the limitation on table size. The file with the tables can be split into several parts and distributed across multiple disks or even hosts. 3. Tables support automatic recovery after a crash. 4. Transactions are supported. 5. This type of table in MySQL is the only one that supports cascading deletion and foreign keys. 6. Locking is performed at the level of individual records. 7. There is enhanced support for character sets.

Example of creating a table of type InnoDB:

create table tab1 (field1 INT, field2 CHAR(10), INDEX (field1)) ENGINE=InnoDB;



"Exotic" table types - I relate to these types those that I have never used and have not studied much, but they exist and are worth mentioning.

NDB Cluster
The type of tables NDB Cluster is intended for organizing MySQL clusters, where tables are distributed between several computers connected in a network.

ARCHIVE
The type ARCHIVE is intended for storing a large amount of data in a compressed format. When creating such tables, a file is created with the same name as the table and the extension .frm, which contains the structure of the table. In addition, two files with the extensions .ARZ and .ARM are created, which contain the data and metadata, respectively. Moreover, when optimizing the table, a file with the extension .ARN may appear.
Tables of type ARCHIVE support only two SQL operators: INSERT and SELECT. The SELECT operator is executed by scanning the entire table.

CSV
The CSV format represents a standard text file, where records are stored in rows, and fields are separated by semicolons. When creating a table, two files are formed. One with the extension .frm containing the structure of the table, and the other with the extension .CSV containing the data in CSV format. (Data in CSV format can also be read by other programs, e.g. using Excel)

FEDERATED
The type FEDERATED allows storing data on remote tables located on other machines in the network. When creating a table, only a file with the extension .frm is created, since the data is stored on the remote machine.
To create a table of type FEDERATED, you must first create a table on the remote machine, and then on the working machine, specify the remote one.

Example of creating a table of type FEDERATED:

//Сначала создаём таблицу на удалённой машине...
create table test1( field1 INT) engine=MyISAM;
//Теперь создаём таблицу FEDERATED на рабочей машине...
create table test2( field1 INT) engine=FEDERATED COMMENT=\'mysql://user_name@remote_host:3306/host_name/test1\';


The structure of tables on the machines must be identical.
In the comment field of the table FEDERATED, a string of connection to the table on the remote machine is specified.
It should also be noted that transactions are not supported in tables of this type.

That's all.

Comments (0)

No comments yet.

Leave a comment
Comment
Images (up to 5 MB)
Your comment will appear after moderation.