How can I change a primary key value in one-one relationship? - sql-server

Hi
I'm using entity framework 4 as business layer.
I've two table in one-one relationship:
Users <---> Employees
Employees table have the same primary key as Users table, so when I add record to Users i have to add one to Employees with the same PK value...and that throws an exception.
Actually I'm doing this using entity framework :
// after adding new Users entity, I add new Employee item to it
newUserEntity.Employee = newEmployeeEntity;
What should I do to be able to insert the PK manually ?
Thanks in advance

There are a number of ways to fix this.
The first one is why do you have two tables. Could you instead have a single table but have a User and an Employee view of that table.
The second is that if you have two tables, why do both of them have an auto generated primary key. You could generate the key in the program (or get it from a key table) and then just insert the key values in the tables.

Related

MS Access tables relationship

I am using MS Access 2016 and I created tables (entities) Employees and Managers with the following attributes:
Employees
-EmployeeId (Primary key)
-Name
-LastName
-Email
-Phone
Managers
-ManagerId (Primary Key)
-EmployeeId (Foreign Key & Unique)
-Position
I am trying to create a relationship between the 2 entities that meet the following requirements:
Managers(EmployeeId) is a unique value.
for every record in Managers table there has to be one record in the Employees table (because the manager is an employee)
I can create a one to one relationship between the tables and I think that is fine because there can only be one ManagerId per EmployeeId (When the employee is actually a manager) but my issue is that when I add a record in my Employees tables Access is forcing me to have a record in the managers table even when I am adding a non-manager to the Employees table. Any suggestions on how I can create a relationship between the 2 tables is greatly appreciated.
When defining a relationship using the visual Relationships window, Access usually does a good job determining which is the primary table and which is the "related" table based on the indexes of the fields being related. But for a 1-to-1 relationship where the indexes on both fields are unique, Access uses the first table you click as the primary table and the second table--the one you dragged the first field onto--as the related table. Thus, it requires that a value be in the primary table before the related table record can be added. I assume that you defined the relationship by click and dragging in the wrong order/direction.
Delete the existing relationship between the tables. Then redefine the relationship by first clicking on the Employees.EmployeeId field, then dragging that field to the Managers.EmployeeId. The relationship window which pops up should show Table/Query: Employees on the left with Related Table/Query: Managers on the right.

Access Database - Two foreign keys in same table related to same primary key

This is in relation to an Access Database. I have a first table that lists employees with a primary key of id. In the first table, I include all employees (whether overlords or underlings). I have a second table that identifies approvals of expenses. I have one column that is a foreign key referencing the underling that made the expense. I have a second column in the same table that is a foreign key referencing the overlord that approved the expense. When creating a query, the query breaks because of the dual relationship. Could someone help me with some code to put in the query to lookup the overlord from the employees table based on the overlord id? Thanks.
I think I have it solved using the DLookUp Function. It looks something like this:
Overlord: DLookUp("[employee_name]", "employees", "ID = " & overlord_id)
I would still like to know if there is a way of doing this just with relationships.

Make a entry mandatory if exist in a table

I have the next design doubt:
I have athlete entity, the athlete can have many nationalities, so I have second table called countries. Then between athlete and countries there is a many-to-many relationship. I create another table athlete_country to resolve the many-to-many relationship.
My question: Is there a way to achieve that athlete_country entry be mandatory for any entry in the athlete table?
I am working on postgresql. Is there a way in another database server?
No, this is not possible to do it this way for logical reason: athlete_country tables references athlete table, and if you do back reference (in fact you can do it) you will not be able to insert any row in either table because each row should reference to the row in another table, which isn't inserted yet.
The solution is to use many-to-one relationship in addition to many-to-many which you have described. For example, you can add "primary_country" field to athlete table which references directly to the country table. In that case you can be sure that any athlete has relationship with at least one country, specified in "primary_country" field and, optionally, with other countries listed in the athlete_country table.
create table country(id serial primary key, name text);
create table athlete(id serial primary key, name text, primary_country int references country(id));
create table athlete_country(athlete_id int references athlete(id), country_id int references country(id), primary key (athlete_id, country_id));

Database table model advice

I am making a Django web app and need help designing the a table within the DB.
I am to insert into the table an employee with a specific employee ID. Lets say there are three employees with the IDs (15039, 98443, 29234). Would the employee ID be the primary key or do I have to make some arbitrary column starting from 1 the primary id with employee id as a standalone column?
In a sense what I am i asking is if the 15039, 98443, and 29234 employees were inserted into the table with empl ID being primary key which order would the DMBS order them?
You did not specify which database you will use, but most likely the primary key will be the clustered index, in which case the database will order the rows by that id.
Many argue you should always create an auto-increment artifical primary key, and that usually saves you a lot of pain in the long run.
However, if you know the value will always be unique and you won't ever need to change the value, you can opt to use it as the PK for the table.

Database schema design for schedules

I have two tables: Companies and Employees.
I also have a relation table Employs which contains the foreign keys company_id, employee_id as a composite primary key. Note: employees can work at multiple companies.
I would like to have another table EmployeeSchedules which simply contains schedules (company_id:integer,employee_id:integer,start_time:datetime, end_time:datetime) for employees working at a company. This will end up being displayed in a calendar widget of some sort.
However, with this design I would have to verify at the application level that the employee actually works at the company before adding a schedule.
I was wondering if there would be a better way to represent this at the database level or just stick with verifying at the application level? For example, if there was a way to link the EmployeeSchedules pkey (company_id,employee_id) with the Employs pkey (company_id, employee_id). Any other design recommendations are welcome!
I would re-define the schema, and add another table:
Person(id, name)
Company(id);
Employee(id, companyId, personId);
Schedules(id, employeeId, startTime, endTime);
That means a an employee record can only be bound to one company. A person can have multiple employee records however. All the "id" columns are unique, and are the primary key of the table. "companyId" refers to the primary key of the company table and so on.

Resources