Database design: OR relation - database

I have a database with a "users" table containing data about my users. Each user is to be linked to a company or a college. I wish to have two separate tables "college" and "company" each with a field "ID". So how do I link each record in the users table to either a company or a college?
The basic thing is that I wish to establish an "OR" relationship in the database.

You can use subtype/super-type relationship. Keep all common fields in the organization table. College and company tables contain only fields specific to those entities.

You could use an 'institution' or 'organisation' lookup table, with a structure something like
InstitutionId[PK], InstitutionType, LookupKey
where LookupKey is the PK to either Company or College.
Or,
InstitutionId[PK], CompanyId[FK], CollegeId[FK]
In both cases you link from user to institution, then onto Company and/or College.
I personally prefer the second option, because it allows you to easily validate the FK relationship and also allows (if applicable) for a user to be a member of a company and/or a college.

I would create relationtables. UserCollege and UserCompany. This way you are even able to have users that are linked to both if needed in the future. If not you simply just create a relationrecord on one of the both

Related

filemaker database relationships

I'm very new to FileMaker currently working on a Mac. I've been assigned a new simple system to work towards completing and I have bumped into some issues with database relationships. I've got experience with PHP/MySQL databases connections etc. but FileMaker seems to require a somewhat different mindset and approach.
I'll try to explain this as simply as I can.
Here's the table relationships in my database
What I'm trying to do is a list of "to-do" notes, an interactive menu where the user can add things that needs to be done. I've done this with a portal on a layout based on the table "site". The portal is based on the table "todo_notes", which is connected to site through the "site_id".
Here's what it looks like in browse mode
What I'm having problems with is adding a relationship between the todo_notes and contacts. The contacts are two separate tables called "county_contacts" and "property_owner_contacts". What I want to accomplish is the possibility for the user to, from a dropdown-list, add a single contact from these two tables. Preferably I'd like to sort of merge these two tables into the same dropdown-list.
Let me know if you need any other information or a better explanation of my issue. Any help is very welcome!
If you have a single contacts table with foreign keys for both county and property owner tables, that would let you have a single list for all contacts. From there you could also build a value list based on a relationship, for example to filter only contacts that belong to either county or property owners.
If you then need to further normalize the tables, fields that pertain to either relationship exclusively could be moved to another table from there, as a one to one relationship, if that is a concern.
The Short Answer
You need to create a Contacts table. Filemaker has no way of dynamically generating value lists. Instead, you can base a value list on any field, therefore, the only way of generating a list of the contact names would be if they were all in the same table.
The Long Answer
Because Filemaker only allows us to use ONE field for a value list, we must create a new table for the contact. I would recommend that you replace the two contact tables with a single contact table,(seeing as the fields look the same between the two tables) and then add a toggle on the contact for Owner or County. However, you could also create a single contact table for all of the fields that overlap that has foreign keys to the owner and county tables.
You would then use the fullname field from the contact and be good to go.
That is, assuming that you did not want to filter the contacts at all or only show contacts associated with this site.
To start with, I highly recommend using the Anchor-buoy method for organizing the relationship graph. Here's an explanation of the anchor-buoy method: http://sixfriedrice.com/wp/six-fried-rice-methodology-part-2-anchor-buoy-and-data-structures/ . It's just a convention, but will help you with the idea of context in FileMaker. It's widely accepted among the FileMaker community as the "right" way to organize a relationship graph. I will continue my explanation using this method.
Each Table Occurrence (the boxes in the graphs, or TO) represents a unique context from which you can view and edit information. In the anchor buoy method, each Table only has one "anchor" TO. I would recommend only using anchor TO's for the context of your layouts. Then, your portal, and any other corresponding information, will be on your buoy TO's. Here is what your new portal relationship would look like. You would select fields from your buoy TO's to use in the portal.
The easiest way to filter your value list by only contacts associated with this site would be to create a foreign key from the contact table to the site, and then add a TO to the graph, for the contact table. You would then click "Include only related values starting from" radio button, and specify your new TO.

How to Normalize the relational schema?

Consider the following Relational Schema:
I am trying to fully Normalize (In Third Normal Form) and determine the functional dependencies. However, with endless research, I cannot get around on how to:
Fully Normalize the Relational Schema
Determine the Functional Dependencies
How would I go about this?
An employee can be a customer, and may become a manager one day. Use the Party Model. "Employee" or "Customer" should be a role played by a Party. A Party hasMany Roles
People can have no address, one address, or multiple addresses. People can share the same address. Use an Address table, and a PersonAddress junction table. Same with phone numbers.
You'll probably want to have individual customers and organization customers (companies or shared accounts). Use the Party Model.
Why does Branch not use a BranchId when all the other tables use id columns?
Are you sure a Customer is assigned to an Employee? What if the employee is on vacation?
Why "city" and "town" in Branch?

Entity Framework 6 - Many-to-Many Table with Additional Information

Seems I am not allowed to post images, so let me describe the image. It is a SQL table diagram showing the relationships between 4 tables. The Tables are:
People
Id
FirstName
LastName
PhoneNumbers
Id
Number
PhoneNumberTypes
Id
Name
Description
PeoplePhoneNumbers
PersonId
PhoneNumberTypeId
PhoneNumberId
The two main tables are People and PhoneNumbers. There is also a PhoneNumberTypes that describes the type of PhoneNumber (Home, Work, etc).
The PeoplePhoneNumbers table serves as a Many-To-Many relationship table between People and PhoneNumbers. However it also connects to PhoneNumberTypes to describe the relationship.
I have been trying to figure out how to handle this Entity Framework because EF does not allow you to add additional information to the Association(Many-To-Many) Table.
Besides the PhoneNumberType info, I also find that their are additional data pieces I need to record in the Association Table like "Start Date", "End Date", etc.
The only solution I have come up with so far is to create an entity in EF that combines the fields in PhoneNumberTypes, PhoneNumbers and PeoplePhoneNumbers into a single entity. Then use SQL stored procedures for CRUD operations against it.
I would prefer a more EF centric solution. Does anyone know of one?
PhoneNumberTypes should not be linked to PeoplePhoneNumbers. Let PeoplePhoneNumbers be what it is and only a junction table. PhoneNumberTypes should be linked to PhoneNumbers. EF should be able to create this setup by convention. Additional information describing phone number should also be linked off of PhoneNumbers. This design adheres to more Domain Driven Design (DDD) principles and also generates a better database design as well.

Database Tables - To decouple or not?

Is it better to create tables that store a lot of data that are related to an entity (User for example) or many tables to store said data?
For example:
User Table
Name
Email
Subscription Id
Email Notifications
Permissions
Or
User Table
Name
Email
Subscription Table
User ID
Subscription ID
Notification Table
User ID
Receives?
... etc
Please consider code in this as well, or I would have posted to ServerVault.
From a relational design standpoint what is important is the normal form you're aiming for. In general, if the "column" would require multiple values (subscription_id1, subscription_id2, etc) then it is a repeating group, and that would indicate to you that it needs to be moved to a related table. You've provided very general table and column notes, but taking a cue from the fact that you named "Email Notifications" and "Permissions" with plurals, I'm going to assume that those require related tables.

Preferred way to map code with user created database entries

I am trying to work out the best database model for the current setup:
An administrator can create "customer products". This means services/products which customer can attach/subscribe to. The simple cases where the product simply costs a price, or the product subscription should send an e-mail is easy to model in the database.
But how about very specific backend code for a customer product? For example, one product might have very specific code implemented for checking a customer status on a different database. How can I map this relationship in the database so I can turn on/turn off some code based on the product settings.
My intuitive way of handling it would be to have a string column on the CustomerProducts table where a pre-defined set of strings could be set, e.g. "MyCustomCodeHandler", and then the code would check for the existence of this string in order to execute it. But for me it doesn't really feel like a real relationship between the database and code.
Data is data, whereas code is code. I would not recommend storing code in the database.
If you need to allow customers to create product types (in the object-oriented sense of "types") with associated code, I'd choose to deploy that code in the same way you deploy other code.
The custom code may also reference custom data stored in the database. I'd choose to create a dependent table per product subtype, and put the type-specific columns in there. The relationship between this subtype table and the generic product table is one-to-one. That is, the primary key in the subtype table is also a foreign key to the generic product table.

Resources