Locks:
1.Table Lock:that maybe lock all table.you can using "lock tables table_name read|write" to lock it,also "unlock tables" to free a table lock.
2.Row Lock:lock a row ,but you must create index on table(wide using in innodb)
3.Page Lock:lock a page in buffer pool,just used in DBD engine.
4.Metadata Lock:introduce into mysql 5.5.metadata is the information altered by DDL statement,the create/drop/alter statement that modify schema.
Table Lock:
when a table lock is set,the whole table is locked.write access not allowed if the read lock is set.and both read and write access are forbodden if the write lock is set.
Row Lock:
Row locks block a set of rows, not the whole table. Therefore, you can modify rows in the table that are not blocked by the lock.Row locks are set at the storage engine level. InnoDB is the main storage engine that currently uses row locks.
session1:
mysql> update t1 set name=sleep(200) where id=1;
----blocking-----
session 2:
mysql> update t1 set name='aaaa' where id=1;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
session 3:
mysql> show engine innodb status\G
TRANSACTIONS
Trx id counter 28966
Purge done for trx's n:o < 28962 undo n:o < 0 state: running but idle
History list length 1221
Total number of lock structs in row lock hash table 1
Deadlocks:
Deadlock is a situation when two or more competing transactions are waiting for each other to free locks, and thus neither ever finishes. With row-level locking, deadlocks are not 100% avoidable. InnoDB has an internal deadlock detector. When it finds one, it just rolls back one of the transactions, reporting an error that we’ll see momentarily. When designing an application, you need to be prepared for such a situation and handle the rollback appropriately.
Metadata Lock:
To ensure data consistency, DDL operations on a table should be blocked if another transaction is using the table. Starting with version 5.5.3, this is achieved by using metadata locks. When a transaction starts, it acquires metadata locks on all the tables it uses and releases the locks when it finishes. All other threads that try to modify the tables’definitions wait until the transaction ends.