Using Indexes in mysql correctly - php

I would like to know that how can I make a proper Indexed table, to understand this concept I will be using movies as example:
I have these 5 tables with multiple fields but I will list here these tables primary indexes fields only:
movies
movie_id = primary index
actors
actor_id = primary index
geners
gener_id = primary index
reviews
review_id = primary index
And then I have these tables for relation with 2 columns each, I am unsure what type of indexes should have these relational tables:
movie_actor
movie_id,actor_id
movie_gener
movie_id,gener_id
movie_review
movie_id,review_id
I have join on these fields If I wants to get one movie details I will use such query:
SELECT *
FROM movies as m
LEFT JOIN movie_actor AS ma ON ma.movie_id = m.movie_id
LEFT JOIN actors AS a ON a.ator_id = ma.actor_id
LEFT JOIN movie_gener AS mg ON mg.movie_id = m.movie_id
LEFT JOIN geners AS g ON g.gener_id = mg.gener_id
LEFT JOIN movie_review AS mr ON mr.movie_id = m.movie_id
LEFT JOIN reviews AS r ON r.review_id = mr.review_id
WHERE m.movie_id = 1234
So what kind of index should I use on relational tables (movie_actor,movie_gener,movie_review) both fields, primary on which one or just index on both?
Thanks

Only one field can be the primary key on each table Your table can only have 1 primary key (corrected by user). Look at this post for information Differences between INDEX, PRIMARY, UNIQUE, FULLTEXT in MySQL?

Related

I try to get information from 4 different tables of the same database

In order to create a list of data, I try to get information from 4 different tables of the same database, using php and only one sql statement. The tables are linked together as follows with foreing keys:
P and B have their primary keys as foreing keys in D.
T has its primary key as foreing key in B
It's easy with 3 diferent sql statements,
{
$sql1="SELECT * FROM daneismos WHERE danDateEpistr IS NULL ORDER BY danDateDaneis";
$sql2="SELECT * FROM titlos WHERE titlosId IN
(SELECT bookTitle FROM book WHERE bookID = '".$recDaneismos['danBookId']."')";
$sql3="SELECT * FROM person WHERE personId = '".$recDaneismos['danPersonId']."'";
}
but too complicated for me using just one.
I guess this could help you:
SELECT p.X, d.Y, b.Z, t.F
FROM person p
JOIN daneismos d ON p.personId = d.danPersonId
JOIN book b ON d.danBookId = b.bookID
JOIN titlos t ON t.titlosId = b.bookTitle
WHERE b.bookId = '$danBookId'
AND personId = 'danPersonId';
Even you can put where conditions on join methods.
Btw, be careful with this multi join statements because could make your database slower

How use two Foreign Keys inside Single query?

I have 2 tables on database,'locations' & 'sales_person'.inside the 'location' table there are two columns name "location_from" & "location_to".Those 2 tables include primary keys of sales_person table as Foreign Keys.
the problem is, how to get both sales person names from sales_person table using only one query?
Join sales_person table twice with location to get names for location_to and location_from
select t.name,f.name
from location l
join sales_person t on l.location_to = t.id
join sales_person f on l.location_from = f.id
I assume you have name column in your sales_person table
You can also use union for single query.
select lt.name from location l join sales_person as lt on l.location_to = lt.id
UNION ALL
select lf.name from location l join sales_person as lf on l.location_from = lf.id

PHP Join Query for Three Table When There is No Direct Common Column?

I have Three Tables in which first two tables have common column to match the record, and 2nd and third table also have common column but there is no direct matching column in first and third table. How shall I write join query ?
Table1(order) Column Names are order_id, patient_id, total, discount
Table2 (order_details) Column Names are order_details_id, order_id, test_id, result
Table3(tests) Column Names are test_id, test_name, test_normal_value
I hope it helps
SELECT * FROM `order`
LEFT JOIN `order_details` ON `order`.`order_id` = `order_details`.`order_id`
LEFT JOIN `tests` ON `order_details`.`test_id` = `test`.`test_id`
"SELECT a.patient_id FROM table1 as a
LEFT JOIN table2 as b on a.order_id = b.order_id
LEFT JOIN table3 as c on c.test_id = b.test_id";
to join this 3 table in one query, we need to assign each table a name. For example table1 AS NewNameTable1 LEFT JOIN table2 AS NewNameTable2. To connecting between this 3 table we need to have a foreign key for each table. So that this 3 table able to JOIN and we able to fetch the data in single query. As long this 3 table is JOINED via foreign key, you can echo out any attribute from any table that Joined.

mysql join when field are table name and primary key

I have to build a complex query on SugarCRMfor a report. SugarCRM relationship it's stored on some tables, so I need to join multiple tables using the relationship tables. The tables have a model and model_id which identify a table and record primary id.
Below is an example of what I am trying to do.
BLOGS : Main table with field ID
BLOG_ENTRY : Child table of Blog with with field ID, BLOG_ID (FK of BLOGS table) and POST_CONTENT_ID (FK of next table BLOG_CONTENT)
BLOG_CONTENT with field ID, MODEL (which identify a child table) and MODEL_ID (which identify child table id)
BLOG_CONTENT_TABLE_ONE with field id
Suppose we can have more than one table BLOG_CONTENT_TABLE_TWO, ...
The query should be something like:
select blogs.id, blog_content.model, blog_content.model_id, blog_content_table_one.id
from blogs, blog_entry, blog_content, blog_content_table_one
where
# join blog and blog_entry
blog.id = blog_entry.blog_id
# join blog_entry and blog_content
AND blog_entry.content_id = blog_content.id
# join blog_content and blog_content_table_one
AND blog_content.model = 'blog_content_table_one'
AND blog_content.model_id = blog_content_table_one.id
This works, but if there is more table, I suppose I should use a MySQL case ?
First, use the ON clause for JOINs. Second, I don't think it's possible to get all possible combinations, but for three blog content tables, you could go for something like this.
SELECT
blogs.id,
blog_content.model,
blog_content.model_id,
blog_content_table_one.id
blog_content_table_two.id
blog_content_table_three.id
FROM blogs
JOIN blog_entry
ON blog.id = blog_entry.blog_id
JOIN blog_content
ON blog_entry.content_id = blog_content.id
LEFT JOIN $blogtable1 blog_content_table_one
ON blog_content.model_id = blog_content_table_one.id
AND blog_content.model = '$blogtable1'
LEFT JOIN $blogtable2 blog_content_table_two
ON blog_content.model_id = blog_content_table_two.id
AND blog_content.model = '$blogtable2'
LEFT JOIN $blogtable3 blog_content_table_three
ON blog_content.model_id = blog_content_table_three.id
AND blog_content.model = '$blogtable3'
WHERE blog_content.model IN ('$blogtable1', '$blogtable2', '$blogtable3')

mySQL Return field names from multiple foreign keys

I'm new to mySQL and was hoping to get some help. I have a table layed out in the following way:
TASK
task_id *
tasktype_id (FK)
proj_id (FK)
empl_id (FK)
taskstat_id (FK)
hrs
notes
If I do a query like this, the results print out fine but the numeric references to foreign keys are printed.
SELECT * AS task FROM task
INNER JOIN tasktype ON tasktype.tasktype_id = task.tasktype_id
WHERE taskstat_id = 1";
But I would like to print out the field names instead of field numbers for the foreign keys in the resulting table. Any ideas? This is what I tried, but without any luck.
SELECT task.task_id,
tasktype.tasktype_id,
project.proj_id,
employee.empl_id,
taskstatus.taskstat_id,
task.hrs,
task.note
FROM task, tasktype, project, employee, taskstatus
INNER JOIN tasktype ON tasktype.tasktype_id = task.tasktype_id
WHERE task.taskstat_id = 1";
well, assuming the "field name" in the related tables (column name) is called... name (I don't know the structure of your joined tables)
select t.task_id,
tt.name, //or tt.description, or whatever
p.name, //same
e.name, //same
ts.name, //same
t.hrs,
t.note
FROM task t
inner join tasktype tt on tt.tasktype_id = t.tasktype_id
inner join project p on p.proj_id = t.proj_id
inner join employee e on e.empl_id = t.empl_id
inner join taskstatus ts on ts.taskstat_id = t.taskstat_id
where t.taskstat_id = 1

Categories