Lets say we have three tables: Cars, People, Companies.
We want the owner of the car to be a people or a company. The structure of the Cars table is: Car_id, Car_model, Owner_id
What is the best way to do that?
If you can't change the current structure of the cars table, then you could add new table called owners with the following columns:
number id -- unique key
number owner_id -- this is the actual owner id
char owner_type -- this is a value indicating whether the owner is a person or a company
You will then need to cross reference cars with owners and look at the value of owner_type to determine which table to get your owner data from.
EDIT
Forgot to mention (rather important):
In the cars table, populate the owner_id with the owners.id column.
What about having another field in Cars table as "Owner_type_id" which will determine if owner is a person or a company or any other thing. Of course, you should keep another table "Owner_Types" for this scenario to work.
As Igby mentioned, your existing structure is far from ideal, but if you're stuck with it, one option is to have a new Owner table that has the owner_id from the Car table as well as a person_id and a company_id, only one of which would need to be populated.
Related
I have two tables. One is Employee and another is Role. The Employee table contains EmployeeId and EmployeeName. Similarly, the Role table will contain RoleId and RoleName column. An employee will belong to one of several different roles.
I am new to database design and having problem figuring out if it's better to have a RoleId column in the Employee table or to create another table called EmployeeRoleMapping which will contain rows indicating this employee is mapped to this role. What are the pros and cons of both approach?
the most important thing is that you must sure of the relationship between Employee and Role in your application.
In my point of view, an Employee may have one or many roles and vise versa one role may belong to one or many Employees. So that, we have to create a new table called EmployeeRoles and this table has employeed_id and Role_id.
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.
I have a question about entity creations that is specific to a student information system that i am building. i have created a Person table (id..) and i am trying to find out how i can handle my student, parent references. is it a good idea to create two separate tables (Student, Parent) that reference the Person table by FK relationship? All of the details about a Person (firstname, last name, SSN ...) have been set in the Person table but there are differences between a parent and student, how do you handle this in a database?
Since there are fundamental differences between parents and students, two tables would be the preferred solution. This way you can easily create a relation connecting the students and parents.
The other option is to use null values in the columns that do not apply for a given record. However, it will be more difficult to ensure that the relation always connects a student and parent.
I agree with Casey Robinson in that its a clean solution.
But if you already have a populated Person table which is being used by other code... in short you can't change the Person table then here is what I would suggest:
Create a table (studentParent) which will have two columns (student_id and parent_id) both foreign keys. The studentParent.student_id = Person.id of the student and studentParent.parent_id = Person.id of the parent.
This way you won't have to change the Person table. And will be able to create the parent, student relationship.
Without knowing more details it seems that two tables Person and Student should be sufficient. Have two columns in Student table like Student_id and Parent_id each of which is a FK to person_id in Person table. This is assuming you will need to know for only student which is a parent and not for every person. Also assuming that both student and parent are person.
In my database I got a list of companies with columns CompanyId and Name.
Further on there is users and those users can add products.
Tables: user and user_products
There is no product table defining products, they are unique per user therefore "user_product"
When a user adds a product he types a name of a company. If that company name exists in the company table I want to make a connection to the company table instead of saving only the name on the user_product. So far so good.. I just store CompanyId in the user_product table.
The problem is when the user enters a name that doesn't exists in the company table. Instead of saving the name as varchar, I want to create a new record in a table called user_company.
The table got columns: UserCompanyId (PK), UserId, Name. If the combination Name and UserId already exists i will of course not create a new row, just reference to this id.
What should I do to maintain a good database design here.. Should i add this record and also a new column in user_product called UserCompanyId. So that either CompanyId or UserCompanyId is always set when adding a new row. It feels like this could be done in a better way. Anyone got any ideas?
I could of course only have one table "company" and have a column UserId which is null when it's a global company added by the system, or the UserId is actually set when a user has added a company name that didn't existed globally. This doesn't feel good either...
Actually, I think you nailed it in your last paragraph. A company is either defined by a user or isn't, so the userId makes sense as a nullable column. This would also allow you to have a unique key on the company name, which allows you to use the database to enforce the fact that a company name can't be duplicated.
Your company table exists to define companies--which user (or whether a user) created the company is just information ABOUT a company.
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.