Home Database Mysql Tutorial 数据库之子查询和事务隔离级别

数据库之子查询和事务隔离级别

Jun 07, 2016 pm 04:03 PM
affairs database Inquire Knowledge points level isolation

数据库大的知识点,特此补上。看的还是《漫画数据库》。 一、查询 如果要从一个数据库里面检索出某一字段为最大的数据项,那用什么方法呢?其实用子查询就可以查到。 1)子查询是查询里面再嵌套查询。 people表: 如果我要查询年龄最大的那一行数据项。就可以

数据库大的知识点,特此补上。看的还是《漫画数据库》。

一、查询

如果要从一个数据库里面检索出某一字段为最大的数据项,那用什么方法呢?——其实用子查询就可以查到。

1)子查询是查询里面再嵌套查询。

people表:

如果我要查询年龄最大的那一行数据项。就可以使用子查询。

select * from people where age = (select max(age) from people);
Copy after login
选出people里面的最大年龄,再选出年龄等于最大值的数据。

当然,还有另外一种方法。

select * from people order by age desc limit 1;
Copy after login
通过从大到小的排序,并且限制数据为一条,那么就是最大的了。当然,有两条数据都是最大,那么这条语句就不完美了。

2)相关子查询

子查询内部嵌套的表格其实可以使用外部指定的表格,为相关子查询。这里说的不是外部和内部的表格一定是不同的。看例子。

假如现在选出年龄大于本国平均年龄的人,单单用子查询是查不了的,因为我们需要每条数据的国家都和平均年龄的国家对应。

select * from people p where age>(select avg(age) from people where count
ry = p.country);
Copy after login
选出平均年龄的时候,如果没有where country = p.country那么选出来的只是大于全部人的平均年龄的数据。

但是有了之后,假如选到country为a的数据,那就会根据a国家的平均年龄进行比较,最终得到结果。

灵活运用子查询功能是很强大的。

二、事务隔离级别

1)事务
既然谈到事务隔离级别,先要说明事务是什么东西,之前论坛一个老鸟也面试过别人这样的问题,
数据库进行数据的检索,插入、更新和删除,用户的一系列操作我们称为事务,最重要的一点,保证数据的一致性和完整性。

保证数据的一致性和完整性是怎么来的?其实数据库事务具有酸性。开玩笑,其实是ACID属性。
A,Atomicity,原子性,事务必须结束与提交或者回滚的任意一个任务。
C,Consistency,一致性,执行数据库事务不能破坏数据库的一致性,举个例子(你从银行取了1000元,那么其他关联表要跟着变)。
I,Isolation,隔离性,两个事务的执行互不干扰。
D,Durability,持久性,事务完成,便将更改持久保存于数据库,不会被回滚。

2)执行控制——利用锁
共享锁,一个事务读取数据时,可以加共享锁,就是其他事务只能读取数据,不能更改数据。
独占锁,一个事务更改数据时,可以加独占锁,就是其他事务不能读取数据,也不能更改数据。

3)隔离级别
笔试题中有考到,第一时间想到赃读,不可重复读这些。隔离级别的倒忘光了。 

设定隔离级别后的现象。

\

先解释三个现象:

脏读:dirty read,事务1提交之前,事务2读取了1中的数据,1一旦回滚,2读取了不存在的数据的现象。

我本金10元,往银行存了10元,后面没存了,但是银行那边读多10元,并且按20元处理。

不可重复读:non-repeatable read,事务1读取时,事务2更新并提交,事务1再读取,发现前后数据不同。

银行查账单,发现你有10元,然后你取了10元出来,银行再查,发现余额为0。前后不同。

虚度(幻读):phatom,事务1检索多行数据,事务2插入符合检索条件的新数据,事务1第二次检索发现前后数据不同。

银行查询你的消费清单,你又跑去消费了,然后后面再查发现新消费的账单怎么不在第一次的查询里面,如幻影般消失。

read uncommited,读未提交,以上三种现象都可能会发生。
read commited,读提交,防止脏读,脏读的问题解决就是提交完成的数据才给你读,不然一旦回滚,读出来就是错的。
repeateable read,重复读,防止脏读和不可重复读,不可重复读确实是发生在事务提交之后,问题在于读取的时候又有事务进行更改,所以问题解决是,一旦读取数据,就不让你在中途进行更新(MySQL默认级别)。

serializable,序列化,防止三种现象,幻读问题是,你事务进行检索数据之后,有人插入新的数据,后面再检索发现多了一条,好比银行小姐明明就看你消费100元,后面你又刷卡1000,后面查出来的总消费是1100,这是银行小姐就慌张了。

解决问题所在就是读取数据时还禁止其他事务进行插入或者删除。

这样看来,其实事务的隔离机制就是在控制事务后其他事务不能进行的操作,隔离级别越高,条件就越多,代价越大,但是也不会出现任何的三种现象。

4)悲观锁和乐观锁

悲观锁:给加载的数据加锁,不让其他事务更新和加载,这样,脏读和不可重复读都不会出现,虚读还是有的,因为还是可以插入或者删除数据。

乐观锁:并不会像悲观那样锁死,基于数据版本的记录,更新最后的更新,这样的话,其实不可重复读的问题还是存在的。

根据不同的数据库设计和性能要求进行所需要的隔离级别,才是最恰当的。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

How to check my academic qualifications on Xuexin.com? You can check your academic qualifications on Xuexin.com, but many users don’t know how to check their academic qualifications on Xuexin.com. Next, the editor brings you a graphic tutorial on how to check your academic qualifications on Xuexin.com. Interested users come and take a look! Xuexin.com usage tutorial: How to check your academic qualifications on Xuexin.com 1. Xuexin.com entrance: https://www.chsi.com.cn/ 2. Website query: Step 1: Click on the Xuexin.com address above to enter the homepage Click [Education Query]; Step 2: On the latest webpage, click [Query] as shown by the arrow in the figure below; Step 3: Then click [Login Academic Credit File] on the new page; Step 4: On the login page Enter the information and click [Login];

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

Download the latest version of 12306 ticket booking app. It is a travel ticket purchasing software that everyone is very satisfied with. It is very convenient to go wherever you want. There are many ticket sources provided in the software. You only need to pass real-name authentication to purchase tickets online. All users You can easily buy travel tickets and air tickets and enjoy different discounts. You can also start booking reservations in advance to grab tickets. You can book hotels or special car transfers. With it, you can go where you want to go and buy tickets with one click. Traveling is simpler and more convenient, making everyone's travel experience more comfortable. Now the editor details it online Provides 12306 users with a way to view historical ticket purchase records. 1. Open Railway 12306, click My in the lower right corner, and click My Order 2. Click Paid on the order page. 3. On the paid page

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

HTML cannot read the database directly, but it can be achieved through JavaScript and AJAX. The steps include establishing a database connection, sending a query, processing the response, and updating the page. This article provides a practical example of using JavaScript, AJAX and PHP to read data from a MySQL database, showing how to dynamically display query results in an HTML page. This example uses XMLHttpRequest to establish a database connection, send a query and process the response, thereby filling data into page elements and realizing the function of HTML reading the database.

Analysis of the basic principles of MySQL database management system Analysis of the basic principles of MySQL database management system Mar 25, 2024 pm 12:42 PM

Analysis of the basic principles of the MySQL database management system MySQL is a commonly used relational database management system that uses structured query language (SQL) for data storage and management. This article will introduce the basic principles of the MySQL database management system, including database creation, data table design, data addition, deletion, modification, and other operations, and provide specific code examples. 1. Database Creation In MySQL, you first need to create a database instance to store data. The following code can create a file named "my

See all articles