Connecting two tables through a auto-generated primary key in Access - sql

I am attempting to create a relationship between two tables in Access 2013. The PROJ table has many fields, with the primary key being PID, which is a randomly generated auto_increment number. I would like to use this as the foreign key for another table (JDD) to create a link between them. The primary key for JDD is another non-randomly generated id, UII, and I want the the PROJ table to autopopulate the PIDs into the JDD table. However, logically, I would assume that since it is a randomly generated number that there is no way to connect them and have them auto-populate unless I manually attached the PID to each UII. Is there anyway around this? Access would let me create the relationship but I couldn't find the plus signs to auto-populate data in the Datasheet view.
Thanks!

Related

Is there a way to create two entries into separate sql database tables that are linked by a foreign key?

I am currently trying to create an entry in a SQL table using ASP.NET Core and LINQ, then create entries in another table where these new entries have a foreign key pointing to the first entry I have just created.
All the entries seem to create just fine however the latter entries have foreign keys set to Null when I look at them in the database. I started with the creation of the entries in the same 'Unit Of Work' and have since tried to split them out into two Units of Work.
All this is begin run as a background job using Hangfire.
If you are talking about SQL purely, I would create a trigger that meets the conditions on the tables to update the corresponding entries, as I don't see any code, I can't give you an piece of code.
For more information: https://learn.microsoft.com/en-us/sql/t-sql/statements/create-trigger-transact-sql?view=sql-server-ver15.
I think that the problem is that at the time that your problematic entries are entered into the database, the foreign keys (primary key in another table) are not set yet, they don't have a value yet.
If you are using entity framework, this can be sorted, or you can add triggers to the database so, when the entry is created, you can look into the other table to get its primary key (foreign key) and modify it

Multiple tables for a single foreign key

I'm currently making a database for my java web app for Tourism.
Only register user can book a tour but can take along several people.
For this, I separate into User table and Guest table, each with its own primary key.
Everything is set so far, but when it comes to making my BookRoomDetail table, I have to fill in which person for which slot in the room. the problem arises when both register user and guest can fill this slot, and they're from 2 different tables.
How do I set the foreign key(or anything else) for this?
As I got your problem, you can add one more field for MainUser in your tblBookRoom Detail as a foreign key. And whenever any user books room you can add his/her primary key and guest primary key in forwhom field.
**tblBookRoomDetail**
ID (Primary key Of Table)
RoomId
For Whom (For MainUser or Guest)
MainUser (MainUser Primary Key Who is doing this reservation)
Slot
FromDate
ToDate
BookId
This is not possible to insert two different primary key value in a column of other table, if you have relation in them.
You can do this without the making the relation in them.
But it's not a good process because if the both key value same in this case you can't identify the actual.
If you want to achieve this goal then you should have a table tblAllUsers which primary key is also primary key of the tblGuest and tblUser and then you can make relation to tblBookRoomDetail table direct to tblAllUsers. In this case you can differentiate the Guest user or Registered Users.
or
Can Create the two different columns in tblBookRoomDetail, one for Guest user and second for Registered user.

One field primary key in multiple tables

My database in Access has a table named Program and its primary key is Program ID. I have some other tables which have additional information relating to the Programs in the Program table. Each of these tables has the field Program ID in them. For example: I have a table named [Additional topics] . The [Additional topics] table has multiple value fields that stores information about the topics and sub topics associated with the Programs. The Program ID field joins the Program table with the [Additional topics]. The Program ID in the [Additional topics] table has no duplicates (as I have the other fields as multiple value fields). So, My question is, should I make Program ID as a Primary key to the [Additional topics] table or should I create a separate ID field for it?
Please help
Thank You
Short answer no
If the ProgarmID only appears once in the additional topics table this would be a 1 to 1 relationship and you should ask yourself do you need the separate table or could you add the fields to the Program table.
The only reason I can think to use foreign key (the) in as a primary key would be as part of a primary key where the primary key is composed of two foreign keys.
Since a program can have one or more Additional topics then it is not possible that the program ID is unique for additional topics and so you must use another ID and program ID will just be a foreign key

lightswitch create lookup list

I've created a simple contact database with three tables, Corporate, Contact, and Country. I've created a LightSwitch application on top of it and am trying to create a countrycode lookup. i.e. on the new Corporate screen, for countrycode field have a lookup list coming from the country table.
How can I do this? If I was writing a SQL query it would purely be:
SELECT CountryCode
FROM Country
In the query designer it makes you create filters, I don't want to add any filters! Also, I've created foreign keys, unique key constraints but LightSwitch doesn't seem to recognise them and complained when I was importing my tables?
* UPDATE *
Error below
Normally, if relationships are set up correctly, this would just happen for you automatically. LightSwitch is very good at this actually.
If I understand correctly, your data source is an external SQL database, that you've added into your LightSwitch application as an attached data source. If there was a relationship between the Client table & the Country table, then LightSwitch would create the look-up combo box in the Client screen automatically. But it's not enough to just have a foreign key column, you have to create a relationship between the two tables.
You don't mention what the columns are in your tables, or more importantly the column data types are. The Country table should have an integer primary key, plus a text column for the name. In the Client table, it should also have an integer primary key, plus an integer foreign key. The relationship should be created in the Client table between its Country foreign key column and the Country table's primary key column.
You mentioned that LightSwitch "complained" when you were importing your tables. What was the error message?

Copying 1-to-1 relationships with identity fields in SQL

Suppose you have two tables that are in a one-to-one relationship; i.e. the primary key of the child table is also the foreign key that links it to the parent table. Suppose also that the primary key of the parent is an identity field (a monotonically increasing integer that is assigned by the database when the record is inserted).
Suppose that you need to copy records from these two tables into a second pair of identical tables -- the primary key of the parent is an identity, and the foreign key linking the child to the parent is also the child's primary key.
How should I copy records from one set of tables to the other set?
I currently have three solutions, but I'd like to know if there are others that are better.
Option 1: Temporarily disable the
identity property in the destination
parent table. Copy records from the
parent table, then the child table,
keeping the same values for the
primary key. Cross your fingers that
there are no conflicts (value of
primary key of source table already
exists in destination table).
Option 2: Temporarily add a column to
the destination parent table to hold
the "old" (source) primary key. Copy
records from the parent table,
allowing the database to assign a new
primary key but saving the old
primary key in the temporary column.
Copy records from the child table,
joining the source child table to the
destination parent table via the the
old primary key, using the join to
insert the record into the
destination child table with the new
primary key. Drop the temporary
column from the destination parent
table.
Option 3: Copy sequentially
record-by-record, first from parent
to parent, then child to child, using
DB-provided "identity of last
inserted record" functions to ensure
that the link is maintained.
Of these options, I think option 2 is my preference. Does anyone prefer one of the other two options, and if so, why? Does anyone have a different solution that is "better"?
This is one reason why it is so critical to remember that even if you use a surrogate key (like an identity column), you always need a business key. I.e., there always need to be some other unique constraint on the table. If you had that, then another choice would be to insert the values into the copy of the parent table without the identity values and use that unique key to insert the proper parent value for the child rows.
If you do not have that unique key, then given your situation, I agree that your best solution would likely be Option #2.
Before you decide on an approach to copy data to new set of tables, you should investigate following items:
a list of tables that reference the data from the parent and child tables (both sets of tables)
Are there any stored procedures/triggers that utilize the data in these tables?
How does this table get populated? Is there an application/data feed that inserts data in this table?
How does the data in this table get deleted?
What is the purpose of the primary key beyond ensuring uniqueness in the table? For this you will have to understand how the data in the table is used by the application.
Based on the answers, you should be able to pick the right solution that will meet the requirements of the application.
My money is on Option 1 (see SET IDENTITY INSERT, http://msdn.microsoft.com/en-us/library/ms188059.aspx).
But: Why are you copying them?
If you are just altering the table schema, or migrating to new tables and retiring the old ones, why not use ALTER TABLE.
If you are going to run them side-by-side you probably need the keys to match.
But to answer your question, use Option 1, definitely.