Suitable design for a database application - php

I have a question related to a web app that I developed in PHP, MYSQL.
basically part 1 is :
I display results in the form of table say for software testing.
ID Prod_Name Set Date Result Platform
1 Alpha1 Pro1 01.01.01 PASS 2.3.1.2_OS
Now, I have divided the tables accordingly
Table Name: Results
ID, Name, Date, Result
Table Name : Set
ID, Set_Name, Prod_name
Table Name : Platform
ID, Platform_Name, Set_Name
Now, ID in each table is an incremented value and does not relate to anything else.
My php app, starts with fetching the results from 'Results' table. Since I want SET to be displayed for every row, I am making an another connection to the database and using the query
select Set_name
from Set
where Prod_name = row['Name'] // row['Name'] is fetched from the results table.
now I also want to display platform which I am extracting it from Platform table using the above method i.e making another connection and passing Set_Name = row['Set_Name'] from the Set table.
Now for my application is there any other way to achieve the same result ?
Typically, for large web based applications, if data is coming from a database server is making multiple connection to a DB server a feasible option?
Please do not consider the fact that with MySQL declaring a connection statement once will do the needful but what about MSSQL server? Do we need to write a long sql statement with several joins/selfjoins/unions and use those variables all over the application?
How is the application design for this case will be?
Can anyonce give me some ideas please?
Thanks.

For pretty much any flavour of database, a single SELECT statement which joins three tables will perform better than three separate statements querying a table apiece. Joining is what relational databases do.

I may not have understood everything, but here is something similar. First, let's make an ER model.
Now, because you don't seem to like joins, create a view in the database.
CREATE VIEW v_test AS
SELECT TestID, ProductName, TestName, Date, Result, PlatformName
FROM Product AS p
JOIN Test AS t ON t.ProductID = p.ProductID
JOIN Platform AS f ON f.PlatformID = t.PlatformID;
With this in place, you can simply use:
SELECT * FROM v_test WHERE ProductName = 'Alpha1'
You may also take a look at this question/answer with a similar scenario.

Related

Is there a dynamic setting in MySQL to implicitly limit all queries in all tables to only records with the YearID set to this year?

I'm using PHP to access a MySql database with ~50 tables. Almost every table has a field for "YearID". My app will be starting its second year soon and I'll just keep adding records to the tables with the new YearID. Currently, my queries do not include YearID as a constraint because there is only a single year's data stored (and I wasn't thinking far enough ahead!).
I want users to be able to access previous years' data. One way to do this, obviously, is to update all my queries throughout the site to include the YearID.
Before I do that, I was wondering if there is some dynamic setting in MySql that would implicitly limit all queries to all tables to only records with a particular YearID.
For example, one query now looks like this:
SELECT distinct RubricPK, tas.DisplayOrder, s.IDENTIFIER
FROM Rubric r
JOIN TeacherAssessedStandards2 tas ON tas.STANDARDID=r.EL_StandardID AND tas.SLorEL="EL" AND
tas.SectionDCID = ?
JOIN Standards s ON s.StandardID = tas.ParentStandardID
ORDER BY tas.DisplayOrder, s.IDENTIFIER
There is no YearID included in this query.
Is there a MySQL setting I can change with PHP so that the query above will only pull from records in each table where YearID=2020?
Or should I get to work editing all my queries to include a YearID constraint?
There is no way to achieve that directly via a setting. What you can do is write a few lines of $scripting_language to find all the tables via the information_schema and create a VIEW based on each one in a separate database. For example:
CREATE VIEW DB2020.table_name AS SELECT * FROM MainDB.table_name WHERE YearID = '2020';
Then give your users SELECT access only to DB2020 database.

MySQL use view to access table under different name

I have a performance and best-practice question concerning mysql tables.
I´m working on an application which connects to a database which gets filled by other programms.
This system is deployed in differnet locations, and from location to location the name of some databases-tables can change (but the fields in this tables etc stay the same).
As I don´t want to change all sql querys in my application for every location, I thought about creating a mysql view which simply mirrors the contents of this table to the normaly used table-name.
Is this a suitable solution, or could it get awfully slow with big tables?
Simple views (created as SELECT * FROM table) behave like the specified table performance wise.
It should be a suitable solution for your case.
mmm, this is tricky. If there are multiple tables then a quick and dirty version for this would be something like
SELECT * FROM (SELECT * FROM table1
Union
SELECT * FROM table2
Union
SELECT * FROM table3) t
Which I think will work. You will of course have problems with pagination, sorting and searching - because you will have to try and do this over 3 or more tables.
Another way would be this
Create a table with the table names and a counter
ImportTable
name
id
Now in this you can enter the names of the tables and the last id that you want to import from.
Create another table to import the records
TableRecords
source
id
field1
field2
etc
Now run something that goes through the tables in ImportTable grabs any new records and shoves them into `TableRecords.
Now this becomes really simply you can query TableRecords and have pagination sorting and searching with no of the previous troubles.
Make something that runs this every 2 minutes say so TableRecords will be 2 mins behind but everything will be really easy and run like a dream.

Is it possible to join two query result with PHP code?

I need to JOIN 2 tables (lets say User & Order table) for reporting module in my web app.
The problems are:
The User table is located on the different server & different
DBMS from the Order table. Technically it is a different system, so the User table is located on SQL Server DB, meanwhile the Order table is located on MySQL DB.
I couldn't use SQL Server's Linked Server because my company policy doesn't allow it. So, I coudn't JOIN them directly with SQL code. They want me to use Web Service instead of linked server.
The result of JOIN operation from those tables has a large number of rows (maybe more than 10,000 rows because the data aimed for reporting). So, I think it was a horrible thing to mapping them using Web Service.
So I came up with this:
I collected 2 query result from different models and join them with my app code (I'm using PHP with CodeIgniter) :
// First result
$userData = $this->userModel->getAllUser();
// Second result
$orderData = $this->orderModel->getAllOrder();
The $userData contains all user entities with the following columns:
[UserId, Username, Address, PhoneNumber, etc..]
And the $orderData contains all order entities with the following columns:
[OrderId, UserId, Date, etc..]
But is it possible to join those two query results in PHP / CodeIgniter?
How about the performance regarding the large amount of data?
Should I just use Web Service as suggested or there's another solution to accomplish this?
Thanks in advance :)
A few things to think about:
Do you actually need to return all user and order records in one single go
Do you actually want to return all rows for these two types of record
Would you be better off with a Report module for these report queries?
Would plain SQL syntax be a smarter move than trying to shim this into existence with the CodeIgniter "Active Record" (renamed Query Builder in 3.0)
Is JOIN really so bad? It is not a UNION, you want the data to be related.
I would recommend you limit your data returns, SELECT only the fields you actually require, make a new Report model to avoid trying to mess up your generic models and do this with raw SQL.
Complicated things get all the more complicated when you try too hard to stick to rules like "1 table = 1 model" and "User::getAllFoos + controller processing > Report::getMonthlyOrderStats()".

Mysql INNER JOIN and Resource #9

I am working on a UCP(User control panel) where a user loggs in by an unique ID that is given at registration.
My problem is i have two tables in my database:
serverplayers - Holds user ID and name and all of his info
and
vehicles - holds vehicle owner and vehicle model.
So what I need to do is connect those two together by users "Username".I tried Mysql INNER JOIN like this.
$car = mysql_query('SELECT vehicles.vOwner, vehicles.vModel
FROM vehicles
INNER JOIN serverplayers ON vehicles.vOwner = serverplayers.User
WHERE vehicles.vOwner = '.$ro['User'].'');
After this I get Recourse #9, i read that it's not an error it's just that the result is empty?I may have error's or problems in my mysql query so please tell everything whats wrong.
But what's the problem I think is that a vehicle is only connected by it's owner's name and doesn't contain UserID.So what I need is basically show a User what car he has by Connecting his username with vOwner.I'm not a very bright boy when it comes to mysql or php I'm just learning and I came across this thing as mysql INNER JOINS that I think is very usefull(but the main reason is that I don't want to recode the whole server and the UCP).
Vehicles:
ServerPlayers:
ServerPlayers table http://img94.imageshack.us/img94/3369/qebq.png
I'm not sure what $ro['User'] is, but it sounds like an ID based on your question. In that case I would just switch vehicles.vOwner to serverplayers.UserID in that comparison. I would also highly recommend changing your schema so vOwner is the ID and not the username (what if the username changes in one of the tables?) It also doesn't look like you need to join on serverplayers for anything.
The "resource" is a mysql results resource. You can't get anything out of it just by looking at it -- you need to run fetch operations on it:
$row = mysql_fetch_assoc($car);
I recommend using PDO or mysqli over the deprecated ext/mysql.

Getting information from different tables [Best Practice]

I have a MySQL database that I have to get information from, said information is spread across different tables and I have been Googling for a while for the best method to get this information and found quite some information, but I was wondering if there is a best practice that I should try, since the tables may get quite big later on I would like to have a good start regarding functionality, and speed.
If your records in your database are related through ID's or primary keys, you can use the JOIN syntax to get data from multiple tables through 1 query.
Example :
Table car : id, brand
Table driver : id, name, car_id
You can get all the drivers of a car in 1 query:
SELECT * FROM driver LEFT JOIN car ON (car.id = driver.car_id) WHERE car.id=5;
This is just a basic example, but read the MySql documentation (or tutorials) to go on.

Categories