SQL Server 中的事务是什么?
SQL Server 中的事务是一组被视为一个单元的 SQL 语句,它们按照“做所有事或不做任何事”的原则执行,成功的事务必须通过 ACID 测试。
事务的 ACID 属性是什么?
首字母缩写词 ACID 是指事务的四个关键属性
- 原子性: Atomicity
- 一致性: Consistency
- 隔离性: Isolation
- 持久性: Durability
为了理解这一点,我们将使用以下两个表测试。
Product (产品表)
ProductID |
Name |
Price |
Quantity |
101 |
Laptop |
15000 |
100 |
102 |
Desktop |
20000 |
150 |
104 |
Mobile |
3000 |
200 |
105 |
Tablet |
4000 |
250 |
ProductSales (产品销售表)
ProductSalesID |
ProductID |
QuantitySold |
1 |
101 |
10 |
2 |
102 |
15 |
3 |
104 |
30 |
4 |
105 |
35 |
请使用以下 SQL 脚本创建并使用示例数据填充 Product 和 ProductSales 表。
IF OBJECT_ID(‘dbo.Product’,’U’) IS NOT NULL
DROP TABLE dbo.Product
IF OBJECT_ID(‘dbo.ProductSales’,’U’) IS NOT NULL
DROP TABLE dbo.ProductSales
GO
CREATE TABLE Product
(
ProductID INT PRIMARY KEY,
Name VARCHAR(40),
Price INT,
Quantity INT
)
GO
INSERT INTO Product VALUES(101, ‘Laptop’, 15000, 100)
INSERT INTO Product VALUES(102, ‘Desktop’, 20000, 150)
INSERT INTO Product VALUES(103, ‘Mobile’, 3000, 200)
INSERT INTO Product VALUES(104, ‘Tablet’, 4000, 250)
GO
CREATE TABLE ProductSales
(
ProductSalesId INT PRIMARY KEY,
ProductId INT,
QuantitySold INT
)
GO
INSERT INTO ProductSales VALUES(1, 101, 10)
INSERT INTO ProductSales VALUES(2, 102, 15)
INSERT INTO ProductSales VALUES(3, 103, 30)
INSERT INTO ProductSales VALUES(4, 104, 35)
GO
网友评论