Some common problems in the interview Mysql

1, insert the data if repeated How to update?

INSERT INTO table (a,b,c) VALUES (1,2,3) ON DUPLICATE KEY UPDATE c=c+1;

 

2, a table, which has increment primary key ID, when the insert 17 records, delete records 15, 16, the article,
then Mysql restart, and then insert a record, this record ID 18 or 15?

( 1 ) If the table type is MyISAM, it is 18 
because MyISAM table will increment the primary key of the largest ID recorded in the data file, restart the MySQL auto-increment primary key of the largest ID will not be lost 
( 2 ) If the table is of type the InnoDB, it is 15 
the InnoDB table just records the maximum increment primary key ID into the memory, so restart the database or the table OPTIMIZE operation will cause the maximum loss of ID

 

3. What are the advantages and disadvantages [SELECT *] and [SELECT all fields] of two kinds of writing?

A former dictionary to parse the data that does not require
 2 result output order, the former construction of the table columns in the same order, the order according to which the specified field.
3 Table field renamed, the former does not need modification, which need to change
 4 . The latter can be optimized indexing, the former can not optimize the
 5 latter than the former high readability 
So, try to use the latter to query

 

4, if a table has only one field VARCHAR (N) type, utf8 encoding, maximum N is the number?

Since each character utf8 occupy up to 3 bytes. And the length of the line defined MySQL not exceed 65535 (text and blob is not included) , the maximum value of N is calculated :( 65535 - . 1 - 2 ) / 3 . Subtracting one reason is that the real 
occasion from the second storage byte minus 2 reason is because the length of the list to store the actual character length (length greater than 256 with two bytes), divided by 3 because utf8 restrictions: each character takes up to 3 bytes.

 

5, row locks in MySQL InnoDB engine is done by adding on what (or realization) of?

InnoDB row lock is achieved by the index to the index items on the lock, which is different from MySQL and Oracle, which is locked by a corresponding data line in the data block to achieve. 
InnoDB row lock to achieve this characteristic means: only there to retrieve data through index conditions, only InnoDB row-level locking, otherwise, would use InnoDB table locks

 

6, mysql with innodb distinguish myisam of?

1 . Transaction support 
MyISAM: it emphasizes that performance, every time the query is atomic, which performs several times faster than InnoDB type, but does not provide transaction support.
InnoDB: provides advanced database features Affairs supports transactions, foreign keys and so on. With a transaction (commit), transaction-safe rollback (rollback) and crash repair capacity (crash recovery capabilities) of (transaction-safe (ACID compliant) ) type table.
2 . InnoDB supports row-level locking, and support MyISAM table-level locking. 
Users in operation myisam table, the SELECT , Update , the Delete , insert statement to the table will automatically lock, lock the table to meet the future if the insert in the case of concurrent, you can insert new data at the end of the table.
. 3 . The InnoDB support MVCC, and MyISAM not supported (MVCC: Multi-Version Concurrency Control multi-version concurrency control)
. 4 . The InnoDB support foreign keys, and MyISAM does not support
5 primary key.
MyISAM: Allow no table any index and primary key exists, index address are stored row.
InnoDB: If no non-null primary key or unique index, will automatically generate a 6 -byte main key (not visible to users), the data part of the main index, the index is stored an additional value of the main index.
6 . InnoDB does not support full-text indexing, and MyISAM support.
7 . Portability, backup and restore
MyISAM: the data are stored as files, it will be very convenient for data transfer across the platform. Can be operated separately in the backup and recovery for a table
InnoDB: Free program data file may be copied, backup the binlog, or use the mysqldump, the amount of data of tens of G when relatively painful
8 storage structure.
MyISAM: Each MyISAM storage into three files on disk. The name of the first file name of the table to begin the extension indicates the file type. .frm file stores the table. Extended data file named .MYD (MYData). Extension index file is .MYI (MYIndex).
InnoDB: all the tables are stored in the same data file (may be more than one file, or file a separate table space), the size of the InnoDB table is limited only to the size of the operating system files, usually 2GB.

 

7, mysql replication principle and process

Mysql built-in replication is the basis for building large, high-performance applications. Mysql mechanism distribution system up to a plurality of data, this distribution is again achieved by copying the data of one host to another host Mysql (slaves), and re-executed. 
During replication server acts as a primary server, while one or more other servers act as from the server. A master index server writes binary log files will be updated, and maintain files to track log cycle. These log records can be sent from the server to update.
When a connection from the master server, it notifies the position of the last successful update the master server reads in the log. Since then, the server receives any updates from happening, and then blocked the main server and waits for notification of new updates.
It follows
a main server to record the updated binary log files.
2 from the master server logs binary copy to its own relay log (Replay log ) in.
3 . From the time of the relay server redo log, the update is applied to its own database.

 

8. Please describe briefly what kind commonly used index?

1 ordinary index: the index is created for the database tables
 2 unique index: general index and similar, it is different: the value of the MySQL database index columns must be unique, but allow free value
 3 primary key index: It is a special The only index, does not allow nulls. Usually at the same time create a primary key index when construction of the table
 4 composite index: The more fields in the database table together as a composite index

 

9, Mysql four transaction isolation level

1 . The Read  Uncommitted (Uncommitted read the contents of) 
the isolation level, all transactions can see the results of other uncommitted transactions. This isolation level is rarely used in practice, because its performance is not much better than the other levels. Uncommitted read data, also called a dirty read (Dirty the Read ).
2 . The Read Committed (read submission)
This is the default isolation level for most database systems (MySQL, but not the default). It meets the simple definition of isolation: a transaction can only see the change has been submitted firms do. This isolation level also supports so-called non-repeatable read (nonrepeatable the Read ),
because other instances of the same transaction in this instance may have a new deal during
the commit , so the same select may return different results.
3 . Repeatable the Read (can be re-read)
This is the default for MySQL transaction isolation level , it ensures that multiple instances of the same transaction at the time of concurrent reads the data, you will see the same data row. But in theory, it will lead to another thorny issue: Magic Reading (Phantom the Read ).
Simply put, phantom read means that when a user reads a range of data row, another transaction and insert a new row within the range, when the user re-read the range of data rows, you will find there is a new " Phantom "line. InnoDB and Falcon storage engine through a multi-version concurrency control (MVCC, Multiversion Concurrency Control gap lock) mechanism to solve the problem.
Note: In fact, multi-version only the problem of non-repeatable read problem, plus clearance lock (that is, here it is called concurrency control) before we can solve the problem of phantom read.
4 . Serializable (serializable)
This is the highest isolation level, transaction ordering by forcing it to make it impossible to conflict with each other, so as to solve the problem phantom read. In short, it is to add a shared lock on each row of data read.
At this level, it could lead to a lot of timeouts and lock contention.

 

10, the database three paradigms (cliché)

A first paradigm (1NF):. With atomic field, can not be divided. (Relational database systems are all in first normal form database table fields are a single property, can not be divided) => on the design of the column
 2 . The second paradigm (2NF) is the basis of the first paradigm (1NF) in the set up, that is in second normal form (2NF) must be met first normal form (1NF). Each instance requires a database table or row must be the only sub-region. => On line design
typically requires adding a column to the table, to uniquely identify each instance storage. This unique property is called the primary key column or primary key.
3 . Third normal (3NF) must satisfy the second paradigm (2NF). Briefly, a third paradigm (3NF) requires a database table comprising a non-primary key information is not already included in other tables. => On the design table
so the third paradigm has the following characteristics: >> 1 . Each column has only one value >> 2 . Each row can be distinguished. >> 3 . Each table contains no non-primary key information already contained in other tables

So, remember rows, columns, tables, words can

Guess you like

Origin www.cnblogs.com/roostinghawk/p/11067637.html