very basic SQLite table design - sqlite

I don't understand the fundamental set-up of tables in SQLite.
Let's say I have three tables:
Studio Table
studioID studioName
-------- --------
1 Lucasfilm
2 Amblin Entertainment
3 Zoetrope Studios
Movie Table
movieID movieName studioID actorID
-------- -------- -------- --------
1 Star Wars 1 1
2 Indiana Jones 1 1
3 Back to the Future 2 3
4 Apocalypse Now 3 2
Actor Table
actorID lastName firstName
-------- -------- --------
1 Ford Harrison
2 Fishburne Laurence
3 Lloyd Christopher
This structure doesn't allow for the fact that Harrison Ford and Laurence Fishburne were both in Apocalypse Now. What would be a conventional way to represent Ford and Fishburne acting in the same movie using similar tables? Sorry for the newbie question.

What would be a conventional way to represent both being in the same movie using similar tables?
There is none.
You need another table: cast
cast table
---------
movieID
actorID
Example data:
movieID | actorID
4 | 1
4 | 2
And maybe there should even be another column in the cast table: roleName. The same actor could have multiple roles in one movie.

Studio 1 : N Movies -> One table for Studios, one for movies and add a FK from studio in movies.
Movies N : M Actors -> One table for actors, and another that links Movies and actors.

Related

Join on LINQ relative to check list boxes

I have 3 tables.
person
equipment
relation between person and equipment
I have a page with a search button and a checklistbox that binds with the equipment. I want the user to be able to select one or more check list and then show people associated with the selected equipment.
-------------------------
table1
p_famil p_code
ali 1
reza 2
mohamad 3
kazem 4
---------------------------------
table 2
e_code e_title
1 home
2 car
3 job
--------------------------------
table 3
er_eq_code er_per_code
2 1
3 1
1 2
1 4

Phonegap + SQLite Joining

I have two tables customer & home in a local database for a ipad application.
I have a SQL query
SELECT C.CustomerID AS 'CID', H.CustomerID AS 'HID', H.lotNumber AS 'lotNumber' FROM CustomerInformation C LEFT JOIN HomeInformation AS H ON ( H.CustomerID = C.CustomerID )
It returns the proper amount of records, and display my address data correctly. However, all the data in the HomeInformation table is null
CustomerID FirstName HomeID CusomterID lotNumber
1 Josh 1 1 73824
2 Smith 2 2 54353
3 Chris 3 3 75342
4 Bob 4 4 42342
5 John 5 5 24342
I have tried to use INNER join, Cross Join, and Outer Join, but no records are returned.

simple SQLite query

How would I query these 4 tables to see actors who are in Apocalypse Now?
Studio Table
studioID studioName
-------- --------
1 Lucasfilm
2 Amblin Entertainment
3 Zoetrope Studios
Movie Table
movieID movieName studioID castID
-------- -------- -------- --------
1 Star Wars 1 1
2 Indiana Jones 1 2
3 Back to the Future 2 3
4 Apocalypse Now 3 4
Cast Table
castID movieID actorID roleName
-------- -------- -------- --------
1 1 1 Han Solo
2 2 1 Indiana Jones
3 3 3 Doc Brown
4 4 1 Colonel Lucas
5 4 2 Tyrone Miller
Actor Table
actorID lastName firstName
-------- -------- --------
1 Ford Harrison
2 Fishburne Laurence
3 Lloyd Christopher
Right now I'm thinking:
SELECT Studio.studioName, Movie.movieName, Cast.roleName, Actor.firstName, Actor.lastname
FROM Studio, Movie, Actor, Cast
WHERE Cast.castID = Movie.castID and Movie.movieName = 'Apocalypse Now'
ORDER BY Actor.lastname
Not really sure how to join the tables.
=.=.=.=.=.=.=.=.=
Try this..
SELECT a.*
FROM Movie m
LEFT JOIN Cast c on (c.movieID = m.movieID)
LEFT JOIN Actor a on (a.actorID = c.actorID)
WHERE m.movieName='Apocalypse Now';
if you need also a Studio join the studio table:
SELECT a.actorID, a.firstName, a.lastName, s.studioName
FROM Movie m
LEFT JOIN Cast c on (c.movieID = m.movieID)
LEFT JOIN Actor a on (a.actorID = c.actorID)
LEFT JOIN Studio s on (m.studioID = s.studioID)
WHERE m.movieName='Apocalypse Now';

datasets and joins. ASP.net

I have 3 tables. Namely Job_Master,Print_details,Paper_Details.
The Structure of the table is as follows:
Job_Master:
jobno jobname amount
1 A 100
2 B 200
3 C 300
Print_Details id being the PK and jobno being FK
id jobno color
1 1 Cyan
2 1 Red
3 2 Black
4 3 Black
5 3 Green
Paper Details id being the PK and jobno being FK
id jobno Type
1 1 Art Paper
2 1 Photo Paper
3 2 Art Paper
4 3 Copier
5 3 Glossy Paper
I want a write a query in SQL server or perform Dataset operations in ASP.net so as to display the below resultset in the grid view:
Desired Resultset:
jobno jobname printDetails paperDetails amount
1 A CYAN,RED Art Paper,Photo Paper 100
2 B Black Art Paper 200
3 C Black,Green Copier,Glossy Paper 300
is this possible using dataset operations in ASP.net or in a SQL server query. I am using SQL server 2008 R2 as my database and the fromt end has been designed using ASP.net3.5.
Any help on this is much appreciated.Thanks in advance.
Try this
SELECT DISTINCT a.Jobno, a.jobname, COALESCE(b.color + ', ', '') + b.color,COALESCE(c.type + ', ', '') + c.type
FROM job_master a left JOIN cprint_details b ON a.jobno=b.jobno
left join paper_details c on a.jobno=c.jobno
ORDER BY a.jobno
Read here for more info LINK

Bill of Materials Query

I currently have 2 tables as follows within my database:
Table: SampleProducts
SampleProductsId (PK) Name
1 A
2 B
3 C
4 D
5 E
6 F
7 G
Table: SampleProductsBoms
SampleProductsBomId (PK) ParentId (FK) ChildId (FK) Quantity
1 1 2 3
2 2 3 4
3 4 6 2
ParentId and ChildId both reference SampleProductsId
In English so I can ensure that we are all on the same page:
Product A is made up of 3 of B
Product B is made up of 4 of C
Product D is made up of 2 of F
I would like to create a Stored Procedure / LinQ statement or something which I can use in my MVC 3 c# Web Application which will give me the following table structure / object to use...
Example:
Recursive Query to find the components of B
ProductId Name Quantity
3 C 4
6 F 2
This could go quite deep, so I really do need recursion!
CTE is helpfull for recursing as require in your problem statement check the link
Common Table Expression
or i think following query may also solve your purpose
select components.SampleProductId as productid,components.Name as Name,Quantity
from SampleProductsBOM bom
inner join SampleProducts products
on products.ParentId=bom.ParentId
inner join SampleProducts components
on components.SampleProductId=bom.ChildId
where products.Name='B'

Resources