我需要这3个ID才能在SQL中为我工作

时间:2018-03-20 01:56:50

标签: java database sqlite netbeans

我的项目标题是Media Advertisements DB。我希望这些以相当简洁的方式显示在Java GUI中。就像有一些选项,用户可以查看这些不同的表,但也可以显示它们,但是当我显示它们时,它是不相干的...如何使结构更好?

以下是我的表格:

enter image description here

1 个答案:

答案 0 :(得分:0)

假设合同仅限于单个客户,那么您可以在合约表中添加一列来引用该客户。

Pic我认为是客户的图片,如果是这样的话,那么就没有必要从合同中引用这个,因为引用客户的合同足以获得Pic。

因此,例如,如果使用以下内容来创建和填充表(仅包含用于测试的DROP表): -

DROP TABLE IF EXISTS customer_tb;
CREATE TABLE IF NOT EXISTS customer_tb (
    cus_id INTEGER PRIMARY KEY, 
    company TEXT, 
    firstname TEXT, 
    middlename TEXT, 
    lastname TEXT, 
    gender TEXT, 
    dob TEXT, 
    dateregistered TEXT, 
    contactno  TEXT, 
    emailaddress TEXT, 
    description TEXT, 
    pic INTEGER
);
DROP TABLE IF EXISTS mediaadv_tb;
CREATE TABLE IF NOT EXISTS mediaadv_tb (
    med_id INTEGER PRIMARY KEY, 
    mtype TEXT, 
    title TEXT, 
    dateadded TEXT, 
    previewimg BLOB, 
    filepath TEXT
);
DROP TABLE IF EXISTS contracts_tb;
CREATE TABLE IF NOT EXISTS contracts_tb (
    contract_id INTEGER PRIMARY KEY, 
    status TEXT, 
    priority TEXT, 
    dateadded TEXT, 
    dateexpiration TEXT, 
    amountpaid REAL, 
    arearofcoverage TEXT, 
    customer_reference INTEGER REFERENCES customer_tb (cus_id)
);

-- Populate customers_tb
INSERT INTO  customer_tb VALUES ("1","ACME","Fred","Tom","Smith","Male","1964-03-26","2018-0101","+54-1234 5678","fred@acme.email.com","Fred is called fred","1");
INSERT INTO customer_tb VALUES ("2","EMCA","Bert","Alan","Johns","Male","1976-10-11","2018-02-01","+54-4321 9876","bert@bertmail.com","competitive","2");
INSERT INTO customer_tb VALUES ("3","Quite Contrary Trading","Mary","Mary","QC","Female","2000-04-07","2017-02-15","+54-7654-3210","maryqc@easymail.com","Likes gardening","3");

--Populate mediaadv_tb
INSERT INTO mediaadv_tb VALUES ("1","type1","Freds photo","2018-01-01","X'00000001'","C:/images/customers/pic001");
INSERT INTO mediaadv_tb VALUES ("2","type2","Bertss photo","2018-02-01","X'00000002'","C:/images/customers/pic002");
INSERT INTO mediaadv_tb VALUES ("3","type3","Marys photo","2018-02-14","X'00000003'","C:/images/customers/pic003");

--Populate contracts_tb
INSERT INTO contracts_tb VALUES (null,"finalisation","Fast","2017-06-19","2018-05-01","123.45","Here and there","3");
INSERT INTO contracts_tb VALUES (null,"pending","medium","2017-06-19","2018-05-01","457.89","Over there","2");
INSERT INTO contracts_tb VALUES (null,"negotiating","TBA","2018-01-01","2020-01-01","567.65","Overall","3");
INSERT INTO contracts_tb VALUES (null,"building","Super fast","2017-11-01","2018-06-01","333.21","London","1");

结果表将是: -

enter image description here

enter image description here

enter image description here

以下查询基于与customers表连接的合约表,然后是加入mediaadv表的customers表: -

SELECT contract_id, 
    dateexpiration,
    status,
    priority, 
    firstname||' '||middlename||' '||lastname AS fullname, 
    company, 
    title, 
    previewimg 
FROM contracts_tb 
    JOIN customer_tb ON customer_reference = customer_tb.cus_id 
    JOIN mediaadv_tb ON mediaadv_tb.med_id = pic
ORDER BY dateexpiration ASC

会导致: -

enter image description here

现在,cutomer_tb和mediaadv_tb可以合并为一个表。但是我没有这样做,因为它根本不清楚表格会有什么用处。