Home Database Mysql Tutorial SQL锁表语句

SQL锁表语句

Jun 07, 2016 pm 03:09 PM
select sql database statement locking lock table

锁定数据库的一个表 SELECT * FROM table WITH (HOLDLOCK) 注意: 锁定数据库的一个表的区别 SELECT * FROM table WITH (HOLDLOCK) 其他事务可以读取表,但不能更新删除 SELECT * FROM table WITH (TABLOCKX) 其他事务不能读取表,更新和删除 SELECT 语句中加

锁定数据库的一个表

SELECT * FROM table WITH (HOLDLOCK)

注意: 锁定数据库的一个表的区别

SELECT * FROM table WITH (HOLDLOCK)

其他事务可以读取表,但不能更新删除

SELECT * FROM table WITH (TABLOCKX)

其他事务不能读取表,更新和删除

SELECT 语句中“加锁选项”的功能说明

SQL Server提供了强大而完备的锁机制来帮助实现数据库系统的并发性和高性能。用户既能使用SQL Server的缺省设置也可以在select 语句中使用“加锁选项”来实现预期的效果。 本文介绍了SELECT语句中的各项“加锁选项”以及相应的功能说明。

功能说明: 

NOLOCK(不加锁)

此选项被选中时,SQL Server 在读取或修改数据时不加任何锁。 在这种情况下,用户有可能读取到未完成事务(Uncommited Transaction)或回滚(Roll Back)中的数据, 即所谓的“脏数据”。

HOLDLOCK(保持锁)

此选项被选中时,SQL Server 会将此共享锁保持至整个事务结束,而不会在途中释放。

UPDLOCK(修改锁)

此选项被选中时,SQL Server 在读取数据时使用修改锁来代替共享锁,并将此锁保持至整个事务或命令结束。使用此选项能够保证多个进程能同时读取数据但只有该进程能修改数据。

TABLOCK(表锁)

此选项被选中时,SQL Server 将在整个表上置共享锁直至该命令结束。 这个选项保证其他进程只能读取而不能修改数据。

PAGLOCK(页锁)

此选项为默认选项, 当被选中时,SQL Server 使用共享页锁。

TABLOCKX(排它表锁)

此选项被选中时,SQL Server 将在整个表上置排它锁直至该命令或事务结束。这将防止其他进程读取或修改表中的数据。

 

HOLDLOCK 持有共享锁,直到整个事务完成,应该在被锁对象不需要时立即释放,等于SERIALIZABLE事务隔离级别 

  NOLOCK 语句执行时不发出共享锁,允许脏读 ,等于 READ UNCOMMITTED事务隔离级别 

  PAGLOCK 在使用一个表锁的地方用多个页锁 

  READPAST 让sql server跳过任何锁定行,执行事务,适用于READ UNCOMMITTED事务隔离级别只跳过RID锁,不跳过页,区域和表锁 

  ROWLOCK 强制使用行锁 

  TABLOCKX 强制使用独占表级锁,这个锁在事务期间阻止任何其他事务使用这个表 

  UPLOCK 强制在读表时使用更新而不用共享锁

注意: 锁定数据库的一个表的区别 

  SELECT * FROM table WITH (HOLDLOCK) 其他事务可以读取表,但不能更新删除 

  SELECT * FROM table WITH (TABLOCKX) 其他事务不能读取表,更新和删除

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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

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)

Hot Topics

PHP Tutorial
1504
276
Go select with default case Go select with default case Jul 14, 2025 am 02:54 AM

The purpose of select plus default is to allow select to perform default behavior when no other branches are ready to avoid program blocking. 1. When receiving data from the channel without blocking, if the channel is empty, it will directly enter the default branch; 2. In combination with time. After or ticker, try to send data regularly. If the channel is full, it will not block and skip; 3. Prevent deadlocks, avoid program stuck when uncertain whether the channel is closed; when using it, please note that the default branch will be executed immediately and cannot be abused, and default and case are mutually exclusive and will not be executed at the same time.

How to create empty tables with the same structure as another table? How to create empty tables with the same structure as another table? Jul 11, 2025 am 01:51 AM

You can use SQL's CREATETABLE statement and SELECT clause to create a table with the same structure as another table. The specific steps are as follows: 1. Create an empty table using CREATETABLEnew_tableASSELECT*FROMexisting_tableWHERE1=0;. 2. Manually add indexes, foreign keys, triggers, etc. when necessary to ensure that the new table is intact and consistent with the original table structure.

How to seed a database in Laravel? How to seed a database in Laravel? Jul 28, 2025 am 04:23 AM

Create a seeder file: Use phpartisanmake:seederUserSeeder to generate the seeder class, and insert data through the model factory or database query in the run method; 2. Call other seeder in DatabaseSeeder: register UserSeeder, PostSeeder, etc. in order through $this->call() to ensure the dependency is correct; 3. Run seeder: execute phpartisandb:seed to run all registered seeders, or use phpartisanmigrate:fresh--seed to reset and refill the data; 4

Optimizing Database Interactions in a Java Application Optimizing Database Interactions in a Java Application Jul 27, 2025 am 02:32 AM

UseconnectionpoolingwithHikariCPtoreusedatabaseconnectionsandreduceoverhead.2.UsePreparedStatementtopreventSQLinjectionandimprovequeryperformance.3.Fetchonlyrequireddatabyselectingspecificcolumnsandapplyingfiltersandpagination.4.Usebatchoperationstor

Restoring a MySQL database from a mysqldump backup Restoring a MySQL database from a mysqldump backup Jul 10, 2025 pm 01:16 PM

TorestoreaMySQLdatabasefromamysqldumpbackup,firstconfirmthecorrect.sqlfilebycheckingCREATEDATABASEandUSEstatements,extractifcompressed,andensurediskspaceandpermissions.Next,createanemptydatabasemanuallyifthedumplacksCREATEDATABASE.Then,usemysql-uuser

How to find the Nth highest value in a SQL column? (e.g., second highest salary) How to find the Nth highest value in a SQL column? (e.g., second highest salary) Jul 12, 2025 am 01:58 AM

There are three common methods to find the Nth highest value of a column in SQL. 1. Use subquery and LIMIT/OFFSET: First sort the target column in descending order, skip the first N-1 record and then take one. It is suitable for simple scenarios but may affect performance; 2. Exclude maximum values ​​layer by layer through nested subqueries: the logic is clear but the structure is complex when the hierarchy increases; 3. Use DENSE_RANK or ROW_NUMBER window function (recommended): Flexible processing of duplicate values, supports precise ranking, suitable for database environments that support window functions. Which method to choose depends on the specific database type, data volume and structural requirements.

Relational Database Design Principles for SQL Developers Relational Database Design Principles for SQL Developers Jul 21, 2025 am 01:56 AM

When designing a relational database, four key principles should be followed. First, correctly use primary and foreign key constraints to ensure data integrity and association accuracy; second, perform standardized design reasonably, usually reaching the third normal form (3NF), eliminating redundancy and ensuring data consistency; third, establishing appropriate indexes for common queries to improve query performance but avoid over-index; finally, using consistent naming specifications and structural styles to enhance readability and maintainability. Mastering these principles can help build a clear, efficient and robust database structure.

How to execute SQL queries in Python? How to execute SQL queries in Python? Aug 02, 2025 am 01:56 AM

Install the corresponding database driver; 2. Use connect() to connect to the database; 3. Create a cursor object; 4. Use execute() or executemany() to execute SQL and use parameterized query to prevent injection; 5. Use fetchall(), etc. to obtain results; 6. Commit() is required after modification; 7. Finally, close the connection or use a context manager to automatically handle it; the complete process ensures that SQL operations are safe and efficient.

See all articles