How to design an extensible accounting system table structure in MySQL to support multiple currencies and exchange rate processing?
As the global economy develops, more and more businesses need to handle multiple currencies and exchange rates in their accounting systems. In order to effectively support this requirement, we need to design an extensible table structure in the database.
In MySQL, we can design an scalable accounting system table structure through the following steps.
CREATE TABLE currency (
id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, symbol VARCHAR(10) NOT NULL
);
CREATE TABLE exchange_rate (
id INT PRIMARY KEY AUTO_INCREMENT, base_currency_id INT NOT NULL, target_currency_id INT NOT NULL, rate DECIMAL(10, 4) NOT NULL, effective_date DATE NOT NULL, FOREIGN KEY (base_currency_id) REFERENCES currency(id), FOREIGN KEY (target_currency_id) REFERENCES currency(id)
);
CREATE TABLE transaction (
id INT PRIMARY KEY AUTO_INCREMENT, transaction_date DATE NOT NULL, currency_id INT NOT NULL, amount DECIMAL(10, 2) NOT NULL, exchange_rate_id INT NOT NULL, FOREIGN KEY (currency_id) REFERENCES currency(id), FOREIGN KEY (exchange_rate_id) REFERENCES exchange_rate(id)
);
SELECT c.symbol, SUM(t.amount) AS total_amount
FROM transaction t
JOIN currency c ON t.currency_id = c.id
WHERE t.transaction_date = '2022-01-01'
GROUP BY c.symbol;
This query will return the total transaction amount for each currency on a specific date (2022-01-01).
Summary:
Through the above design, we can create an extensible accounting system table structure in MySQL to support multiple currencies and exchange rate processing. The currency table stores information about different currencies, the exchange rate table stores exchange rate information between different currencies, and the transaction table stores transaction information between currencies. By using the above table structure, we can perform various queries and operations to meet different accounting needs.
The above is the detailed content of How to design a scalable accounting system table structure in MySQL to support multiple currencies and exchange rate processing?. For more information, please follow other related articles on the PHP Chinese website!