3.9 MySQL 不支持的功能
本节介绍其他数据库中有而MySQL 中无的功能。它介绍省略了什么功能,以及在需要这些功能时怎么办。一般情况下, MySQL 之所以忽略某些功能是因为它们有负面性能影响。有的功能正在开发者的计划清单上,一旦找到一种方法可以实现相应的功能而又不致于影响良好性能的目标,就会对它们进行实现。
■ 子选择。子选择是嵌套在另一个SELECT 语句内的SELECT 语句,如下面的查询所示:
SELECT * FROM score
WHERE event_id IN (SELECT event_id FROM event WHERE type = "T")
子选择打算在MySQL 3.24 中给出,到那时它们就不会忽略了。但到那时,许多用子选择撰写的查询也可以用连接来编写。请参阅3 . 8 . 1节“将子选择编写为连接”。
■ 事务处理和提交/回退。事务处理是由其他客户机作为一个整体不中断执行的一组S Q L语句。提交/回退功能允许规定数条语句作为一个整体执行或不执行。即,如果事务处理中的任何一条语句失败,那么直到该语句前执行的所有语句的作用都被撤消。
ySQL 自动进行单一SQL 语句的同步以免客户机互相干扰。(例如,两个客户机不能对相同的表进行同时写入。)此外,可利用LOCK TABLES 和UNLOCK TA B L ES将数条语句组成一个整体,这使您能够完成单条语句的并发控制所不能满足的操作。MySQL 与事务处理有关的问题是,它不能自动对数条语句进行组织,而且如果这些语句中有某一条失败后也不能对它们进行回退。
为了弄清事务处理为什么有用,可举例说明。假如您在服装销售业工作,无论何时,只要您的销售人员进行了一次销售,都要更新库存数目。下面的例子说明了在多个销售人员同时更新数据库时可能出现的问题(假如初始的衬衫库存数目为4 7):
t1 销售人员1卖出3件衬衫
t2 销售人员检索当前衬衫计数( 4 7):
SELECT quantity FROM inventory WHERE item = "shirt"
t3 销售人员2卖出2件衬衫
t4 销售人员2检索当前衬衫计数( 4 7)
SELECT quantity FROM inventory WHERE item = "shirt"
t5 销售人员1计算库存的新数目为47 - 3 = 44 并设置衬衫计数为44:
UPDATE inventory SET quantity = 44 WHERE item = "shirt"
t6 销售人员2计算库存的新数目为47 - 2 = 45 并设置衬衫计数为45:
UPDATE inventory SET quantity = 45 WHERE item = "shirt"
在这个事件序列结束时,您已经卖掉了5 件衬衫,但库存数目却是45 而不是4 2。问题是如果在一条语句中查看库存而在另一条语句中更新其值,这是一个多语句的事务处理。第二条语句中所进行的活动取决于第一条语句中检索出的值。但是如果在重叠的时间范围内出现独立的事务处理,则每个事务处理的语句会纠缠在一起,并且互相干扰。在事务处理型的数据库中,每个销售人员的语句可作为一个事务处理执行,这样,销售人员2 的语句在销售人员1 的语句完成之前不会被执行。在MySQL 中,可用两种方法达到这个目的:
■ 方法1:作为一个整体执行一组语句。可利用LOCK TABLES 和UNLOCK TABLES将语句组织在一起,并将它们作为一个原子单元执行:锁住所需使用的表,发布查询,然后释放这些锁。这样阻止了其他人在您锁住这些表时使用它们。利用表同步,库存情况如下所示:
t1 销售人员1卖出3件衬衫
t2 销售人员1请求一个锁并检索当前衬衫计数(47)
LOCK TABLES inventory WRITE
SELECT quantity FROM inventory WHERE item = "shirt"
t3 销售人员2卖出2件衬衫
t4 销售人员2试图取得一个锁:这被阻塞,因为销售人员1 已经占住了锁:
LOCK TABLES inventory WRITE
t5 销售人员1计算库存的新数目为47 - 3 = 44 并设置衬衫计数为44,然后释放锁:
UPDATE inventory SET quantity = 44 WHERE item = "shirt"
UNLOCK TABLES
t6 现在销售人员2的锁请求成功。销售人员2检索当前衬衫计数( 44)
SELECT quantity FROM inventory WHERE item = "shirt"
t7 销售人员2计算库存的新数目为44 - 2 = 42,设置衬衫计数为4 2,然后释放锁:
UPDATE inventory SET quantity = 42 WHERE item = "shirt"
UNLOCK TABLES

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
