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

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.

Related

Multiple foreign key relationship to one table

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.

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

Find relationship between any table A and B in Sql Server

Suppose we have the following tables, with pretty foreign keys in place.
CREATE TABLE Person
(
Id int not null
--other stuff
)
CREATE TABLE Employee
(
Id int not null,
PersonId int not null,
UserId int null
--other stuff
)
CREATE TABLE User
(
Id int not null,
Name varchar(25) not null,
Password varchar(25) not null
--other stuff
)
CREATE TABLE Roles
(
Id int not null
--other stuff
)
CREATE TABLE UserRoles
(
UserId int not null,
RoleId int not null
--other stuff
)
What are the ways(query, additional software?) to ask
"What is the relationship between table X and Y?"
E.g. I would like to 'ask' :
What is the relationship between tables Person and Roles?
Expected answer :
Person 1:N Employee 1:1 User 1:N UserRoles N:1 Roles
Note that tables Person and Roles do not have a direct relationship. The expected result should list the tables in-between of these two.
Something like this. A diagram representation would do, but it should only have the tables involved in the relationship.
Why I can't use "Database Diagrams" in SSMS.
Creating a relevant diagram with only needed tables takes too much time looking up the references by hand.
I can't use "Add Related tables" because it makes the diagram absolutely unreadable by adding 200+ tables.
The difference from diagramming would be that I only want to input two table names.
I believe this might be what you are looking for:
select t.name as TableWithForeignKey,
c.name as ForeignKeyColumn,
t2.name as DependentOnTable,
c2.name as ReferencedColumn,
N'N:1'
from sys.foreign_key_columns as fk
inner join sys.tables as t
on fk.parent_object_id = t.object_id
inner join sys.columns as c
on fk.parent_object_id = c.object_id
and fk.parent_column_id = c.column_id
inner join sys.columns as c2
on c2.object_id = fk.referenced_object_id
and c2.column_id = fk.referenced_column_id
inner join sys.tables as t2
on t2.object_id = c2.object_id
order by TableWithForeignKey
Note that all relationships in SQL server are 1:N because
neither a 1:1 can be established: How do I create a real one-to-one relationship in SQL Server
nor a N:N relationship can be established: Foreign Key to non-primary key
If you want to setup such relationships then you can merely use the extended properties to write it down for yourself and then "manually" enforce it.

SQL Update One Table with Value from Another Table

I have the following SQL statement -
SELECT e.*, i.CaseNo, c.Claimant, c.Defendant, c.ClientID
FROM tblExpenses AS e
INNER JOIN tblInvoices AS i ON e.InvNo = i.InvNo
INNER JOIN tblCases AS c ON i.CaseNo = c.CaseNo
which demonstrates the link between three tables.
tblInvoices has a primary key [InvNo] which is also a foreign key in tblExpenses thus linking each expense with the relevant invoice.
tblCases has a primary key [CaseNo] which is also a foreign key in tblInvoices, thereby linking each invoice to a particular case.
Finally each case in tblCases has a column called [ClientID] which identifies the client whom the invoice was sent to.
Now then, tblExpenses also has a foreign key [ClientID] but at present the column is not populated. What I want to do, is use the above links to populate [ClientID] in tblExpenses, with the [ClientID] from tblCases, via the aforementioned links.
However I am unclear how to rewrite my SELECT query in order to carry out this population of the [ClientID] column in tblExpenses.
Can anyone please assist?
You already have the sufficient select query to get the required information. You just need to convert it to an update query.
UPDATE e SET e.ClientID=c.ClientID
FROM tblExpenses AS e
INNER JOIN tblInvoices AS i ON e.InvNo = i.InvNo
INNER JOIN tblCases AS c ON i.CaseNo = c.CaseNo

How can I relate a ID to an actual name in a Database

I have a database but one part of it that has baffled me is how I can get it to relate a supplier ID to the name of the supplier.
So for example I want to add a product through a form but at the moment when I come to select supplier ID it will only show the ID number (5, for example), rather than the name of the supplier the ID relates to
I suspect what you need to do is join the supplier table with the product table. Assuming the product table has a supplier_id column, something like this:
(EDIT based on first comment)
SELECT p.description, s.supplierId, s.supplierName
FROM product p
INNER JOIN supplier s ON s.supplierId = p.supplierId
WHERE p.id = some_value;
If you post the table descriptions, like a SHOW CREATE TABLE table_name, this could be refined and made more useful for you.

Resources