Should I both have a primary key and a foreign key in the same table?
Eg: one to one
I have a User table and a Address table.
Each User has one address.
Should I only add a foreign key in the Address table that points to the primary key in the User table?
Or should I also create a primary key in the Address table?
Update:
Each User CAN have a address. So not all users will have a address. If a user decides to enter his/hers address. Then the address will end up in the Address table
As I said in my comment, the first question is why you need an address table at all if they are truly 1 to 1?
That aside, I would reverse the relationship. You state that:
Each User has one address.
If that is an accurate description of the model, the User table should reference the address table, and not the other way around. An address could have multiple users. So the user table would have a foreign key column that points to the address table.
Regarding other comments and answers, I agree that it's a good practice for nearly every table to have a primary key. There are certainly exceptions, but it's a good rule of thumb.
I would recogmend you to use a primary key in each table. It could happen, that 2 users live in the same house. Just to be safe...
All tables should have a primary key. The foreign key points to a primary key in another table to establish the relationship.
Related
I am trying to figure out relationships between my tables and the notion of how it all inter-relates is not clear.
Lets say I have a Person table (first name, last name, etc) where the Primary key is social security number.
I also have a Fireman table that has just 2 columns -- date when joined the fire company, and a unique fireman number. The 3rd column would be a link back to the Person table.
Initially, I made SSN in the Fireman table to be a foreign key linking to SSN in the Person table. But doesnt it imply that every time I create a new Fireman, I can re-use the same SSN over and over? The foreign key constraint would not be violated -- so its "all good" -- but its not what I want. Does it make sense? I dont want to allow for different unique Fireman numbers all sharing the same SSN number. So how do I set this up -- whats Primary, whats foreign, whats one to one and whats one to many.
A key can be primary and foreign all at the same time in a one-to-one relationship.
You can scrap your primary key from the Fireman table, and just use SSN as the unique key. Since it's one-to-one, there's no need for a separate identifier.
Alternatively, you could create a unique index on the column SSN in your fireman table. That would prevent duplicate entry.
Note that data validation, relationships and indexes are all separate things. You can use indexes and relationships to validate data, but you can also use different concepts, like constraints.
I want the relationship to be like a single profile only have one address associated to it. While a single address can be from many profile. Where would I place the foreign key.
Address address_id, state, town, phone, email
Profile username, profile_pic, date_added, password
well since you want one to many relation for address table you should add a foreign key ( let's say addr_id ) to profile table.
That way one address row can be used in multiple profiles
From http://www.mysqltutorial.org/mysql-foreign-key/:
"The customers table is called parent table or referenced table, and the orders table is known as child table or referencing table.
A foreign key can be a column or a set of columns. The columns in the child table often refer to the primary key columns in the parent table."
Looks like you want the foreign key on the user table.
Hope this helps!
I have read certain places that it is a good practice to typically use an auto-incrementing Primary key for most MySQL tables, rather than simply relying on a non-increment field that will be enforced to be unique.
My question is specifically about a User table, and a table connected to it by a Foreign Key. Here's the schema:
TABLE Users {
id
name
...
}
TABLE Authors {
user_id (FK)
author_bio
}
Should the Authors table have its own auto-incrementing primary key as well, or should it rely on the user_id foreign key as a primary key?
ALSO
Are there noticeable performance reasons to NOT use the auto-incrementing id as the Primary?
It's not either-or. If you use an auto increment primary key, and you have candidate keys that need to enforce constraints, then your schema should have both.
Both your user and author tables should have individual primary keys. (Every table must have a primary key.) I would not use the foreign key as the primary key. If that truly is the case, I wouldn't have a separate author table; I'd put those columns in the user table.
PS - My naming preference is singular for tables. It should be user and author tables. They happen to contain multiple rows, but a single row means a single entity.
You most definitely want the Authors table to have its own primary key such as authors_id, and then have user_id as a foreign key.
It depends on what you're trying to accomplish. If every author maps to exactly one user (and you're sure this isn't going to change), you can get away with having user_id as a primary key. If not, you'll need an independent primary key for Authors.
(Note that the reverse relation doesn't have to be true: not every user has to map to an author.)
In my db I have three tables (I have more but for case is equal, users can be companies or single people).
Users has a primary key id_user;
Company has a primary key id_company and a foreign key users_id_user;
job_offers has a primary key id_job_offers and two foreign keys: company_id_company and company_users_id_user.
My questions are:
Does a primary key make sense in job_offers? I don't think that there is a reason for it.
job_offers has two foreign keys, one related to company and other to users. Is there a problem with this? Does there exist another way to accomplish the same task?
All tables should have a primary key. It sounds like you are asking whether your primary key should be a surrogate key or a natural key.
You might ask the same question of your other tables as well. For instance, assuming the email column in your users table is required and unique, it could be used as a (natural) primary key.
This question is pretty heavily debated, and both approaches can work (as can a mixed approach). If you want to read up on this subject in general, do a google search for "Natural vs. Surrogate Key".
Does a primary key make sense in
job_offers? I don't think that there
is a reason for it.
Yes . I agree that every table should have their own PK.
Should each and every table have a primary key?
I have more but for case is equal,
users can be companies or single
people
job_offers has two foreign keys, one
related to company and other to
users. Is there a problem with this?
Does there exist another way to
accomplish the same task?
The system have two types of users:
normal user (person) and company user.
The job_offers is a table that save
job offers from a company. If a
company user want to post a job , a
record will be inserted to the
job_offers table . Then once the
normal user get this job offer , the
job_offers.company_user_id_user will
be assigned to this normal user 's
userid.
But from your ER diagram , Company.users_id_user is the PK , which cannot be null , and this PK is used in the job_offers.company_users_id_user as a FK. So job_offers.company_users_id_user also cannot be null .
As a result , it cannot handle the situation that a company user just post a job and before a normal user gets this job offer or no one gets this job offer eventually .In this case, job_offers.company_users_id_user should set to null , which violates the job_offers.company_users_id_user 's not null constraint.
I will accomplish the same task using this design:
Users
=================
id_user (PK)
email
activation
password
Company
=================
id_company (PK)
activities
foundation
user_id (FK to Users)
description
job_offer
=================
id_job_offer (PK)
id_company (FK to Company)
description_offer
tags
user_offer
=================
id (PK)
user_id (FK to Users)
job_offer_id (FK to job_offer)
1) make sense a primary key in
job_offers? I think there is no reason
Yes there is - every table ought to have a primary key. It's called 'normalization.'
Your choice might not be very good. I'd say that the two foreign keys together should be the primary key, not the id column.
2) The job offers have two foreign
keys, one related to company and other
to users, any problem ? exists another
way (best way) to make this?
No, that's how many-to-many relationships are done.
I think you're right. There is no need for a separate id field there. The two foreign keys should, together, make up the table's primary key.
Looks fine to me.
I have an existing InnoDB table which already has foreign keys pointing to different tables.
But when I try to create a foreign key pointing to the Primary index, I get an error (check data type).
The table is User with User_Id as the Primary.
I want a foreign key Manager_ID which is a FK to User_Id.
Both of INT
Both of Length 10
Unsigned...
But I still get a data check error...?
Make sure that Manager_ID is not set to NOT NULL.
You have to allow nulls on that field, as the top-most person in the company will have no manager.
I found a post over on the MySQL boards that might help.