好的,这是一个直截了当的问题,但我在如何实施解决方案方面遇到了问题。
所以这就是数据库结构的样子,对于所有需要的东西都大大减少了。
表事件、联系人、contact_event_role、event_roles。
create table events(
event_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
name VARCHAR(200) NOT NULL,
PRIMARY KEY(event_id)
);
INSERT INTO events VALUES(1, 'stackoverflow');
INSERT INTO events VALUES(2, 'throwsanerror');
create table contacts(
contact_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
fname VARCHAR(40) NOT NULL,
lname VARCHAR(40) NOT NULL,
email VARCHAR(90) NOT NULL,
PRIMARY KEY(contact_id)
);
INSERT INTO contacts VALUES(1, 'bill', 'smith', 'bsmith@email.com');
INSERT INTO contacts VALUES(2, 'amy', 'lee', 'amylee@email.com');
event_roles(
role_id INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
description VARCHAR(80),
PRIMARY KEY(role_id)
);
//The roles look something like this
INSERT INTO event_roles VALUES(1, 'Event Coordinator');
INSERT INTO event_roles VALUES(2, 'Decision Maker');
INSERT INTO event_roles VALUES(3, 'Inquiry Contact');
contacts_event_role(
event_id INTEGER UNSIGNED NOT NULL,
contact_id INTEGER UNSIGNED NOT NULL,
role_id INTEGER UNSIGNED NOT NULL,
FOREIGN KEY(event_id) REFERENCES events(event_id),
FOREIGN KEY(contact_id) REFERENCES contacts(contact_id),
FOREIGN KEY(role_id) REFERENCES event_roles(role_id),
PRIMARY KEY(event_id, role_id)
);
INSERT INTO event_role VALUES(1, 1, 1);
INSERT INTO event_role VALUES(1, 1, 2);
INSERT INTO event_role VALUES(2, 2, 1);
这就是数据库的要点。带有一些虚拟数据。很确定那里一切都很好。
所以这是我的逻辑
我要做的是插入/更新客户端和角色,并在必要时让客户端填充多个角色。
所以我的伪代码看起来像这样
//perform a check to see if the event_role is being filled...
check4role = SELECT * FROM contacts_event_role WHERE role_id = 1 AND event_id = 1
//perform a check to see if the contact already exists.
check4contact = SELECT * FROM contacts WHERE fname = :fname AND lname = :lname AND email = :email;
//if the role is already being filled && contact exists
if( check4role == true && check4contact == true)
UPDATE contact_event_role
//else if the role exists and contact does not exists
elseif( check4role == true && check4contact == false)
INSERT INTO contacts
UPDATE contact_event_role
//else if the role does not exists and the contact does exist
elseif( check4role == false && check4contact == true)
INSERT INTO contact_event_role
//else if the role does not exists and the contact does not exist
elseif( check4role == false && check4contact == false)
INSERT INTO contacts
INSERT INTO contact_event_role
你知道我不确定,但我想我只是说出了正确的逻辑,但我真的很想得到一些反馈,比如这是否应该这样做,或者我的逻辑是否有缺陷。我觉得我错过了什么。
谢谢!