databases Ms access - ms-access-2007

I have created database for employees in access 2007.
Employee table
Employee code (primary key)
Name
Department
Etc
Leave record table
Employee code (foreign key)
From date
To date
Number of days etc
Now, I am facing the issue of how to enter multiple leaves under one employee code because code is primary key, and it won't let me enter multiple employee code.

[Employee_code] should not be the Primary Key for the [Leave_record] table. You need that field in the table as a Foreign Key (as you have noted), but it should not be the Primary Key because the Primary Key cannot contain duplicates.
Consider adding an AutoNumber field named [Leave_record_ID] to the [Leave_record] table and make that the Primary Key. Or, you could use a composite Primary Key based on the [Employee_code] and [From_date] fields.

Related

How to create an SQL column that can have multiple values?

I created a table in SQL using PostgreSQL called "tenants". Below is the code for the tenants:
create table tentants (
id bigserial not null primary key,
tenant_name varchar(1000) not null,
offices int not null,
number int not null,
email varchar(1000)
I want to include the ability to add multiple values to "office" in case a tenant rents more than one office. I don't want to use JSON for this. I tried creating a related table called "offices" but That could only allow me to add one office per tenant.
What is the best approach for this?
You can use text, that works for me with ids separated by commas like this
4,3,67,2
Anyway the proper approach would be another table and name it tenant_offices
tenant_offices
columns >
tenant_id
office_id (well ofcourse you should have atleast an office table)
You can create an "tenant_offices" table (like you did before), having as structure :
id, tenant_id, office_id,... where id is the primary key of the "tenant_offices" table and tenant_id and office_id are foreign keys.
tenant_id which refers to your tenants table and office_id which refers to your offices table.
Here, the tenant can therefore rent several offices.
Hoping to have enlightened you, or helped !
I assume that the relationship is one-to-many tenant to office (that is, office can be rented only by one tenant)
Then you have to create table offices with a foreign key that points to the tenant:
CREATE TABLE offices (
id bigserial not null primary key,
tenant_id bigserial foreign key references tenants(id))
additional columns if needed
note that in this version you will not retain history of rents (you will have to run update on offices to change tenant_id)
EDIT: In case of a many-to-many relationship (that will also allow us to retain history of rents) we need to create a relationship table:
CREATE TABLE TenantsOffices (
id bigserial not null primary key
tenant_id bigserial foreign key references tenants(id),
office_id bigserial foreign key references offices(id),
start_date datetime,
end_date datetime)
Useful information: https://www.sqlshack.com/learn-sql-types-of-relations/

A table that does not have a primary key and has only one foreign key, can the foreign key be duplicated?

Assuming that the EMP_SALARY_INFO table does not have any other relations apart from the EMPLOYEE table.
My question is there is no primary key for the EMP_SALARY_INFO table. There is only one foreign key. When I create entity instances for the EMP_SALARY_INFO table, my foreign key i.e., EMP_ID repeats. Is this valid or does it violate any relational database rule? If this repetition is a violation, would I have to create a unique id (primary key) for the EMP_SALARY_INFO table?
That is perfectly OK. I would call out that in your example there is a possibility of having duplicate date ranges for the same employee. For example, if a mistake is made during data entry you may have two different pay rates for the same date range. Now, this may be a valid scenario in your case, however if it’s not then consider creating a unique constraint on Emp_id, from_date and till_date.
Foreign key of a particular table is a primary key of the table its referring to. Hence, in your case above as long as you have data in your empid of employee and same data if referred from emp_salary_info wont cause any sort of problem even if it repeats in the emp_salary_info table as its not repeating in the table its referring to i.e. Employee

Creating unique primary key to ignore duplicates

I have a main large table which I have had to put into 3rd normal form and into smaller tables (with primary and foreign keys linking them). The table is about renting books.
I have a customer table which I need to create a primary key for. In the main large table there are duplicates of the customer_id, as the table as a whole is for renting the books, so one customer may have more than one renting.
The table I am currently trying to add a primary key for will not have any nulls or duplicates, however i am unsure how to create the primary key for this without the error- unsure how to make it unique.
CREATE TABLE customer AS
SELECT cust_id, country_id, name, address, postcode
FROM BOOKS
WHERE cust_id != 0;
ALTER TABLE customer
ADD PRIMARY KEY (cust_id);
Is anyone able to help me in how to create the primary key on my customer table, but just taking each unique cust_id from the main table.
In SQL Server the straightforward way to add unique keys is to use IDENTITY. Identity fields are integer fields that auto populate successive values by a specified start value and interval. If you don't specify the interval it will start at 1 and increase the value by 1 each time a value is assigned.
While it's usually done when creating a table, you can do it in your ALTER TABLE step, and it will assign values when added to an existing table. I've explicitly specified the start value and interval that matches the default to show the syntax :
ALTER TABLE customer
ADD cust_id int not null PRIMARY KEY IDENTITY(1,1)

Do I need to insert a value to a foreign key in SQL?

Simple question.
If i had a table users which contains columns like username, password, and an auto incrementing id which is the primary key.
And another table called names which contains first_name, middle_name, last_name and a foreign key called user_id which references the id in the users table.
Do I have to insert a value to that foreign key? Or SQL will automatically copy the value of the id in the users table?
Another follow up question, What is the data type of the user_id field in the names table?
Yes, you will have to insert the foreign key. When you insert a record into the names table, SQL will not automatically be able to know which user the names record is referring to.
As user_id is referring to an id field that is auto-incrementing it is most likely going to be an integer field.

About primary key in the table?

I would like to have some explaination about the characteristics of a primary key in the table of such database.This is for vb 2008 express edition,since im new to this language,if this is true, as far as i understand about the characteristic in setting the primary key on each field for true.My question is if you are doing an updates/edit records in your table using the DataContext,if you setup the primary key for true of one of your field in the table it would edit all records in one datarow but if you put the primary key for true in all fields except one of them,all the records in the data column of that field which primary key is false could be edited.Basically its impossible to edit all records in the datarow and all the records in the datacolumn of the table in such one event.
Is there any further explaination about the characteristics of primary key in the table?
The purpose of the primary key in a database table is to identify the field (or fields) that make up a value that uniquely identifies each record on the table. A typical examples are CustomerID in a Customer table; each customer is given a unique ID, and this ID can be used to link the customer into other tables (such as an order table).
Sometimes there are tables where not one single field will contain a unique value for each record. In such cases more than one field can be set as the primary key. In those cases, the combination of values in the primary key fields should always be unique.
So, on the database level, this is not related to the possibility to edit the field or not.
Of course, wikipedia has some content on the subject.