This question already has answers here:
Meaning of "n:m" and "1:n" in database design
(9 answers)
Closed 3 years ago.
I have started a new job recently as .Net Developer, in that for the projects that I am working on requires strong understanding of relationships like
1-N, 1-1 and N-N. I have completely understood 1-1 relationship with real life example like In a Tablets and Billing table if Billing table has TabletId as a foreign key then there is 1-1 relationship between them, but talking about 1-N and N-N relationship regarding same tables how the real life example can be put forward. I have not completely understood.
Tablet
TabletId
Name
description
Maker
ExpiryDate
Billing
BillingId
TabletId F.K
TDate
so from above table structure it is clear that one bill can have one tablet only. but if i want one bill can have multiple tablets then how will i achieve that.
Please give me example of Tablets and Billing table for 1-N and N-N relationships
When talking about relationships in a database, one could be more precise by stating the minimum and maximum links. The relations we usually see in a database are:
1:1 implemented as {1}:{0,1} Example: a user has or doesn't have one address. So, each user has 0 to 1 addresses; an address belongs to one user.
1:n implemented as {1}:{1,n} Example: a user has one or more or no phone numbers. So, each user has 0 to n phone numbers; a phone number belongs to one user.
m:n implemented as {0,m}:{0,n} Example: a product in one or more or no shops; a shop sells one or more or no products.
And here is how we omplement these relations:
The address table has a user ID and it's the table's primary key.
The phone table has a user ID.
There is a bridge table linking product and shop consisting of these two columns: product ID and shop ID.
Of the three relations the first (1:1) is rare. Usually we would rather store the values in a single table. But in the address example we could use it to easily guarantee that a user either has a full address with both street and city and country given or none at all.
The 1:n and m:n relations are both frequent in databases.
Related
So I have 3 main entities. Airports, Customers, and Vendor.
Each of these will have multiple contacts I need to relate to each.
So they way I set it up currently.
I have the following tables..
Airport
Customer
Vendor
I then have one Contacts table and a xref for Airport, Customer, Vendor...
I am questioning that and was thinking a contacts table for each ..
Airport and AirportContacts
Customer and CustomerContacts
Vendor and VendorContacts
Any drawbacks to either of these designs?
To me, the deciding factor is duplication of entities vs "one version of the truth". If a single real-world person can be a contact for more than one of the other entities, then you don't want to store that single person in multiple contact tables, because then you have to maintain any changes to his/her properties in multiple places.
If you put the same "Joe Smith" in both AirportContacts and VendorContacts, then one day when you look and see his city is "Denver" in one table and "Boston" in another table, which one will you consider to be the truth?
But as someone mentioned in comments, if a contact can only be associated with one of the three other entities ("types" as you call them), then putting them in separate tables makes the most sense.
And there's yet a third scenario. Say "Joe Smith" can be a contact for both Airports and Vendors. But say that he has some properties, like his gender and age, which are the same regardless of which "type" he is being considered, but there might be some properties, like phone number, or position/job title, which could depend on the "type". Maybe he uses one phone in his capacity as an Airport Vendor, and a different phone as a Vendor Contact. Moreover, maybe there are some properties that apply to one type of contact that don't apply to the others. In these cases, I would look at some kind of hybrid approach where you keep common properties in a single Contact table, and "Type"-specific properties in their own type-related tables. These type-related tables would be bridge tables that have FKs back to the Contact table and to the main entity table of the "Type" they are related to (Vendor, Customer or Airport).
What I have so far ... Dont mind some of the data types.. just placed quick placeholders..
My database describes companies websites. A company website has several attribute: URL, load time, etc. Each website is targeting one or several countries. I have problems expressing the relation between the website and the countries.
My setup number 1 looks like this:
A Company table
A Country table
A Website table, with fields WebsiteId, CompanyId (refers to Company.id), Countries (array referring to Country.id) and several attributes like URL, LoadTime, etc.
My setup number 2 looks like this:
A Company table
A Country table
A Website table, with fields WebsiteId, CompanyId (refers to Company.id) and several attributes like URL, LoadTime, etc.
A WebsiteCountries table, with fields WebsiteId, CountryId
I have problem with both approaches:
Setup number 1 uses an array of Country.id, which makes it hard to maintain
In setup number 1, CompanyId has a unique constraint in the Website table, but allows duplicate in setup number 2. More generally, CompanyId + Countries could act as a candidate key in setup 1. This disturbs me a bit : am I wrong and is it the best approach?
#Jodrell already gave you the correct answer - "setup number 2" with compound PK is the best solution. I'll just explain it in details.
There are companies, countries and websites. For each of them we create a separate table.
Now it's time for references:
If a company may have only one website, then it's one-to-one relationship. If it may have many websites, then it's one-to-many relationship. In both cases you have a foreign key CompanyId in Website table - it'll only differ in UNIQUE constraint.
You write "Each website is targeting one or several countries." Additionally, for every country there may be one or more websites (for different companies). It means that it's a classical many-to-many relationship. In such case you must add a junction table - WebsiteCountries. Here you'll have a compound primary key that constists of two columns: (WebsiteId, CountryId). This'll prevent from duplicates.
Hope this helps.
I am a novice database admin and I am working on a project that would take a hardware inventory spreadsheet and migrate it to SQL server. Currently, the data exists as one large denormalized view. I am trying to achieve 3NF, but somehow I feel that I haven't designed it well.
Let me give you a brief overview of the situation:
The school district has several buildings, which in turn, have several rooms. The school is experiencing an expansionary period so new rooms are constantly "built", so the "Room Num" cannot be used as a primary key, therefore leading me to use a surrogate key.
Teachers are assigned to rooms, but a room doesn't necessarily have to have a teacher (i.e. labs). Teachers can also change rooms.
When the hardware is purchased, it is given a barcode number (think of it as a line item number on the PO). The actual piece of hardware is identified via its serial number.
Hardware is assigned to a room and given a workstation number and assigned an IP address.
Does the design seem solid or is it flawed? Also, is it ok to create a junction table between a one-to-many relationship (a specific piece of hardware is assigned to a room, a room can have many pieces of hardware).
Please find the link below.
Logical Database Design
Start with a functional decomposition of each entity and define what it "is" versus how it is used. 3NF is about avoiding duplication and keeping the right level of references.
For this problem, model a BUILDING (PK: auto-ID, name, etc).
Then model ROOM (PK: auto-ID, number, etc) with foreign-key to BUILDING primary key.
Then model TEACHER (PK: auto-ID, name, etc) with no foreign-keys.
For the relationship between teacher and room assignment, use a join table:
TEACHER_ROOM
This would have a compound primary-key of:
TEACHER_ID
ROOM_ID
Thus a teacher can be assigned to a room but a room does not need a teacher and in this model a teacher could be assigned to MANY rooms (one-to-many cardinality).
Same thing with hardware - define it by itself with SERIAL_NUMBER, etc.
Then have a ROOM_HARDWARE table that has which hardware is in which room with a unique key on the HARDWARE ID as it can only be in one room at a time but a ROOM can have many pieces of HARDWARE.
The only one that sticks out to me is the Hardware table. Category and Manufacturer should be in separate tables.
Are there any hard and fast rules against creating a junction table out of a table's primary key? Say I have a table with a structure similar to:
In this instance there are a list of items that can be sold together, but should be marked as dangerous. Any one item can have multiple other items with which it is dangerous. All of the items are uniquely identified using their itemId. Is it OK to refer a table to itself in a many-to-many relationship? I've seen other examples on SO, but they weren't SQL specific really.
This is the correct design for your problem, as long as your combinations can only be two-item combos.
In database design a conceptual design that renders a relation in many-to-many form is converted into 2 one-to-many in physical design. Say for example a Student could take one or many courses and a course could have many students, so that's many to many. So, in actual design it would be a Student table, Course table then CourseTaken table that has both the primary key of Student and Course table thus creating 2 one to many relayionship. In your case altough the two tables are one and the same but you have the virtual third table to facilitate the 2 one to many relationship so that to me is still very viable approach.
I am having trouble thinking of how I should build my simple database, its been about 3 years since I have done any sql dev which was back at uni so I have had habit of a brain fade.
I have two tables
business
customer
A customer can belong to many businesses and a business can have many customers.. so far I have created this as a database..
The Business table looks like this
- id
- name
- address
Customer table:
- id
- name
My question is how do I link these two together, how do i keep track of which customers are with which businesses so if a customer joins a business do I need a new table that is like business id and customer id??? I'm just a bit lost, hopefully you guys get what I am asking.
any help would be greatly appreciated.
if you want to link just two tables. the link to this question will be a fit for you: link two database tables
I would go with the party model, with a relationship table. This lets you have individual or organization customers (or busineses) and multiple relationships between them:
PARTY
id
type: {individual, organization}
name
RELATIONSHIP
fromParty references PARTY
relationshipType: {customerOf, organizationalContactOf, leadOf, staffOf, vendorOf}
toParty references PARTY