What is ACID?

20/11/2023 - 2 phút

Follow  on Google News

What is ACID?

ACID is an acronym for four important properties in transaction management in database systems:

  1. Atomicity: Atomicity ensures that a transaction will be performed atomically. It means that the transaction will either complete or not complete, there is no intermediate state. If a part of the transaction fails, the entire transaction will be cancelled and the data will return to its original state.

  2. Consistency: Consistency ensures that a transaction will bring the database from one valid state to another valid state. It ensures that the data will always comply with the rules and constraints previously defined in the database.

  3. Isolation: Isolation ensures that a transaction does not affect other transactions happening simultaneously. It prevents access to uncommitted data of a transaction by other transactions.

  4. Durability: Durability ensures that once a transaction has been committed, the stored data will persist and will not be lost even in the event of system failures such as hardware errors or power outages.

Example

erDiagram
    Customer||--o{Transaction:"Has"
    Transaction}o--o{TransactionHistory:"Belongs to"

    Customer{
        int ID
        string Name
        string Address
        int AccountBalance
    }
    Transaction{
        int ID
        string TransactionType
        string TransactionDate
        int Amount
    }
    TransactionHistory{
        int ID
        int CustomerID
        int TransactionID
        string Description
    }

We have the following tables:

“Customers” Table:

ID (Primary Key)NameAddressAccount Balance
1Alice123 Elm Street1000
2Bob456 Oak Street800
3Charlie789 Maple Avenue1500

“Transactions” Table:

ID (Primary Key)Transaction TypeTransaction DateAmount
1Transfer2023-11-01200
2Withdrawal2023-11-0250
3Deposit2023-11-03100
4Transfer2023-11-04300
5Withdrawal2023-11-0575

“Transaction History” Table:

ID (Primary Key)Customer ID (Foreign Key)Transaction ID (Foreign Key)Description
111Transfer 200 to Bob
222Withdraw 50 from Bob
313Deposit 100 to Alice
414Transfer 300 to Charlie
535Withdraw 75 from Charlie

In this example, the “Customers” table contains information about the customers and their account balances. The “Transactions” table contains information about the bank transactions, including the type of transaction, the transaction date, and the amount involved. The “Transaction History” table records the transaction history of each customer, including a detailed description of each transaction.

The ACID properties such as Atomicity, Consistency, Isolation, and Durability ensure that the transactions are performed safely, and the data is not lost or inconsistent in case of failures.