Using a MS Access SQL query to create a second table with a one-to-one relationship with the existing table - sql

I am using Microsoft Access 2016 and am attempting to use an Access SQL query to create a new table with a one-to-one relationship with an existing table.
I have already set-up the first table of the database - employees.
I have then used another query to create a second table (desks) and to link the EmployeeID field as a foreign key. However, it creates a one-to-many relationship, rather than a one-to-one relationship.
How do I need to alter the queries to create a one-to-one relationship?
Thanks for your help!

EmployeeID in table Employess linking to the unique attribute of table Desks guarantees a 1-to-1 relationship even if the relationship diagram says otherwise.
I would claim that the relationship diagram does not consider unique attributes and therefore shows it as a 1-to-many relationship.

Related

Entity Relationship Diagram - 3 Tables that are not linked

How do I go about making an ERD in which the 3 tables I have are not linked in any way, so there is not a foreign key amongst the tables?
One table for registered users
One table for storing events (e.g event name, location, time/date etc.)
One table for image storing (for the gallery)
You have answer right into your question:
3 tables are not linked in any way
So you just need to add desired entities on your ERD diagram and no relations. Your Entity Relationship Diagram will be boring and looks like:

Is this actually a one-to-many relationship?

I have three tables in my PostgreSQL database:
User - Contains a username and password
MaleProfile - Contains information related to each male user
FemaleProfile - Contains information related to each female user
Initially, instead of having separate MaleProfile and FemaleProfile tables, I had a single Profile table. In that situation, I would have had a one-to-one relationship between the User table and the Profile table. But I've since decided that I really need separate profile tables for men versus women. In this new situation, each record in the User table must map to one and only one record in either the MaleProfile table or the FemaleProfile table (but not both). From the other direction, each record in the MaleProfile table maps to one and only one record in the User table. The same holds true for each FemaleProfile record.
Strictly speaking, the relationship between the User table and each of the profile tables is one to zero-or-one. But are these relationships essentially just one-to-many relationships in the sense that "many" in this case means just zero or one (but not more than one)? If so, would I express them as you would any one-to-many relationship by creating a foreign key column in the MaleProfile table and in the FemaleProfile table, each of which points to the PK column in the User table? Would I need to add any additional constraints to the profile tables to maintain referential integrity?
Thank you.
Just make sure the referencing column in your male/female tables has a uniqueness constraint on it as well as a foreign key constraint. This is a 1-to-1/0 relationship, not 1-to-many.
CREATE TABLE MaleProfile
(UserId INT NOT NULL PRIMARY KEY
REFERENCES "User" (UserId));
I believe that you should put the employeeID number in the profile tables and enforcing a unique constraint in those tables. Although I can't really think of how to keep someone from having both a male and female entry, I believe that this way is the way to go.
Check out https://stackoverflow.com/a/669015/1504882 to see a similar situation to yours, but instead with different types of employees.

Translating ER to SQL DDL

When I am translating an ER to SQL DDL I need to Create a table only for the entities or for the relations too?
Yes, you need to create tables for both entities and relationships. Also, keep in mind that you have to include foreign keys and link your tables
It depends what type of relationship you have. If it is many to many relationship then it must require a separate table for the relationship itself. Any way you can search on google ER diagram to relational database or look the text book on the relational model chapter of Modern database management sytem of author Hoffer.
You require a CREATE TABLE statement for each ENTITY.
Your relations ships are generally implemented as FOREIGN KEY CONSTRAINTS or FOREIGN KEY INDEXES between those tables.
Each entity becomes a table and each many to many relationship becomes a table.
Add the child columns (FK column) to the (child) tables also. When you create a N:M (many to many) relationship in for example DeZign for Databases, you see that an intersection table is create automatically. The columns which are added automatically are in first instance the columns of the primary key of both tables. You can see that in this video:
http://www.datanamic.com/support/vd-dez001.html

link two database tables

A very quick question. Is there a term for a table who's primary function is holding two foreign keys? For example, if I have two tables, user and group, is there an official term for the "membership" table that holds user_id and group_id?
It's an intersection table which supports many-to-many relationships.
A linking/intersection table, which resolves a many-to-many. For instance, if a car can have more than one driver, and a driver can have more than one car, you would use a linking table to resolve the many-to-many relationship. I drew an example ERD in OmniGraffle as an illustration:
As Stefan Already answers: There are link/insersection tables for such a case. They will map many-to-many relations since there is no other possibilities in databases. You only have to create a table "UserGroup" and two columns for the specific ids which are both primary and foreign keys.

SQL Database : Relationships & schema

i am a student and i have a question about database schema , i already created the Entity Relationship Diagram [ERD] and in this step i should do the database schema , must all the entities on my database has a relation between them and the other entities ? i.e : each entity should have a foreign key for the entity before it , because i can create all the tables and only 2 tables can have a relation between them and i control the other tables using C# program i am going to create .
** in my ERD all the entities have a relation between each other .
...must all the entities on my database has a relation between them and
the other entities?
No. Entities of course can stand on their own. Although in practice if you're finding that you have very few relationships you're probably doing something wrong...
You're sort of right in saying that only two tables can have a relationship but I'd word it as a relationship can have only two participants; tables can certainly relate to more than just one other table.
Any time you create tables in the database if there is a relationship between the information in the tables then you should create a relationship to make sure that when the data is entered into the tables that the reference data will be there. This will enforce referential integrity. For example:
Employees Database:
EmployeeInfoTable:
pk EmployeeID
EmployeeName
EmployeeDateHired
fk SalaryID
EmployeeLoginTable
pk LoginID
fk EmployeeID
Username
Password
SalaryTable
pk SalaryID
SalaryRange
So what this would do is make sure that if you add a salary to an employee it has to exist in the Salary table and if you added a login then you would have to have an employee to reference. It's just an added layer of security to make sure that the data that is being input into the database is valid and can be used.
While you can technically control this with C# or whatever programing language you want it's easy to forget these little rule especially in a database that has like 200 tables or more. So getting in the habit of using relationships and maintaining referential integrity is a very good thing to do.
All entities need not have relationships with each other. A relationship will only exist between two entiries in the ERD if one entity somehow relates to the other.
The general rule of thumb for creating table is:
Each entity in the ERD will have a table for it
For a one-to-many relationship in the ERD, the many side table will contain a foreign key to the one side table
For a many-to-many relationsip, there will be a separate table for the relationship that will contain foreign keys to both the entity tables
If a relationship has an attribute, there will be a separate table for the relationship that will contain foreign keys to both the entity tables
the primary key should always be id due to convention, and your foreign keys could be called othertablename_id so if a table has a link to all the other tables it will need a key for each one of those.
for example: if dogs has more than one owners but owners can only have one dog, you could just have dog_id in the owner table. but then you would have to query the owners table to find all the owners of one dog.
if dogs have more than one owner and owner can have more than one dog, then you have to have a join table called dogs_owners with id and both foreign key dog_id and owner_id
of course you can name your tables and fields whatever you want but it's convention to use id and _id
yes an entity usually equates to just one table, apart from when you have many-to-many relationships between your entities.