How to read N:M:K relation (databases)? - database

I understand the N:M, 1:N and N:1 relationships.
Let's suppose we have a travel agency and look at the relation "booking a travel". The entities involved in this relation are customers, employees and the destination. Rules are as follows: one customer can book several travels and a destination can be booked by several customers. Apparently, this relationship is N:M:K.
How do you have to read N:M:K? Is it like 1 customer can book M destinations with N different employees? But you also can't book one same travel with more than one employee, so how do I have to rephrase it, if needed, in several sentences?
Thanks in advance

If I understand you correctly:
The "base entities" are customers, employees and destinations.
Now consider a single booking. It is booked by one and only one customer. It has one and only one destination. It also can be booked with one and only one employee.
Thus there will be 4 tables in the database. The bookings table is, using pseudo syntax:
BookingId (PK),
CustomerId (FK of [customers]),
EmployeesId (FK of [employees]),
DestinationId (FK of [destinations])
Now,
SELECT * FROM bookings WHERE CustomerId = xxx
will give you the different bookings to various destinations by the same customer, and each booking is made by only one employee. Similarly to bookings by the same employee or to the same destination as well.

Related

How do I add multiple values in one column (I am using phpmyadmin)?

The case study is that a company name group is contracted with many energy companies worldwide. Now there are only 2 supervisors in the company group who have access to oversee the contracted companies.
Which means that 1 supervisor oversees many companies.
This is the contracted companies table:
Company table
the companies have the access to see which supervisor can see their company
This is the Supervisor Table
Supervisors Table
I dont know how to add multiple values in the companyID column in the supervisors table. This is my first time getting a big university project to be done in phpmyadmin.

How to solve this? Use a bridge table?

I am designing a star schema and come across this problem. I have two dimension table - a Product (Key = Product Id) and a Customer (Key = Customer Id). In another table there are three fields - Business Unit, Product Id and Customer Id.In this table one Product id refers to many Business Units and many Customer Ids. My question is do i need to place the third table as a bridge table between Product Dim and Customer Dim? Or what should be a better way to solve this?
Product Dim
Product ID
Product Name
....
....etc
Customer_Dim
Customer_Id
Customer Name
Customer Type
.....etc
Third Table
Business Unit
Product ID
Customer ID
.....etc
Right now i created the third table as Bridge table and joined with Product and Customer Dim. The third table will have Product_id and Customer_id as FKs and Business Unit as the attribute.
Is this the right approach?
Thanks,
Arun
Yes, this is what's called a many-to-many relation. A product can be connected to many companies, and a company can be connection to many products.

Entity Relationship diagram for a three-way relationship

I want to create an entity relationship diagram for a three way relationship in the following contrived situation:
Each COMPANY has many DEPARTMENTS. An EMPLOYEE can work simultaneously for many DEPARTMENTs. However, an EMPLOYEE can work for only one DEPARTMENT of a COMPANY. In other words, the EMPLOYEE works for as many DEPARTMENTS as COMPANYs.
What kind of associations and relationships will such a diagram have? My purpose is not to create tables in a database but just as an aid to understanding.
Thanks in advance for your help!
COMPANY_EMPLOYEE_DEPARTMENT:
COMPANY (PK)
EMPLOYEE (PK)
DEPARTMENT
Each combination of company and employee, can only be associated with one department. That is, an employee can only work for one department in each company.
This should work as long as a department does not belong to more than one company, and that the COMPANY_EMPLOYEE_DEPARTMENT relation does not link a department of one company with another company.

Basic table structure question

I have Brand and Company. 1 Company can have 1 or more Brands.
As an example, company has company_id, company_name. Similarly Brands has brand_id and brand_name. Now can i add the FK column company_id to brands also and the relationship is complete in 2 tables or do i need a 3rd table like Company_Brands which will have company_id, brand_id and the default PK?
I am not asking for an ideal text book way this should be done but in a high transaction environment where performance is important so less query stain and also where writes will be high along with data will change in tables as this is a user content site so information may not be accurate and thus edited constantly.
Just add the foreign key company_id to the brands table. You have described a 1 to many relationship i.e. 1 company can have many brands, but 1 brand cannot have many companies.
You would only need the junction table if you had a many to many relationship.

Help with many-to-many relation

I have a problem with a many-to-many relation in my tables, which is between an employee and instructor who work in a training centre. I cannot find the link between them, and I don't know how to get it. The employee fields are:
employee no.
employee name
company name
department job title
business area
mobile number
ext
ranking
The Instructors fields are
instructor name
institute
mobile number
email address
fees
in a many-to-many relationship the relationships will be in a 3rd table, something like
table EmployeeInstructor
EmployeeID
InstructorID
to find all the employees for a specific instructor, you'd use a join against all three tables.
Or more likely there will be classes involved --
Employee takes Class
Instructor teaches Class
so you'll have and EmployeeClass table,
an InstructorClass table,
and join through them. And Class needs to be unique, or else you'll need
Class is taught in Quarter on ClassSchedule
and end up joining EmplyeeClassSchedule to InstructorClassSchedule.
This ends up being one of your more interesting relational designs pretty quickly. If you google for "Terry Halpin" and "Object Role Modeling", this is used as an illustrative situation in the tutorial.
First of all, you will need a unique key in both tables. The employee number may work for the employee table, but you will need another for the instructor table. Personally, I tend to use auto incrementing identity fields called ID in my tables. This is the primary key.
Second, create a new table, InstructorEmployee. This table has two columns, InstructorID and EmployeeID. Both fields should be indexed. Now you can create an association between any Employee and any Instructor by creating a record which contains the two IDs.

Resources