So my intention here is to make the "Employee" contain 2 references to the same Primary Key in the "EmployeeContactInformation" table. The reason for this, is I want my employee to contain 2 different copies of the same table. E.g. 1 for work contact info, another for home contact info.
How would I implement this and what relationship would I use?
I'm assuming 1 to Many?
Current database screenshot
Lets change the design of your database:
Creat a table named as ContactInfoTypes. It will hold the definitions of each ContactInfoTypes you want (in your case: workContactInfo and homeContactInfo). It will have two columns (contactTypeId, contactTypeName)Add an extra column to named as ContactInfoType (DATATYPE number) in your EmployeeContactInfo TABLE. The Column ContactInfoType will hold a Foreign-Key value of above TABLE ContactInfoTypes
When you insert a contact in TABLE: EmployeeContactInfo, you would have to Insert two rows (one with the number value stored against workContactInfo and one with the homeContactInfo).
Employee can have several contact Infos ( home and work like you mentioned). So therefore the relationship should be one to many
To model this you should add a foreign key of the one side of the relationship into the many side . Therefore you should add an employee_id column in the EmployeeContact table. This way each employee contact row will be connected to an employee
"Work extension" column in the employee table can be moved to the EmployeeContact table and renamed "extension" since it is a phone number extension that could be for a home phone or work phone
"Home phone number " column in the EmployeeContact table should be renamed to just "phone number" because the table is for both home and work
Should save another column in the EmployeeContact table which saves information about work/home
Related
I have a CUSTOMERS table and a CONTACTS table the relation between them is one to many obviously.
also I have PROJECTS table and PROJECT_CUSTOMERS table with relation one to many and with relation one to one between CUSTOMERS and PROJECT_CUSTOMERS.
my problem is that I have a fifth table PROJECT_CONTACTS ....I can't figure which tables shall I refer to in this tables, currently I am refering to PROJECT_CUSTOMERS and CONTACTS table, is this correct or there is something better ?
Your title refers to "foreign keys" but your question just seems to be about what columns should go in what tables.
First, decide what situations can arise and what you want/need to say about them. (This will your tables, columns, candidate keys, foreign keys and constraints.)
Every table holds rows that make some predicate (statement template parameterized by column names) true. Your design seems to have:
CUSTOMERS(id, ...) -- ID identifies a customer and ...
CONTACTS(id, ...) -- ID identifies a contact and ...
PROJECTS(id, ...) -- ID identifies a project and ...
PROJECT_CUSTOMERS(pid, cust_id, ...) -- project PID has customer CUST_ID and ...
PROJECT_CONTACTS(pid, cont_id, cust_id)...)
-- project PID has contact CONT_ID and project pid has customer CUST_ID and ...
A foreign key has a table & column list referencing a table and column list that forms a candidate key. It says that lists of values in the first table appear as lists of values in the second table. Where that is so, declare a foreign key.
I must create a table for handling student data which has two columns department code and register number,. For Eg. dept 101 has 30 students with register numbers (1-30) And dept 102 has 30 different students with same register numbers (1-30) . for instance.,The student with regno:3 who belongs to dept:101 and another student with regno:3 but in the dept:102 are in same table.,There can be same regno's but different dept code is there a way to relate my two columns.. I have added another image for more clarifications Table Structure with data My problem is how can i have unique set register numbers for each deparment in the same column to avoid duplication. The Values of reg_no with same Department Code must have a unique set,.. and another set of regno with a different dept_code must have another or individual set of unique set of numbers
Can i Have Multiple unique sets in a single Column..??
The
Table sample image
Is there any solution or should i use another table for each dept_code
Tried on Googling didn't Get a result yet . Thanks in Advance
try this
UNIQUE KEY 'thekey' ('dept','regno');
more about the background is found at this StackOverflow post
Its just a quick question :
I have user table and it has fields name, address and books_bought. books_bought is foreign key and its value is some PK from other table. Now after 1st insert, I will fill out all of this fields , but after second INSERT I want only to add a additional books_bought, so that am creating array of books_bought values?
You're doing it the wrong way around - this is a one-to-many relationship i.e. many books bought to one user. You need to have the foreign key on the many side of the relationship, so instead of having a foreign key to books_bought on the users table, add a foreign key to users on the books_bought table.
If you have a books table and a users table, then this is a many-to-many relationship and you will need a link table to go between them to hold the foreign keys.
You should not have more than one book in the books_bought cell because it will violate the atomicity constraint fo the database tables. You have to have a separate entry for each book_bought. This would cause a lot of redundant information as name, address would be repeated for each book bought by a specific person.
To solve this, you have to split the table into something like this:
R1(primary_key , name , address) and R2(foreign_key , books_bought)
Here foreign_key refers to primary_key of R1
I record the datas of a competition in an MySQL database.
I need to have unique entry for each player so i’ve done this :
alter table xtu_datas_competition add unique index(email);
It works perfectly.
But there are several competitions, so i have also a colum id_ competition and i want to have unique entry for email and for specific colum id_ competition
For example :
2 identical email for 2 identical colum id_ competition : duplicate
2 identical email for 2 different colum id_ competition : not duplicate
How can i adapt my previous code ?
You simply have to define a unique constraint containing both columns:
alter table xtu_datas_competition add unique index(id_competition,email);
Hope this will help you.
I would like to set an auto-update relationship between fields in two tables. One table contains information on the team and team leader, while the other table contains the team member name list with team and leader information included. I want the second table updated when the first table is updated (the team name is fixed and the team members always stay in the same team, while the team leader might be changed).
I set the team name as the primary key in the first table, and member name and team name as the primary key in the second table.
I want to use the relationship in Access, but it always shows: no unique index ... for primary table. Any instructions or help with any misunderstanding of the use of relationships is appreciated.
Since the Team Name is fixed, I would suggest creating a Team_ID field in both tables. Give each team an ID (it doesn't really matter what ID they get, so long as you're consistent across both tables) numbered 1 to however many teams you have.
Set the primary key in both tables to be Team_ID.
Set up a 1-to-Many relationship between the two tables, since you can have several team members on each team. Set that relationship up to be Cascading, and it will automatically update the second table when the first table is updated.
Also, I completely agree with ElectricLlama's statement above. Normalize your tables so you're only using each field a minimum number of times. Your first table should have Team_ID, Team_Name and Team_Leader, and your second table should have Team_ID and Team_Member. When you need to see the leader's info, join the two tables on Team_ID and bring in the necessary fields from each table.