我正在尝试记录表中数据的更改。我已经阅读了有很多方法可以做到这一点听起来更方便,但在我的情况下,我想用触发器来实现这个功能。我有两张桌子:
CREATE TABLE Applications
(
Application_ID INT NOT NULL,
Student_ID INT NOT NULL,
Job_ID INT NOT NULL,
ApplicationChange_TYPE VARCHAR(64) NOT NULL,
PRIMARY KEY (Application_ID),
FOREIGN KEY (Student_ID) REFERENCES Students(Student_ID),
FOREIGN KEY (Job_ID) REFERENCES Jobs(Job_ID),
CONSTRAINT CK_TYPE CHECK (ApplicationChange_TYPE in ('Submitted', 'Withdrawn', 'Invited For Interview', 'Invited to Assessment Centre', 'Rejected', 'Accepted'))
);
和
CREATE TABLE ApplicationChanges
(
ApplicationChange_ID INT NOT NULL,
Application_ID INT NOT NULL,
ApplicationChange_TYPE VARCHAR(64),
ApplicationChange_DATETIME DATE NOT NULL,
PRIMARY KEY (ApplicationChange_ID),
FOREIGN KEY (Application_ID) REFERENCES Applications(Application_ID),
CONSTRAINT CK_APPTYPE CHECK (ApplicationChange_TYPE in ('Submitted', 'Withdrawn', 'Invited For Interview', 'Invited to Assessment Centre', 'Rejected', 'Accepted'))
);
我想创建一个触发器,以便在编辑/更新Applications表中记录的ApplicationChange_TYPE字段时,在ApplicationChanges表中创建一条新记录。我需要它将Application_ID和ApplicationChange_TYPE从应用程序表中已编辑的记录转移到ApplicationChanges中的新记录。应使用当前日期填充ApplicationChange_DATETIME字段。
当前,两个表的主键在创建新记录时使用序列并触发自动ID号。
任何输入都将非常感谢!如果我一点都不清楚,请道歉。
编辑:
此外,我已经尝试过这一点,但我对它不太了解,而且它看起来很破碎,以至于我认为包含它没有任何意义。但这是:
CREATE OR REPLACE TRIGGER ApplicationsUpdateTrigger
AFTER UPDATE ON Applications
BEGIN
INSERT ApplicationChanges (Application_ID, ApplicationStatus_TYPE, ApplicationChange_TYPE)
SELECT Application_ID, ApplicationStatus_TYPE FROM Applications
JOIN inserted ON inserted.Application_ID = Applications.Application_ID
INSERT INTO ApplicationChanges (Application_ID, ApplicationStatus_TYPE, ApplicationChange_DATETIME)
VALUES (NULL, Application_ID, ApplicationStatus_TYPE, GETDATE())
END;
/
答案 0 :(得分:0)
我会坚持使用Oracle作为问号标签(尽管在示例触发器中使用GETDATE()建议使用SQL Server)。
我不确定您要在ApplicationChanges表中存储哪个ApplicationChange_TYPE值:更改前的那个(旧的)还是更改后的(新的)?
让我们介绍这两种变体,假设自动递增id列的序列和触发器工作正常。
旧价值:
private void button_confirm_Click(object sender, EventArgs e)
{
listBox_savedata.Items.Add(textBox_ordertostart.Text +" "+ comboBox_suburb.SelectedItem.ToString() + " " + dateTimePicker2.Value.ToShortDateString());
for (int i = 0; i <= listBox_savedata.Items.Count; i++)
{
string s1 = listBox_savedata.Items[i].ToString();
int startpos = s1.IndexOf("PM");
string sub = s1.Substring(0, 5);
if(sub+" "+"PM"==DateTime.Now.ToString("HH:mm tt"))
{
mplayer.PlayLooping();
}
}
}
新价值:
CREATE OR REPLACE TRIGGER ApplicationsUpdateTrigger
AFTER UPDATE ON Applications
FOR EACH ROW
BEGIN
IF (:OLD.ApplicationChange_TYPE IS NULL AND :NEW.ApplicationChange_TYPE IS NOT NULL) -- this one is actually redundunt as long as you've got NOT NULL constraint on Application.ApplicationChange_TYPE column
OR (:OLD.ApplicationChange_TYPE <> :NEW.ApplicationChange_TYPE)
THEN
INSERT INTO ApplicationChanges (Application_ID, ApplicationChange_TYPE, ApplicationChange_DATETIME)
VALUES (:OLD.Application_ID, :OLD.ApplicationChange_TYPE, sysdate);
END IF;
END;
/
差异很合理:第二种变体适用于UPDATE和INSERT并使用:新记录值而不是:旧记录值。