I'm not a database programmer. I really don't plan on becoming one; I dabble. My skills and work lie elsewhere, and these keep me busy enough. Nevertheless, I find myself needing, and begging, for some quick help at times in an area in which I have no time or need to learn fluently. I have officially attempted some trials at building a couple of new tables for this, but to no avail. I do most of my learning in a hands-on setting, and will know how this particular schema works once I've received some direction (which is all I'm asking for, I think) and implemented it.
That said, the following was suggested by @RolandoMySQLDBA four years ago, and I've found it terrific for a project I'm building.
I'm hoping he will see this and that he, or someone, can help me extend this to include a table of "Organizations" along with a table of Organization rel_types, to be associated with any of the persons…
CREATE TABLE person
(
person_id INT NOT NULL AUTO_INCREMENT,
...
PRIMARY KEY (person_id)
);
CREATE TABLE relationship
(
rel_id INT NOT NULL AUTO_INCREMENT,
person_id1 INT NOT NULL,
person_id2 INT NOT NULL,
reltype_id TINYINT,
PRIMARY KEY (rel_id),
UNIQUE KEY outer_affinity (reltype_id,person_id1,person_id2),
UNIQUE KEY inner_affinity (reltype_id,person_id2,person_id1),
KEY has_relationship_to (person1_id,reltype_id),
KEY has_relationship_by (person2_id,reltype_id)
);
CREATE TABLE relation
(
reltype_id TINYINT NOT NULL AUTO_INCREMENT,
rel_name VARCHAR(20),
PRIMARY KEY (reltype_id),
UNIQUE KEY (rel_name)
);
INSERT INTO relation (relation_name) VALUES
('friend'),('follower'),('foe'),
('forgotabout'),('forsaken'),('fixed');
I will be, and am, grateful to anyone who can assist me with this.
Best Answer
If you want to keep things simple, it may be enough to add
organization(organization_id PK, ...)
table andperson_organization_relationship (person_org_relationship_id (PK) , person_id,organization_id ,reltype_id , date_from, date_thru)
. Depends on requirements you may or may not add unique constraint, on , for instance,(person_id, organization_id,reltype_id)
. I would reuse the samerelation
table which is used for describing relationships between persons. In many cases such model will be working just fine.If more general model is needed, concept of
Party
can be added. Party can be a person, or organization, or something else that interacts with the system . For instance if RDBMS supports enums, you can haveThen you either add 2 tables,
PERSON
andORGANIZATION
(which both haveparty_id
as a primary key and foreign key toPARTY
) and store their attributes separately, or store all attributes inPARTY
table.Concept of Party lets you store any type of relationship between person and organization,or 2 organizations , or 2 persons in one table. Moving forward, it'll make other things easier to implement. For example, various contact information such as address(physical or electronic), phone, etc can stored in one place . On the other hands, it has its price. The more universal model you are implementing, the more time you'll need to spend in the beginning.
So I'd recommend to use simplest model that satisfies requirements .
Some notes regarding current physical implementation of
relationship
table:reltype_id
) as a leading column inouter_affinity
andinner_affinity
outer_affinity
,inner_affinity
UNIQUE KEY UQ_relationship_person_1_person_2 (person_id1, person_id2, reltype_id)
andINDEX IDX_relationship_person_2(person_id2)
(depends on typical queries it might be useful toperson_id1
as second column to this index).date_from
(and maybedate_thru
) attribute .