Foreign Keys in Databases - mysql

This table should contain foreign keys to allow sensible links to be made with the other two tables, together with the dates of each exam.
So what am i being asked to do exactly?
All the other tables have been populated.

This looks like a classic Many To Many relational problem.
One student can study more than one subject
A subject can be studied by more than student
This cannot be readily modelled relationally using just the two tables you have given.
The traditional way to model this is to introduce a third table which contains the details of which students are studying which subjects.
The table would generally contain the id's of the tables that participate in the many to many relationship, so in your case it would have student_id and subjects_id. These would be defined with foreign key constraints back to the student and subjects tables.
This page may go a long way to helping you understand this.
By the way - the primary key constraint on the subjects table looks a bit suspect to me ...

First of all, your PRIMARY KEY of the subject table should be subjects_id, because you may have 2 final exams the same day.

CREATE TABLE IF NOT EXISTS entries(
entry_id INT UNSIGNED NOT NULL AUTO_INCREMENT,( PK)
student_id INT UNSIGNED NOT NULL,(FK From students table)
subject_id INT UNSIGNED NOT NULL,(FK From subjects table )
);
You need to make (student_id,subject_id) combination unique since it shouldn't be duplicated.This is rough draft.Assume that this table is a bridge which connects students and subjects.

Related

What database technique should I use?

I am new to databases. This is for a class. I am using MySQL. I will be accessing the database with PHP. I have two tables already. TableA is for products. TableB is for US States. I want to have data about sales of each product in each state. I have considered this is a many to many relationship.
Technique idea #1:
I have considered making a third table, TableC, that has a column for the state names and a column for each product. My issue with this is that I don't know how create a relationship between the product rows in TableA and the product columns in TableC. If I add a product to TableA I want it to automatically add the column in TableC.
Technique idea #2:
Add the product columns to TableB. Same issue as above. Also, seems like a worse design.
Are one of these techniques the right way to do this or is there another technique?
The art and science of making a good schema revolves around finding the best place to put something, or in many cases, the least inconvenient.
Putting sales data in a table that's intended for geographic information is almost always a mistake. Keep your tables focused on one entity, and where information there exists only when related to other tables, make a "join table" that connects the two and put that data there.
For example:
CREATE TABLE products (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255)
);
CREATE TABLE states (
id INT PRIMARY KEY AUTO_INCREMENT,
code CHAR(2),
name VARCHAR(255)
);
CREATE TABLE state_sales(
id INT PRIMARY KEY AUTO_INCREMENT,
state_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT
);
You can extend that later to include things like the date/month/quarter the sales happened in and so forth.

MySQL Workbench: What is the foreign key relationship between child and parent tables?

I have the following tables:
CREATE TABLE Salesperson
(
id INT,
name VARCHAR(30),
age INT,
salary INT,
PRIMARY KEY (id)
);
CREATE TABLE Orders
(
Number INT,
ORDER_DATE DATE,
cust_id INT,
salesperson_id INT,
Amount INT,
PRIMARY KEY (Number),
FOREIGN KEY (salesperson_id) REFERENCES Salesperson (id)
);
I would describe the foreign key relationship as a many-to-one, non-mandatory-to-mandatory relationship between the referencing (child) table Orders to the referenced (parent) table Salesperson.
However, MySQL Workbench describes the relationship as a one-to-many, mandatory-to-non-mandatory relationship between the referencing table (child) table Orders to the referenced table (parent table) Salesperson.
Is it me, or MySQL Workbench that has it backward? An explanation would be appreciated.
Update
I found this definition of "Mandatory" under the MySQL Workbench docs:
The Mandatory checkboxes are used to select whether the referencing table and the referenced table are mandatory. By default, both of these constraints are true (indicated by the checkboxes being checked).
The relationship is in both directions. Or, you can think of it as two halves of the same relationship.
A foreign key relationship like this typically called 'one-to-many', rather than 'many-to-one'. MySQL workbench is referring to it using the normative pattern.
FOLLOWUP
Q: why is Orders.salesperson_id mandatory, while Salesperson.id non-mandatory? Salesperson.id is a primary key, and Orders.salesperson_id is not a primary key, so wouldn't the check boxes be reversed?
A: I'm not familiar with the checkboxes in MySQL workbench.
As Mike Lischke points out in his comment, what we're concerned with at the Entity-Relationship modeling level is the "cardinality" of the relationship. Basically, we're asking the questions, Do we require that an Order be related to a Salesperson? That is, can we have an Order that is not related to a Salesperson?
Can we have Salesperson that is not related to an Order?
And obviously we've already answered the questions, can a Salesperson be related to more than one Order? Can an Order be related to more than one Salesperson? That's how we determined that this is a one-to-many relationship. Now we're just further refining the cardinality.
I think the "Mandatory" checkbox in the UI is meant to answer the question: Can we create an instance of Salesperson that is not related to an Order, or do we require a Salesperson to be related to at least one Order?
Again, the questions make sense if we view this as two relationships we're asking questions about. We're asking questions about the relationship from Salesperson to Order, and also about the relationship from Order to Salesperson.
Based on the answers to our questions, we mark Order.salesperson_id as "mandatory" to indicate that we are going to require that an Order be related to a Salesperson. We mark the other direction of the relationship as "non-mandatory" to indicate that we are not going to require that a Salesperson be related to an Order.

How to make a composite key to be unique?

I am making a database of students in one school.Here is what I have so far:
If you don't like reading jump to the "In short" part
The problem is that I'm not happy with this design. I want the combination of grade, subgrade and id_class to be unique and to serve as a primary key for the students table.
I can remove the student_id and make a composite key from the 3 but I don't want that either. Maybe I should make another table lets say combination_id where grade, subgrade and id_class are foreign keys and there is one extra column comb_id that serves as ID for the table. And all the columns will be Primary Keys. But the problem is that those 3 columns can still repeat because of that extra column (comb_id). For example I can have the same grade, subgrade and class_id but different comb_id which will make the row valid because of the composite key of the 4 columns of the table (combination_id).
In short I want students_id to remain the only primary key of the table but to be a foreign key to another table which is somehow unique combination of grades, subgrade and class_id.
If I was not clear enough ask in the comments below and thank you in advance.
PS I'm sorry for the indescriptive title but I'm bad at naming
EDIT 1:
To be more clear:
grade can be 1 to 12
subgrade can be a to j
id_class can be 1 to 30 and it is your number in class
So a student can be from 7b class and his number in class - 5
Don't mix the concepts of unique keys and primary keys. You can very well add a unique key spanning the three columns grades, subgrade and class_id. That way, no two rows could have the same values for these three columns. As you write that you don't want to have these three as a composite primary key, I'm not sure whether a composite unique supplemental key would be any better. If not, you'll have to clarify when composite keys are acceptable.
To create a unique key, you can use the following SQL statement:
ALTER TABLE students ADD UNIQUE gsc (grades, subgrade, class_id);
The word gsc there is just a name I made up from the initials of the key columns; use whatever name you want, as it hardly matters unless you want to identify the key in some EXPLAIN output or similar.
I'm not totally clear on why you want what you have described, but I would look at the model in the following way...
You have Students
- They are distinct entities, not a composite of other entities
- They have their own properties; name, date of birth, etc
You have classes
- These are groups of students
- Each accademic year the same "class" has different students in it
- They also have their own properties; grade, sub-grade, etc
You have an extra property in your model that I would not normally use
- If a class has 20 students, each of them is identified with a secondary id from 1 to 20
This would give me the following Dimension tables
Student Class Grade SubGrade
----------------------- ------------------------ ----------------- -----------------
id INT PK id INT PK id INT PK id INT PK
first_name VARCHAR(45) name VARCHAR(45) name VARCHAR(45) name VARCHAR(45)
last_name VARCHAR(45) grade_id INT FK desc VARCHAR(45) desc VARCHAR(45)
etc, etc subgrade_id INT FK etc, etc etc, etc
The Class table would have a unique constraint on (grade_id, subgrade_id) so that only one class could ever be 7b.
Then you need to relate the students to their classes using a fact table...
Class_Membership
-----------------------
id INT PK
student_id INT FK
class_id INT FK
academic_year INT
If a student should only ever be in one class in any academic year, you would put a unique constraint on (student_id, academic_year).
Alternatively, you could have the academic year in the Class table. This would mean that you would have the same class repeated for every year, but that in some years class 7g may not exist (as there are less students that year, for example).
Equally, you could have students who move from 7b to 7c mid-year. In which case the Class_Membership table could have a start_date field and possibly an end_date field.
None of that, however, directly creates the id_class field (1-20 for a class with 20 students). Personally I would not have such a field, the id field from the Class_Membership table can serve most of the same functionality, and probably additional functionality. Where it is necessary, however, you could simply add it to the Class_Membership table...
Class_Membership
-----------------------
id INT PK
student_id INT FK
class_id INT FK
academic_year INT
class_member_id INT
Then you could also have a unique constraint on (academic_year, class_id, class_member_id).
There is quite a lot of flexibility here, depending on your exact real-world-model and your particular needs. But hopefully this example is a good start for you; Dimension tables listing Entities, and a Fact table (or tables) relating these entities together and/or further describing the Entities.
alter table <TableName> add constraint <ConstraintName> unique(<col1>, <col2>)
Modified the answer due to syntax mistakes. Mentioned above is correct.

Featured value in one to many relation, which table should hold that?

say that i have a one to many relations where there are two tables, a Person table and a Belonging table. Now, each Person has ONLY ONE favorite belonging and a specific belonging cannot belong to another person as well.
My question is, where would that information be better kept ? In the Person table as a favorite_belonging_id or in the Belonging table as an is_favorite entry ? To my eyes, the first choice seems to be the better version, but I would like to hear what sql knowledgeable people have to say about it.
EDIT : A Person has many belongings but only ONE favorite belonging and each belonging can only belong to one person. It's a one to many association.
I'd be tempted to go with your first suggestion (a favourite_belonging_id column in the Person table), as one can then create a foreign key reference from (person_id, favourite_belonging_id) to (owner_id, belonging_id) in the Belonging table.
If one were to go the other route of creating a is_favourite flag in the Belonging table, there is no obvious way of ensuring the 1:1 nature of person-favourite belonging relationships (a composite UNIQUE index over (owner_id, is_favourite) would fail when a person has multiple belongings that are not their favourite).
That said, it doesn't feel like this information really belongs in the Person table, as it isn't really a property of the person but rather it's a property of the Belonging. If you feel strongly about it, you could create a Favourites table that has a UNIQUE (or PRIMARY) index over person_id.
to me it does NOT belong in the person table since it has nothing to do with the base person.
if you have only the belonging table - which i also assume has a person_id in it, then this is where you are expressing the relationship between the belonging and the person, and it is where the qualifier should also go.
another option is to have a third table in the middle linking the two - in this case, the favorite flag goes there.
edit:
my preference in design would be the third table option - here you can put a begin date and end date as well as the favorite flag - this would allow you to theoretically trade a belonging to another person at some point in time and still know what happened.
I see that pretty much all the different options have already been laid out in different answers, but instead of commenting on all to give you my impression on what I think you should do, I'll just create an answer myself.
Just to be clear on how I understand how the system works: All users can have multiple belongings, but any belonging can only be help by one person.
In this case, it makes the most sense to have a user_id in the belongings table that can tie a belonging to a person. Once a user_id is set, nobody else can claim it anymore.
Now, as to the 'favorite' part, there are several things you can do. What truly is the best way to do it strongly depends on the queries you plan on running on it. Some consider adding a JOIN table, but honestly this is a lot of additional data that is rather pointless; there is likely going to be the exact amount of rows in it as the user table and by putting it in a separate table, there is a lot you can't do (for example, see how many people DON'T have a favorite). Likewise, a JOIN table would make no sense for the user_belonging relationship, as there is a 1:1 relationship between the belonging and the amount of people who can have it.
So I believe there are two viable options: either add a field (/switch) in the belongings table to indicate of a user's belonging is his/ her favorite, or add a field to the user table to indicate which belonging is the user's favorite. I would personally think that the latter holds the most merit, but depending on the queries you run, it might make more sense to to the former. Overall, the biggest difference is whether you want to process things pre-insert or post-select; e.g. in the latter situation, you will have to run an independent query to figure out if the user already has a favorite (in the former case this won't be necessary as you would put a unique index on the field in the user table), whereas in a post-select situation you will have to do cross reference which of the selected belongings from the belonging table is the user's favorite.
Please let me know if I explained myself clearly, or if you have any further questions.
The following may not be the best options because it offers a somewhat unconventional method of flagging the favourite belonging. The advantage, though, is that this way you'll have just two tables with no circular references and every person will be guaranteed to have no more than one favourite belonging.
So, it's two tables, people (or persons) and belongings. The people table has this structure:
person_id INT AUTO_INCREMENT,
other columns as necessary,
PRIMARY KEY (person_id)
The belongings table is created like this:
belonging_id INT AUTO_INCREMENT,
person_id INT NOT NULL,
is_favourite enum ('1'),
other columns as necessary,
PRIMARY KEY (belonging_id),
FOREIGN KEY (person_id) REFERENCING people (person_id),
UNIQUE (person_id, is_favourite)
The key element is declaring is_favourite as a nullable enum with a single possible value. This way, when you declare a unique constraint on the pair of (person_id, is_favourite), you are allowed to have as many rows with the same person_id and empty (null) is_favourite as possible, because unique constraints ignore rows where at least one member is null. And you won't be able to create more than one person_id with is_favourite = '1', because that would violate the unique constraint.
Neither. My suggestion is to add another table person_favourite_belonging, like this:
CREATE TABLE person
( person_id INTEGER NOT NULL
--- various other columns about Persons
, PRIMARY KEY (person_id)
) ;
CREATE TABLE belonging
( belonging_id INTEGER NOT NULL
, person_id INTEGER NOT NULL
--- various other columns about Belongings
, PRIMARY KEY (belonging_id)
, UNIQUE KEY (person_id, belonging_id) --- this Unique constraint is needed
, FOREIGN KEY (person_id)
REFERENCES person (person_id)
) ;
CREATE TABLE person_favourite_belonging
( person_id INTEGER NOT NULL
, belonging_id INTEGER NOT NULL
, PRIMARY KEY (person_id)
, FOREIGN KEY (person_id, belonging_id) --- for this Foreign Key constraint
REFERENCES belonging (person_id, belonging_id)
) ;
This is just my preferred way of doing this. There are alternatives and all have their pros and cons. The pros with this approach are:
No circular path in the Foreign Key constraints (and therefore):
No chicken and egg problems when inserting, deleting or updating Persons, Belongings or Favourite Belongings.
All foreign key columns can be defined as NOT NULL.
The integrity can be enforced at the database level.
If your requirements change and you want to have 2 (or more) favourites per person, you only change appropriately the constraints at the Favourite table.
Check also my answer in this question (with an almost identical problem): In SQL, is it OK for two tables to refer to each other?
favourite_thing is a FK to the belonging table (if that table exists, otherwise it could be a domain) , but in an additional constraint, you can force belonging_id in the persons table to be unique.
UPDATE:
DROP table belonging;
CREATE table belonging
( id INTEGER PRIMARY KEY
, description varchar
);
DROP table person;
CREATE table person
( id INTEGER PRIMARY KEY
, description varchar
, favourite_thing INTEGER REFERENCES belonging (id)
);
-- Now add the unique constraint
-- NOTE: favourite_thing can still be NULL
ALTER TABLE person
ADD CONSTRAINT must_be_unique UNIQUE (favourite_thing)
;
UPDATE 2: if every belonging belongs to exactly one person, you could add an owner field to belongings:
CREATE table belonging
( id INTEGER PRIMARY KEY
, owner_id INTEGER NOT NULL REFERENCES person(id)
, description varchar
);
DROP table person CASCADE;
CREATE table person
( id INTEGER PRIMARY KEY
, description varchar
, favourite_thing INTEGER REFERENCES belonging (id)
);
ALTER TABLE person
ADD CONSTRAINT must_be_unique UNIQUE (favourite_thing)
;
Actually you present a one-to-one relation.
So you can:
1. Hold it in Person table.
2. Hold it in Belonging table.
3. Hold it in both.
4. Hold it in separate table.

Usage of Primary and Foreign keys in an EER diagram

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.