Multiple foreign key relationship to one table - sql-server

How do you make a relationship between multiple foreign key values from one table to another table
First Table:
Item1a
Item1b
Item1c
Second Table:
ItemID
Description
Whereas Item1a, Item1b and Item1c gets their source from the second table: ItemID. Connecting them all in the relationship diagram doesn't change anything, how do I build their connection?
Thanks

You need three different JOIN for the second table.
SELECT <columns>
FROM FirstTable f
<LEFT/INNER> JOIN SecondTable s1 ON s.ItemID = Item1a
<LEFT/INNER> JOIN SecondTable s2 ON s.ItemID = Item1b
<LEFT/INNER> JOIN SecondTable s3 ON s.ItemID = Item1c
Hope this help.

Related

SQLite query with 3 entities in Hibernate 4 (nested join?)

I have the following tables in my database (SQLite):
CREATE TABLE product (
id INTEGER NOT NULL,
name TEXT,
PRIMARY KEY (id)
)
CREATE TABLE feedback(
product INTEGER PRIMARY KEY,
score NUMERIC,
textnote VARCHAR(255),
FOREIGN KEY(product) REFERENCES product(id)
)
CREATE TABLE product_detail (
id INTEGER NOT NULL,
product INTEGER,
description TEXT,
url VARCHAR,
PRIMARY KEY (id),
FOREIGN KEY(product) REFERENCES product (id),
)
So feedback is in a one-to-one relationship with product and product_detail is in a many-to-one relationship with product.
I am trying to write a SQLite query for Hibernate 4 that will give me all the attributes from the 3 tables in one single table. All I came with by far is this:
String hql = "SELECT p.id, p.name, d.id, d.description, d.url " +
"FROM ProductDetail d INNER JOIN d.product AS p";
I'm correctly obtaining the values I want from product and product_detail. How can I add to the query also the information in feedback? I know all products have at least one product detail, but I can't say the same about feedback: most of the products don't have a feedback. I think I should use a LEFT JOIN but I can't figure out how...
Note that as I'm using Hibernate I have all classes relative to the tables in my application (Feedback, ProductDetail and Product).
You must also join feedback:
SELECT
p.id, p.name,
f.score, f.textxnote,
d.id, d.description, d.url
FROM product p
INNER JOIN feedback f ON f.product = p.id
INNER JOIN ProductDetail d ON d.product = p.id
You may change to LEFT the joins if there is a case that a product does not have a row in either of the tables feedback and ProductDetail.

I want to insert data from different tables in observation table

OBSERVATION table-
O_ID, NAME ,OL_ID ,BP_ID ,OC_ID and OL_ID ,BP_ID ,OC_ID these three column contain FK.
OBSERVATION_CATEGORY table- OC_ID NAME
BUSINESS_PROCESS table- BP_ID NAME
OPERATING_LOCATION_1 table- ID NAME
I have 4 other tables in which ID column is there which has to inserted in observation table. In short I have to fetch data from 4 tables into one table.
FIRST CREATE TABLE AND CREATE SQL QUERY LIKE below:-
INSERT INTO table5
SELECT O_ID,OC_ID, oc.NAME,BP_ID bp.NAME,ol.ID,ol.NAME
FROM table1 ob INNER JOIN table2 oc ON ob.O_ID=oc.OC_ID
INNER JOIN table3 bp ON ob.O_ID=bp.BP_ID
INNER JOIN table4 ol ON ob.O_ID=ol.ID;
How ever you have NOT mention your DATABASE nor your exact problem.

I want to select from two tables (a,b) which are related to each other through third table(c), but table a has a composite primary key

I want to select from two tables (customers,machins) which are related to each other through third table(orders), but table a has a composite primary key
Table customers has primary key as
id
Table machins has a composite primary key with these two columns:
machinId
machineModel
And these two are related to table orders
Id(fk)
machine(fk)
machintype
I want to select from customers and machins all machines that a person has bought (orders)
My select command is
Select c.name, c.land, m.namemachin
from orders o
inner join customers c on c.id = o.id
inner join machins m on o.machine = m.Id
where c.name = 'karl' and m.machine = o.machintype
But it doesn't work
Can you please help me?
If you have a composite primary key in table machins then both of that column should be available in table orders for a proper relation. Then only you can do a proper join this tables for a select query.
As per the discussion m.machineModel = o.machintype
So please try this query
Select c.name, c.land, m.namemachin
from orders o
inner join customers c on c.id = o.id
inner join machins m on m.machinId = o.machine
and m.machineModel = o.machintype
where c.name = 'karl'
If your fetching is something related to string concatenation i.e to obtain all machines as string. I think you can use STUFF and FOR XML PATH.
Or else go through th below link it might help
http://www.w3resource.com/sql/joins/using-a-where-cluase-to-join-two-tables-related-by-a-composite-primary-key-or-foriegn-key-pair.php

How to restore SQL data from one table to another

I have a backup table with 15,000 rows. The data was directly copied from a main table. I need to perform an UPDATE statement on the main table, using the data from the backup table. Both tables are identical in structure and use a unique primary key called ID.
How do I loop through the backup table, find the matching record for each row in the main table, and then replace each record in main table with the matching record in the backup table?
Thanks
EDIT:
Proposed Solution based on Sean Lange's comment:
UPDATE a
SET a.col1 = b.col1
, a.col2 = b.col2
FROM table1 a
INNER JOIN table2 b
ON a.id = b.id
You can use a JOIN update to accomplish this. Assuming they have matching primary key ID's, it should be quick and painless.
UPDATE table1
SET a.col1 = b.col2
, a.col2 = b.col2
FROM table1 a
INNER JOIN table2 b
ON a.id = b.id

How can I "subtract" one table from another?

I have a master table A, with ~9 million rows. Another table B (same structure) has ~28K rows from table A. What would be the best way to remove all contents of B from table A?
The combination of all columns (~10) are unique. Nothing more in the form a of a unique key.
If you have sufficient rights you can create a new table and rename that one to A. To create the new table you can use the following script:
CREATE TABLE TEMP_A AS
SELECT *
FROM A
MINUS
SELECT *
FROM B
This should perform pretty good.
DELETE FROM TableA WHERE ID IN(SELECT ID FROM TableB)
Should work. Might take a while though.
one way, just list out all the columns
delete table a
where exists (select 1 from table b where b.Col1= a.Col1
AND b.Col2= a.Col2
AND b.Col3= a.Col3
AND b.Col4= a.Col4)
Delete t2
from t1
inner join t2
on t1.col1 = t2.col1
and t1.col2 = t2.col2
and t1.col3 = t2.col3
and t1.col4 = t2.col4
and t1.col5 = t2.col5
and t1.col6 = t2.col6
and t1.col7 = t2.col7
and t1.col8 = t2.col8
and t1.col9 = t2.col9
and t1.col10 = t2.col0
This is likely to be very slow as you would have to have every col indexed which is highly unlikely in an environment when a table this size has no primary key, so do it during off peak. What possessed you to have a table with 9 million records and no primary key?
If this is something you'll have to do on a regular basis, the first choice should be to try to improve the database design (looking for primary keys, trying to get the "join" condition to be on as few columns as possible).
If that is not possible, the distinct second option is to figure out the "selectivity" of each of the columns (i.e. how many "different" values does each column have, 'name' would be more selective than 'address country' than 'male/female').
The general type of statement I'd suggest would be like this:
Delete from tableA
where exists (select * from tableB
where tableA.colx1 = tableB.colx1
and tableA.colx2 = tableB.colx2
etc. and tableA.colx10 = tableB.colx10).
The idea is to list the columns in order of the selectivity and build an index on colx1, colx2 etc. on tableB. The exact number of columns in tableB would be a result of some trial&measure. (Offset the time for building the index on tableB with the improved time of the delete statement.)
If this is just a one time operation, I'd just pick one of the slow methods outlined above. It's probably not worth the effort to think too much about this when you can just start a statement before going home ...
Is there a key value (or values) that can be used?
something like
DELETE a
FROM tableA a
INNER JOIN tableB b
on b.id = a.id

Resources