简体   繁体   English

包含数据库完整性

[英]Containing database integrity

Okay, so this is a straight forward problem but I am having a problem on how I should go about implementing the solution. 好的,这是一个直截了当的问题,但是我在执行解决方案方面遇到了问题。

So here is what the database structure looks like with much reduced for all that is needed. 因此,这是数据库结构的样子,它减少了所有需要的内容。

The Tables Event, Contact, contact_event_role, event_roles. 表事件,联系人,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);

So that's the gist of database. 这就是数据库的要旨。 With a bit of dummy data. 带有一些伪数据。 Pretty sure that all is fine and well there. 可以肯定,一切都很好。

So here is my logic 所以这是我的逻辑

What I am trying to do is insert/update the client and role and when necessary have the client fill multiple roles. 我想做的是插入/更新客户端和角色,并在必要时让客户端担任多个角色。

So my pseudo code looks like this 所以我的伪代码看起来像这样

//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

You know what I'm not sure but I think I just talked the correct logic out but I would be really interested in some feedback as to if this is how it should be done anyways or if my logic is flawed. 您知道我不确定什么,但是我想我只是说出了正确的逻辑,但是我对一些反馈非常感兴趣,这些反馈是无论如何应该这样做还是我的逻辑有缺陷。 I feel like I'm missing something. 我觉得我想念什么。

THANKS! 谢谢!

It looks good to me - but you could make your code a lot easier to read by changing it to look like this: 对我来说看起来不错-但您可以通过将代码更改为如下所示来使代码更易于阅读:

if (check4contact == false)
{
    insert into contacts
}

if (check4role == false)
{
    INSERT INTO contact_event_role
}
else
{
    UPDATE contact_event_role
}

I worked this out by breaking it up into two separate checks. 我通过将其分解为两个单独的检查来解决。 Instead of trying to just handle every possible case, I broke up the code into "what are you trying to do" chunks. 我没有尝试仅处理所有可能的情况,而是将代码分解为“您要做什么”的块。 The first one is just making sure that the contact exists. 第一个只是确保联系人存在。 If they don't, we create them. 如果没有,我们将创建它们。

Then the second part is updating the contact_event_role table. 然后第二部分是更新contact_event_role表。

You could have also done this by moved out common code - eg, you were calling "INSERT INTO contacts" twice, and both times it was called because "check4contact" was false. 您也可以通过移出通用代码来完成此操作-例如,您两次调用“ INSERT INTO联系人”,并且两次都被调用,因为“ check4contact”为假。 So that's one way to clean up your logic. 因此,这是清理逻辑的一种方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM