[Database] Mysql base

And union all the difference 1.union

  • union would result set processing rule out the same result
  • union all result sets are not processed, the same result is not disposed

And a statement of the difference between 2.preparedStatement

  • Any time preparedStatement instead of using statement
  • Precompiled PreparedStatement prevent SQL injection
  • PreparedStatement multiple use can improve efficiency

3. Database three paradigms

  • The first paradigm (1NF) without duplicate column (atomicity), each column is indivisible atomic data items, not a non-atomic set of data items, arrays, records.
  • Second Normal Form (2NF) property entirely dependent on the primary key, if there is data which is only a part of the primary key and relevant, it does not meet the second paradigm.
  • The third paradigm (3NF) is not dependent on the other properties of the non-primary property of non-key columns are each independently of the other non-key columns.

4.Mysql row-level locking

MYISAM engine supports only table-level locking, and INNODB engine supports row-level locking, the following content is also deployed for INNODB row-level locking:

⑴ shared lock

Also known as the lock and read lock operation is created. Other users can concurrently read the data, but no transaction can modify the data.
If the transaction data T to A plus shared lock, other transactions can only share plus A lock can not add an exclusive lock.

SELECT ... LOCK IN SHARE MODE;

We will check out every piece of data shared locks, if other threads plus exclusive lock will be blocked.

⑵ exclusive lock

Also known as a write lock, if the transaction data T to A plus exclusive lock, other transactions can not be added to any A lock of any kind. Both approved the transaction row data read his locks, but also modify the data.

SELECT ... FOR UPDATE;

Increase after the query FOR UPDATE, Mysql will query results in each row plus exclusive lock, when there is no line of other threads of the query result set is used exclusively when the lock can be successfully applied for an exclusive lock, otherwise it will be blocked.

The basic elements of the transaction (ACID properties)

  • Atomicity (Atomicity): After the start of the transaction all operations, either all done, or all do not do, can not be stuck in the middle part. An error occurred during the execution of the transaction, will be rolled back to the state before the start of the transaction, all operations like never happened. That transaction is an indivisible whole, like a high school chemistry ever atom, is the basic unit of matter.
  • Consistency (Consistency): the beginning and the end of the transaction, integrity constraints of the database is not corrupted before. For example, A transfers to B, A can not be deducted money, B did not receive.
  • Isolation (Isolation): the same time, only one transaction request for the same data, without any interference from each other between different transactions. For example, A is to withdraw money from a bank card, withdraw money before the end of the process A, B can not transfer money to the card.
  • Persistent (Durability): After the completion of the transaction, the transaction all updates to the database will be saved to the database can not be rolled back.

6. transaction concurrency issues

  • Dirty read for two transactions T1 and T2, T1 T2 has been updated is read but not yet submitted fields. Thereafter, when the rollback T2, T1 reads the contents of the temporary and is valid
  • Non-repeatable read means reading the same line twice in a transaction data, but these two are not the same read data
  • A phantom read the database system administrator achievement for all students from grade ABCDE specific scores changed, but the system administrator B would insert a specific record score at this time, when the end of the A system administrator found that there is a change record not change overnight, just like happened as hallucinations, this is called phantom reads.

7. transaction isolation level

  • Uncommitted Read (Read Uncommitted) transaction A transaction read uncommitted transaction B
  • Read Committed (Read Committed) Transaction A Transaction B can only read committed (ORACLE, etc. default)
  • Repeatable read (Repeatable Read) queries in the same transaction are consistent start time of the transaction
  • Serialization (Serializable) to perform a transaction serialization, when you read other people can not see

8. Database Query Language Category

⑴DQL(Data Query Language)

Data query language DQL from the SELECT clause, FROM clause, WHERE clauses

⑵DML(Data Manipulation Language)

Data Manipulation Language DML comprises INSERT, UPDATE, DELETE

⑶DDL(Data Definition Language)

DDL data definition language used to create the various objects in the database ----- table, view,
index, synonyms, and other such clusters:
the CREATE TABLE / the VIEW / the INDEX / the SYN / the CLUSTER
DDL operations are recessive submitted! Can not rollback

⑷DCL(Data Control Language)

Data Control Language (DCL) is used to set or change the database user or role privileges statements, these statements include GRANT, DENY, REVOKE and other statements, by default, only members of the sysadmin, dbcreator, db_owner or equal db_securityadmin role only claim perform data control language.

9.mysql export table structure

 sudo mysqldump -h10.1.210.15 -u用户名 -p密码 数据库名 device_bind_info>device_bind_info.sql
 格式:
 sudo mysqldump -h地址 -u用户名 -p密码 数据库名 表名>导出sql

10. optimistic and pessimistic locking

  • Every time someone thought to pick up data is not modified, it will not be locked, but when the update will determine what others during this time did not go to update the data, you can use the mechanisms of the version number. Optimistic locking is suitable for the types of applications to read, so you can improve throughput if the database provided similar write_condition optimistic locking mechanisms are actually provided like.
  • Pessimistic locks every time someone else to pick up data are considered to be modified, so every time she took the data will be locked, so people want to take this data will block until it got the lock. Traditional relational database inside to use a lot of this locking mechanism, such as row locks, table locks, etc., read lock, write lock, are locked before doing the first operation.
  • Other kinds of locks have advantages and disadvantages, can not be considered to be a good alternative, as optimistic locking apply to lower write relatively few cases, namely when the conflict is really rare, so can save a lock of overhead, plus large throughput of the entire system. However, if the frequent conflict, the upper application will continue to be retry, such but rather reduces the performance, so use pessimistic locking is more appropriate in this case.

Guess you like

Origin blog.csdn.net/cheidou123/article/details/93905153