我是SQL的初学者,我正在使用SQLite编写C#应用程序。我需要在数据库中实现此处https://imgur.com/a/NImUPCc所示的层次结构的帮助。我知道使用私有帐户时,它将是一个称为PrivateAccounts的表(应该称其为PrivateAccounts不是私有的,但现在没什么关系),在那里我会抛出许多行,其中包含ID,Logins等。但是该怎么办与公司帐户一起使用,在哪里有公司的一般帐户,我需要将在该公司工作的人员的帐户链接到该帐户吗?为每家公司创建表会高效吗?不过,这会使数据库混乱,因为如果可能的话,我想为公司帐户和个人帐户建立一个数据库
答案 0 :(得分:0)
我建议3张桌子。
实体表,其中包含帐户所有者的详细信息(并通过对拥有实体的引用来满足子公司的要求),并具有用于类型(私人或公司)的列。
具有帐户名称和所有者的帐户表。
用于记录交易的 Account Transaction 表,基本上有两列引用了帐户(因此是实体)和交易金额。
以上内容非常简单,您可能想添加更多列。
下面是一个简短的演示,其最终显示了帐户余额,帐户名称,直接所有者名称和显示的帐户类型。
DROP TABLE IF EXISTS entity;
DROP TABLE IF EXISTS account;
DROP TABLE IF EXISTS account_transaction;
CREATE TABLE IF NOT EXISTS entity (id INTEGER PRIMARY KEY, entity_name TEXT, entity_type INTEGER, entity_owning_entity);
CREATE TABLE IF NOT EXISTS account (id INTEGER PRIMARY KEY, account_name TEXT, entity_reference INTEGER);
CREATE TABLE IF NOT EXISTS account_transaction (id INTEGER PRIMARY KEY, account_reference INTEGER, amount REAL);
INSERT INTO entity (entity_name, entity_type, entity_owning_entity)
VALUES
("Company 1",1,null), -- will be id 1
("Company 2",1, null), -- will be id 2
("Sub Company A",1,1), -- Owned by Company 1 will be id 3
("Sub Company B",1,1), -- Owned by Company 1 will be id 4
("Sub Company X",1,2), -- Owned by Company 2 will be id 5
("Sub Company Y",1,2), -- Owned by Company 2 will be id 6
("Fred",0,null), -- Not owned personal type (0) will be id 7
("Mary",0,null) -- Not owned, personal type, will be id 8
;
INSERT INTO account (account_name, entity_reference)
VALUES
("Fred's Cheque",7), -- id 1
("Sub A Cheque",3), -- id 2
("Fred's Savings",7), -- id 3
("Mary's Cheque",8), -- id 4
("Sub X Chequ",5) -- id 5
;
INSERT INTO account_transaction (account_reference, amount)
VALUES
(3,123.45),(3,57.55),(3,200), -- 381 for Fred's Cheque (i.e. references id 3 in the account table)
(2,100),(2,100),(2,100),(2,100), -- 400 for Sub A Cheque (i.e. references id 2)
(1,10),
(4,15),(4,15),(4,30),
(5,13),(5,14),(5,15),(5,16),(5,17)
;
WITH cte1(id,name) AS
-- Use Common Table Expression (temp table) to resolve owner of an entity
(SELECT id, (SELECT entity_name FROM entity WHERE me.entity_owning_entity = id) FROM entity AS me)
-- Main SELECT Query
SELECT
-- Handle case when no entity owner or when entity owner
CASE
WHEN (SELECT name FROM cte1 where cte1.id = entity.id) IS NULL
THEN entity_name
ELSE (SELECT name FROM cte1 where cte1.id = entity.id)||'-'||entity_name
END AS AccountOwner,
account_name AS Account, -- Show the axccount_name
sum(amount) AS Balance, -- Show the balance
-- Use and appropriate value for the type (private or company account)
CASE
WHEN entity_type = 1 THEN 'Company'
WHEN entity_type = 0 THEN 'Private'
END AS Type
FROM account_transaction -- main table
JOIN account ON account.id = account_reference -- relationship to the account table
JOIN entity ON entity.id = entity_reference -- relationship to the entity table
GROUP BY account.id -- group according to accounts (i.e. 1 row is made from all rows in a group for aggregates such a sum)
ORDER BY AccountOwner -- Order result according to the Account Owner
;
SELECT查询将返回:-