How to make this relationship in MS Access (picture) - database

I need to connect id_player from table Players to player_score and player_assist in table Goals. The primary key must refere to these two.
Is there any way how to do it?
Access writes me "The relationship already exist".
I will be grateful for every answer.
Access screen - Access
Is this the correct solution? - Possible solution

For each Foreign Key reference existing in a table; a separate join must be made to the main/base table to get the related data.
In your case; add Players again to your query and join it to the Goals.player_assist; while joining Players to Goals.Player_Score.
As to why: The same join can't get both pieces of data as they represent different relationships to the players table.
in SQL this would look like:
SELECT G.*
, PS.id_Player as ScoredByID_Player
, PS.first_name as ScoredByfName
, PS.last_name as ScoredBylName
, PA.id_Player as AssistedByID_Player
, PA.First_name as AssistedByfName
, PA.Last_name as AssistedBylName
FROM GOALS G
INNER JOIN Players PS
on G.id_Player = PS.Player_Score
LEFT JOIN Players PA
on G.id_Player = PA.Player_Assist
Note we alias the field names from players so we know which is scored by and which is assisted by. We also alias the tables for readability and because we have to copies of "Players" and for the engine to keep track of which table we mean; we have to have them "named" differently.
The reason why I LEFT join (outer) the second time, is because not all scores have an assist; but all scores have someone scoring them. So the first join to players can be an inner join but the second join may not have an assist; and we still may want to see details for all goals made. If we made the 2nd join an inner one, we would lose all scores where an assist wasn't involved.

Related

How to compare tables for possible combinations to match people

I have to tables. A table that we call "The Vault" which has a ton of demographic data and Table A which has some of the demographic data. I am new at SQL Server, and I was tasked with finding a list of 21 students in The Vault table (Table B). Problem, there is no primary key or anything distinctive besides, FirstName, LastName, BirthMonth, Birthday, Birthyear.
Goal: We could not match these people in the conventional way we have, and so we are attempting a Hail Mary to try to see which of these shared combinations will perhaps land us with a match.
What I have tried doing: I have placed both tables on tempt tables, table A and table B, and I am trying to do an Inner Join but then I realized that in order to make it work, I would have to do a crazy join statement where I say (see the code below)
But the problem as you can imagine is it brings a lot more than my current 21 records and is in the thousands so then I would have to make that join statement longer but I am not sure this is the right way to do this. I believe that is where the WHERE clause would come in no?
Question: How do I compare these two tables for possible matches using a WHERE clause where I can mix and match different columns without having to filter the data constrains in the ON clause of the JOIN. I don't want to JOIN on like 6 different columns. Is there another way to do this so I can perhaps learn. I understand this is easier when you have a primary key shared and that would be the JOIN criteria I would use, but when we are comparing two tables to find possible matches, I have never done that.
FROM #Table a
INNER JOIN #table b ON (a.LAST_NAME = B.LAST_NAME AND a.FIRST_NAME = b.FIRST_NAME.....)```

Oracle Implicit Partition Pruning

I am trying to optimize a long-running stored procedure at my company. From checking the query plan, it looks like we could make some nice gains by writing the query to allow for better partition pruning. The trouble is, it seems like doing so would create a very verbose query. Essentially, we have a bunch of tables that have a foreign key to client and "sub-client". In many cases, data is not shared between clients/sub-clients so we partitioned on those IDs for each table. Here's a sample query to show what I mean:
SELECT ...
FROM CLIENT_PRODUCT cp
INNER JOIN ORDER o ON o.product_id = cp.id
INNER JOIN PRICE_HISTORY ph on ph.product_id = cp.id
WHERE cp.id = ?
All of the tables have a foreign key that references a client and sub client. The same client product cannot belong to two different clients or sub clients (Sorry. This example is using made up tables and is a bit contrived)
I can improve partition pruning by doing the following:
SELECT ...
FROM CLIENT_PRODUCT cp
INNER JOIN ORDER o ON o.product_id = cp.id and o.client_id = l_client_id and o.sub_client_id = l_sub_client_id
INNER JOIN PRICE_HISTORY ph on ph.product_id = cp.id and ph.client_id = l_client_id and ph.sub_client_id = l_sub_client_id
WHERE cp.id = ? and cp.client_id = l_client_id and cp.sub_client_id = l_sub_client_id
With this change, I just explicitly say what partition Oracle can look at for each join. This feels pretty gross though because I've added a bunch of mostly repeated SQL that doesn't functionally change what is returned. This same pattern would need to be applied for many joins (larger than the example)
I know that our application has an invariant that any Order for a Product must belong to the same Client and Sub-Client. Likewise, any Price-History item must belong to the same Client and Sub-Client as the Product. The same idea applies to many pairs of tables. In an ideal world, Oracle would be able to infer the Client and Sub-Client for each join from the other tables in the join because of that invariant. It does not seem to be doing that (and I understand that my specific invariant does not apply to everyone). Is there a way I can get Oracle to do this implicit partition pruning without me needing to add all those additional conditions? It seems like that would add a lot of value across the codebase and remove the need for all these "unnecessary" explicit joins.
There's also the possibility that I'm just totally overthinking / misunderstanding this so other suggestions would be great.

TSQL Joining three tables where none have a common column

This will probably be pretty simple, as I am very much a novice.
I have two tables I have imported from Excel, and pretty much I need to update an existing table of e-mail addresses based off of the email addresses from the spreadsheet.
My only issue is I cannot join on a common column, as none of the tables share a column.
So I am wondering if, in a Join, I can just put something like
FROM table a
INNER JOIN table b ON b.column 'name' = a.column 'nameplus' `
Any help would be appreciated!
A join without matching predicates can be implemented effectively be a cross join: i.e. every row in table A matched with every row in table B.
If you specify an INNER JOIN then you have to have an ON term, which either matches something or it doesn't: in your example you may have a technical match (i.e. b.column really does - perhaps totally coincidentally - match a.column) that makes no business sense.
So you either have
a CROSS JOIN: no way of linking the tables but the result is all possible combinations of rows
Or:
an inner join where you must specify how the rows are to be combined (or a left/right outer join if you want to include all rows from either side, regardless of matched-ness)

LINK OR JUNCTION between three tables

In database conception, could I make a join between three tables ? Like this:
Edit:
I am asking on design, and not code !
this database design is possible and valid. But it is extremely hard to say yes or no, because every database design heavily depends on the requirements. As I can see from the image, you have done something similar to this:
The red rectangle represents FOREIGN KEYS. If you look at my image, you will see, that the MainTable table has only 3 fields and those 3 fields are foreign keys. This is possible, but when we design, we usually add other fields (not related to other tables) to the table. For example, lets say that:
Table1 ==> Car
Table2 ==> Driver
Table3 ==> City
MainTable ==> DriverWhoDrivesACarInACity (ridiculous name but you get the point)
With image in mind, you see that in the MainTable, we are actually storing information about which Driver drove a certain Car in which City (dont pay attention to the real life meaning of this, because it is not relevant at this point). Everything looks good. You start your application and you start inserting data. And then some day it happens, that a certain Driver wants to drive a certain Car in a certain City that he already drove in. That means you will have a duplicate row in the table MainTable. And at this point, you see that there could be some problems with this design. And after some thought, you decide to add another field to your MainTable table: you add "Date and time of usage" so that you know when the car was driven.
So now you have something like this:
Please note that you dont "need" foreign key relationships, it is just a good practice in order to sustain database integrity!
Here is a sample for SQL server...try accordingly.
SELECT p.Name, v.Name
FROM Production.Product p
JOIN Purchasing.ProductVendor pv ON p.ProductID = pv.ProductID
JOIN Purchasing.Vendor v ON pv.BusinessEntityID = v.BusinessEntityID
WHERE ProductSubcategoryID = 15
ORDER BY v.Name;
select f1.*,f2.*,f3.* from f1 a
join f2 b on a.1=b.1
join f3 c on a.1=c.1

Is it possible to query two access tables where you want to know if a value/range in the first table is between two fields in the second table?

I'm trying to query two tables, ASSAYS, AND LITHO in a diamond drillhole database.
I was given values (SAMPLE_NO) to search for in the ASSAYS table, to return values such as HOLE-ID, FROM, and TO. So each sample that we take has a HOLE-ID, SAMPLE_NO, FROM AND TO. One hole-id can have multiple sample numbers, but each sample number is unique. The from and to will be unique in each hole-id. This I can find no problem.
My coworker also wanted to know what rock type was associated with each sample. This info is located in another table so I'll need to figure out how to query for this. The information that this table holds is HOLE-ID, FROM, TO, and ROCKTYPE.
You're looking for what is called a JOIN. This allows you to JOIN data of multiple tables based on matiching column values.
This could be your starting point:
SELECT a.*, l.*
FROM ASSAYS a LEFT JOIN LITHO l ON a.hole-id = l.hole-id
WHERE a.sample_no = 'XXXX'
Please google for JOIN and SQL to find out about the exact syntax.

Resources