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
網友評論