Assume your connection from your web server to your DBA server is extremely slow, and you need to do a series of three queries to handle a single web request. Is there a way to combine the queries into one? Assume the following scenario
person.id person.name
1 James
2 Stacy
.
country.id country.name
1 USA
2 UK
.
location.person_id location.country_id
1 1
1 2
The web form would post two variables, ie name=”James” country=”China” and you want to do the following
- Does “James” exist, if not, insert james
- Does “China” exist, if not, insert china
- Does “James” live in “China”, if not insert relationship
ie something like
select person.id, country.id, location.person_id
from person, country, location
where
person.name="James" and
country.name="China" and
person.id=location.id and country.id=location.country_id
The above query is not useful because it will return no records if either the person, or the country or the location does not exist.
I know that it would be possible to do this using a stored procedure but not all databases support stored procedures.
The solution is very simple