如何使用MySQL建構一個靈活可擴展的會計系統表結構
2.1 公司表(company)
公司表用於儲存企業的基本信息,包括公司名稱、註冊地址、聯絡資訊等。
CREATE TABLE company (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
address VARCHAR(200),
phone VARCHAR(20),
. ..
);
2.2 部門表(department)
部門表用於儲存企業的部門信息,每個部門都有一個唯一的部門編號作為主鍵,還包括部門名稱、所屬公司等字段。
CREATE TABLE department (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
company_id INT,
FOREIGN KEY (company_id) REFERENCES company_id INT,
FOREIGN KEY (company_id) REFERENCES company(id),
...
2.3 員工表(employee)
CREATE TABLE employee (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100),
emp_number VARCHAR(20),
department_id INT,
FOREIGN KEY (department_ ) REFERENCES department(id),
...
2.4 帳戶表(account)
CREATE TABLE account (
account_number VARCHAR(20) PRIMARY KEY,
name VARCHAR(100),
company_id INT,
FOREIGN KEY (company_id) REFERENCES company_id INT,
f
);
交易表用於儲存企業的交易記錄,每個交易都有一個唯一的交易號碼作為主鍵,並包括交易日期、交易金額、交易類型等欄位。
CREATE TABLE transaction (
id INT PRIMARY KEY AUTO_INCREMENT,
transaction_number VARCHAR(20) UNIQUE,
transaction_date DATE,
amount DECIMAL(10, 2),#type VARCHAR(20),
...
2.6 帳戶交易表(account_transaction)
CREATE TABLE account_transaction (
id INT PRIMARY KEY AUTO_INCREMENT,
account_number VARCHAR(20),
transaction_id INT,
amount DECIMAL(10, 2),#fORE KIGN KEY (account_number) REFERENCES account(account_number),
FOREIGN KEY (transaction_id) REFERENCES transaction(id),
...
);
3.1 查詢某部門的員工清單
SELECT e.name, e.emp_number, d.name AS department_name
FROM employee e
JOIN department d ON e.department_id = d. id
WHERE d.id =
3.2 查詢某個帳戶的交易記錄
SELECT t.transaction_number, t.transaction_date, t.amount, t.type
FROM transaction t
JOIN account_transaction at ON t.id = at.transaction_id
JOIN account a ON at.account_number = a.account_number
WHERE a.account_number =
以上是如何使用MySQL建構一個靈活可擴展的會計系統表結構?的詳細內容。更多資訊請關注PHP中文網其他相關文章!