I have a contacts table and a contact_category table. I am trying to reflect in table that one contact can belong to several categories.
Is there any recommended design pattern for implementing this? What comes to my mind is just creating a string in an additional field for every contact and concat the categories this contact belongs to.
For ex.:
"cat1,cat3" would mean that a contact belongs to cat1 and cat3
But, isn't there any proper way of designing this?
Generally speaking, a comma-delimited text field with multiple values is a bad idea in database design, in my rarely-humble opinion.
I'd recommend something like this (I code in SQL Server, so that's what my syntax will look like):
Contact
ID -- primary key
-- other contact fields
Category
ID -- primary key
-- other category fields
Contact_Category
Contact_ID -- foreign key to Contact
Category_ID -- foreign key to Category
The above allows you to associate a contact to multiple categories and a category to multiple contacts. Let me know if you have any questions.
Related
The main idea is to store multiple ids from areas into one column. Example
Area A id=1
Area B id=2
I want if it is possible to save into one column which area my customer can service.
Example if my customer can service both of them to store into one column, I imagine something like:
ColumnArea
1,2 //....or whatever area can service
Then I want using an SQL query to retrieve this customer if contains this id.
Select * from customers where ColumnArea=1
Is there any technique or idea making that?
You really should not do that.
Storing multiple data points in a single column is bad design.
For a detailed explanation, read Is storing a delimited list in a database column really that bad?, where you will see a lot of reasons why the answer to this question is Absolutely yes!
What you want to do in this situations is create a new table, with a relationship to the existing table. In this case, you will probably need a many-to-many relationship, since clearly one customer can service more than one area, and I'm assuming one area can be serviced from more than one customer.
A many-to-many relationship is generating by connection two tables containing the data with another table containing the connections between the data (A.K.A bridge table). All relationships directly between tables are either one-to-one or one-to-many, and the fact that there is a bridge table allows the relationship between both data tables to be a many-to-many relationship.
So the database structure you want is something like this:
Customers Table
CustomerId (Primary key)
FirstName
LastName
... Other customer related data here
Areas Table
AreaId (Primary key)
AreaName
... Other area related data here
CustomerToArea table
CustomerId
AreaId
(Note: The combination of both columns is the primary key here)
Then you can select customers for area 1 like this:
SELECT C.*
FROM Customers AS C
WHERE EXISTS
(
SELECT 1
FROM CustomerArea As CA
WHERE CA.CustomerId = C.CustomerId
AND AreaId = 1
)
In a new database model that I am designing, I have a company table that has a contacts table related to it using a foreign key companyID field.
I have a branch table with PK branchId and FK companyId.
The problem is: Sometimes contacts can move from the company or branch they are in to a different one. And I need to know all the former companies and branches the contact was in.
What is the best way to do this?
I can't figure out anything that makes sense. If I insert another record into contacts with the different details, There will be a duplicate record, and if I just change the details, how will I know what the former details were?
I would suggest a table that joins your contacts table to your company table. This new table would have a contactsid, a companyid, a date (or date range, depending on your needs) and any other info that you would need to describe the relationship between the contact and the company. The contacts table would then no longer need an FK companyID.
Hello I have a Database like three tables:
Customer: Customer ID, Customer Name, Customer Surname
Product: Product ID, Product Name, Product Price
Now I have another table
CustomerProduct: Customer ID, Product ID
Here are all bought stuff standing, while taking the customer id and product id.
I just ask what kind of normalization of database structure this is?
Can you give me an explanation ?
Such a table, which handles many-to-many relationships by storing the two related primary keys (as foreign keys) is called by various names, but junction table seems to be the standard (I personally use association table).
Most relational databases do not handle many-to-many relationships natively - you need an extra table like this.
Consider the following scenario:
Tables:
Employee (EmpId(PK), Name)
TeamMembers(TeamId(PK), EmpId(PK))
Project(ProjId(PK), TeamId)
I really want to avoid using composite PK, but the only way I see out of the problem is creating a Team table with only 1 column TeamId(PK) (i do not want to store any info associated with the team other than its members) (EDIT: if I create a team table, i'll add TeamMeberId to TeamMembers table and make it a PK)
Another problem with current setup is that I can't set a relationship for TeamId between Project and TeamMebers tables
Should I just create a 1 column Team table? What's the best approach in this case?
EDIT
just to clear things up, the only thing I want to know about that team is its existance, no additional info of any kind
EDIT2
Tables New Design (anything wrong with it?):
Employee (EmpId(PK), Name)
Team(TeamId(PK))
TeamMembers(TeamMemberId(PK), TeamId(FK), EmpId(FK))
Project(ProjId(PK), TeamId(FK))
If the only thing interesting about a team is the fact that it exists, then there is nothing wrong with a Team table with just one column: TeamId. It ensures referential integrity from the TeamMembers and Project tables.
But I do not understand your objection against a composite PK. The columns TeamId and EmpId in the TeamMembers table are alreay a composite primary key.
There is nothing wrong with this scenario. I'd do it.
On the other hand, you could hold other information in your Team table like a team Name or something.
There is nothing wrong with a 1 column table. However you might want to consider what other attributes your Team table could have. For instance, a team name?
For the relationship between project and employees, you merely have to join through the TeamMembers table.
does EmpId really need to be a primary key in your TeamMembers table? you could just say that each team has many employees, and the relationships work out.
Since it looks like there is a 1-to-1 relationship (correct me if I'm wrong) between Project and TeamMembers and you don't want to store additional info about the team, wouldn't it be easier to get rid of the TeamMembers table and go with a many-to-many linking table between Project and Employee
Employee (EmpId(PK), Name)
EmployeeProjects(EmpId(PK), ProjId(PK))
Project(ProjId(PK), <other project info>)
but to answer your original question. There is nothing particularly wrong with having a single column table.
This is how I would structure the tables
Employee (EmployeeId(PK), Name)
Team (TeamID(PK))
TeamMembers(TeamMembersID(PK), TeamId, EmployeeId)
Project(ProjectID(PK), TeamId)
I like to have a PK being the table name, suffixed with ID.
This convention does have the side affect of sometimes creating seemingly redundant primary keys (as TeamMembersID) but it solves your composite key problem.
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.