如何在条件为真时自动在一个表中添加值

时间:2018-04-27 18:21:36

标签: sql sqlite android-sqlite

所以,这是条件。

我有User_tbl,其代码如下

CREATE TABLE Users_tbl (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    username TEXT,
    password TEXT,
    user_type INT
);

user_type是0,1或2.如果是0那么它是玩家,1是教练,2是观众。

现在,我想创建一个新表,其中包含coach列表。架构将是

CREATE TABLE coach_tbl(
coach_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
username TEXT,
password TEXT
)

我需要的是,当Users_tbl中的条目有user_type = 1时,它应该触发另一个查询,该查询将在coach_tbl中创建一个条目并填写列。它应该动态发生。

1 个答案:

答案 0 :(得分:0)

以下TRIGGER将实现您的目标: -

CREATE TRIGGER setup_coach 
AFTER INSERT ON Users_tbl
WHEN new.user_type = 1
    BEGIN 
        INSERT INTO coach_tbl (username, password) VALUES(new.username,new.password);
    END
;

以下用于测试上述内容: -

DROP TABLE IF EXISTS Users_tbl;
CREATE TABLE IF NOT EXISTS Users_tbl (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    username TEXT,
    password TEXT,
    user_type INT
);
DROP TABLE IF EXISTS coach_tbl;
CREATE TABLE IF NOT EXISTS coach_tbl(
coach_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
username TEXT,
password TEXT
);

CREATE TRIGGER setup_coach 
AFTER INSERT ON Users_tbl
WHEN new.user_type = 1
    BEGIN 
        INSERT INTO coach_tbl (username, password) VALUES(new.username,new.password);
    END
;
INSERT INTO Users_tbl (username, password,user_type) 
VALUES 
    ('Fred','fred1234',0),
    ('Bert','bert1234',1),
    ('Harold','harold1234',0),
    ('Alan','alan1234',1);

结果是: -

enter image description here

补充(又名为什么不是上述)

存储完全相同的值。即复制它们,与规范化相反,可能导致问题。例如如果(使用上述)“Bert”的名字或“密码”改变了,你必须在两个地方改变它。目前没有必要复制这些数据,因为它很容易获得

例如,如果您想列出可以使用的教练: -

SELECT username FROM coach_tbl;

但是,使用以下内容完全相同但没有附加表: -

SELECT username FROM Users_tbl WHERE user_type = 1;

假设您有教练特定信息,例如说教练的次数,那么您可以有一个额外的表格,如: -

CREATE TABLE IF NOT EXISTS coaching_information
    (
    user_id_reference INTEGER REFERENCES Users_tbl(id),
    number_of_times_coaching INTEGER DEFAULT 0
    )
;

与TRIGGER一起自动添加默认辅导信息: -

CREATE TRIGGER autoadd_coaching_information_entry
AFTER INSERT ON Users_tbl 
WHEN new.user_type = 1
BEGIN 
    INSERT INTO coaching_information
        (user_id_reference)
        VALUES (new.id)
        ;
END
;
  • 注意!无需设置 number_of_times_coaching 列,因为它默认为0。

假设所有TABLES都已清空,然后使用(再次): -

INSERT INTO Users_tbl (username, password,user_type) 
VALUES 
    ('Fred','fred1234',0),
    ('Bert','bert1234',1),
    ('Harold','harold1234',0),
    ('Alan','alan1234',1);

导致: -

enter image description here

  • 2指伯特,4指艾伦

然后你可以列出所有没有执教过的教练(选择因为没有更新教练信息的懒惰): -

SELECT 'Coach '||username||' is up for a coaching experience.' AS coaching_needed 
FROM coaching_information 
JOIN Users_tbl ON Users_tbl.id = coaching_information.user_id_reference
WHERE coaching_information.number_of_times_coaching < 1 

结果将是: -

enter image description here

Say Bert改名为Charles,例如使用UPDATE Users_tbl SET username = 'Charles' WHERE username = 'Bert';

然后只需更改1,上述查询的结果就是: -

enter image description here

用于测试上述内容的完整SQL是: -

DROP TABLE IF EXISTS coach_tbl;
DROP TABLE IF EXISTS Users_tbl;
CREATE TABLE IF NOT EXISTS Users_tbl (
    id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
    username TEXT,
    password TEXT,
    user_type INT
);

DROP TRIGGER IF EXISTS setup_coach; -- get rid of defunct TRIGGER
CREATE TABLE IF NOT EXISTS coaching_information
    (
    user_id_reference INTEGER REFERENCES Users_tbl(id) ON DELETE CASCADE,
        -- Note ON DELETE CASCADE will automatically delete a coach should 
        -- the coach's User_tbl entry be deleted i.e. the deletion is propoagted
        -- to the children of the parent.
        -- (could also use ON UPDATE CASCADE but unlikely to change the underlying id)
    number_of_times_coaching INTEGER DEFAULT 0
    )
;

DROP TRIGGER IF EXISTS autoadd_coaching_information_entry;
CREATE TRIGGER autoadd_coaching_information_entry
AFTER INSERT ON Users_tbl 
WHEN new.user_type = 1
BEGIN 
    INSERT INTO coaching_information
        (user_id_reference)
        VALUES (new.id)
        ;
END

;
INSERT INTO Users_tbl (username, password,user_type) 
VALUES 
    ('Fred','fred1234',0),
    ('Bert','bert1234',1),
    ('Harold','harold1234',0),
    ('Alan','alan1234',1)
;
--
-- Note! oncommmenting this will change Bert's name to Charles
--UPDATE Users_tbl SET username = 'Charles' WHERE username = 'Bert';
--
SELECT 'Coach '||username||' is up for a coaching experience.' AS coaching_needed 
FROM coaching_information 
JOIN Users_tbl ON Users_tbl.id = coaching_information.user_id_reference
WHERE coaching_information.number_of_times_coaching < 1