Showing posts with label databases. Show all posts
Showing posts with label databases. Show all posts

Monday, 27 August 2012

What Is An SQL Max Query?

Websites and computing applications often store data in databases. Database systems such as MySQL and Oracle are common for both Web and desktop programs. When these programs need to retrieve data from a database, they execute query statements on it. Queries can retrieve specific data items, by referring to table and column names. The MAX function in SQL normally appears inside the select statement, which is a data query. The MAX function is particularly useful for numerical data, as it retrieves the maximum value in a column.

In this article we'll briefly go over the purpose and use of the max clause in select query statements, referring in particular to the MySQL database system.

Data

Databases for Web and desktop applications use various systems, but most of them use SQL statements. Developers build application user interfaces on top of databases, writing SQL statements to insert, update and query data. When you visit a website built using a database, the server side scripts within the site will typically execute standard query statements on the data for common tasks.

Databases store many different types of data value, including text strings and numbers. A typical database includes one or more table, with each table having a few columns. Each record in a table may store a value in each column. For example, a table to store product details could have a column representing price. Each record in the table would represent a single product, and would have a value in the price column.

When applications built on databases need to retrieve data, they use query statements, as in the following SQL example:

SELECT price FROM product;

This would retrieve the values in the price column for every record in the product table.

Max Queries

Developers can include additional conditions within query statements, tailoring the data values retrieved. For example, if an application needs to establish the product with the highest cost, it can use a MAX query.

The following code demonstrates:

SELECT MAX(price) FROM product;

This query should result in a single data item, representing the maximum value in the price column. The code uses the MAX clause as part of the statement, including the column name as a parameter to the function. The rest of the statement is the same as a standard query.

As Clause

The MAX clause also appears along with the AS clause. The following code demonstrates:

SELECT MAX(price) AS top_price FROM product;

The AS clause allows developers to assign a name to use as a reference for the resulting data value. This can be particularly useful in cases where the query is selecting a special data item such as the maximum value in a column. Depending on the database system in use, the name specified in the AS clause may appear in the application user interface.

Where Clause

SQL queries involving MAX often also involve WHERE clauses. The following statement demonstrates:

SELECT MAX(price) FROM order WHERE product_type=3;

This would return the maximum price record for a particular product type. The product type could be an additional column in the product table. Scripts for Web and desktop applications in languages such as PHP, ASP and Java can use variables to dynamically load data values into queries, such as the product type in this case.

Grouping

SQL statements sometimes use the MAX clause in statements involving grouping. The following code demonstrates a grouping query:

SELECT product_type, MAX(price) FROM product GROUP BY product_type;

This would be useful if the developer needs to retrieve the maximum price for each product type. If the query only specifies the column it needs the maximum for, it will only return a single result. Since this statement specifies two columns, one of which is enclosed in a MAX clause, it must also specify which column to group the results by, so that the maximum for each type is returned.

Links:

MySQL :: MySQL 5.0 Reference Manual :: 12.2.8 SELECT Syntax
MySQL :: MySQL 5.0 Reference Manual :: 11.15.1 GROUP BY (Aggregate) Functions

Monday, 23 July 2012

Web Development Concepts: Static Versus Dynamic

What is the difference between a static and dynamic Web page?

Essentially, whether a Web page is static or dynamic depends on what happens between the user making a request to see it, and the server sending the requested content to their browser. It makes no difference to users whether a page is static or dynamic, but is relevant to developers, as each model uses different techniques and technologies.

Essentially a static page is one whose content is stored in static, unchanging form in a document on the Web server, while the content of a dynamic page is only created when it is requested for viewing.

What happens when someone browses to a page?

When you browse to a Web page, for example by clicking a link or typing its URL into your browser address bar, your computer sends a request message to the Web server on which the requested page is hosted.

With a static page, the content is simply sitting in a file, typically in HTML, and the content of this file is returned by the server to your computer, where it is displayed in your browser window. For example, if you had a file saved as "page.html" on your server in the root directory, and browsed to it as follows:

http://exampledomain.com/page.html

The server would respond by sending the content of the file.

For dynamic pages, the process is different. Dynamic pages use programming languages such as PHP. In this case you would save your script on the server, for example using the name "page.php", and it would contain programming code as well as HTML code.

When the page is browsed to this time:

http://exampledomain.com/page.php

The code within the script file is executed (run) on the server. The server side program may do many different things, but typically one of them will be to build a Web page with HTML content and send this to the user's browser. The browser displays this HTML the same way it would have if the page had been static - the difference lies in what happens on the server, and makes no difference at the user (or "client") end.

How exactly is this different?

Static and dynamic pages don't just use different file formats, they use a fundamentally different technique. Static pages typically contain only markup code, whereas dynamic pages also contain programming code.

The content of an HTML file is basically text, images etc contained within a tree structure, with each element delineated by tags, each pair of opening and closing tags comprising an element as in the following example paragraph:

Some text...


This is HTML markup, and its structure, together with formatting such as CSS, tells the browser how the content of each element should be laid out and displayed.

With a dynamic page containing server side code, a different process takes place. The page script is executed according to its own logical structure, and as it goes it builds HTML structures and writes them out. This example PHP performs a simple calculation and writes the result out in HTML:

<?php
$answer=3*2;
echo "<p>".$answer."<p>";
?>

A typical PHP script will also do other things such as connecting to a database and querying it for data to write out as part of the HTML.

What are the advantages to using dynamic pages?

There are many advantages to using dynamic pages, the main one being the fact that they contain dynamic data. By connecting to a database, a dynamic page can potentially offer different content each time it is viewed. Static page content remains the same unless the page files themselves are manually changed, whereas dynamic pages populate their content at the time of viewing, according to what happens to be in the data store at that particular time.

It is often the case that many pages within a site will contain the same, or similar content. If this content needs to be changed, in a static site each page on which it occurs will need to be changed in turn. However, using dynamic scripting, the piece of content can be contained in a single, dedicated script, and this script can then be called from each page on which the content item appears. If the content needs to be changed, it only has to be changed in one place, i.e. the dedicated script, or the database if this is where it comes from.

Dynamic sites are therefore easier to maintain and to make changes to, while still keeping a consistent appearance and behaviour. If a site is being built for commercial purposes, being able to store the data in a database is also an advantage in itself.

Are there disadvantages to using dynamic pages?

The only potential disadvantage to using dynamic pages is the fact that a greater range of technologies, and therefore skills, are involved. A certain amount of programming expertise is necessary, and in most cases database development as well. However, if you're thinking about trying dynamic Web page development, don't be put off even if you have no programming experience, as these skills are actually relatively easy to pick up. There are lots of online tutorials and guides, so you really can teach yourself.

Do I need to use dynamic pages?


No! Using dynamic pages is not obligatory. However, depending on the purpose and nature of your site, it may be a real advantage. Setting your dynamic site up may seem an intimidating prospect, but once this is done the site will become much easier to amend and maintain. If your site only contains a small amount of simple information that does not need to be updated very often, there's probably no need to learn Web programming if it isn't something you're interested in.

How do I get started creating dynamic pages?

The first thing is to make sure you have a sound enough grasp of the basic Web technologies that are used in both static and dynamic pages, including the initial skills for making a Web page using HTML, CSS and JavaScript. You don't need to be at an advanced level, just make sure you understand the basic principles and practices.

Next is learning a server side language. I would recommend PHP, which is available on most Web hosting platforms. Start with the basics and build your knowledge incrementally, this is the only way to do it, and don't worry if you feel you're making slow progress, it will all fall into place eventually!

If you're building a dynamic site, you'll probably want to use a database. MySQL is available for free on many Web hosting packages and is a good system to start with. You can learn SQL if you like, but if your host provides phpMyAdmin, you can carry out most common tasks using the browser interface. If you're unsure about how to structure your database, it may be worth finding out a bit about relational database modelling.

Related Links
MySQL :: Developer Zone
MySQL Tutorial - Introduction
phpMyAdmin
PhpMyAdmin Tutorial
PHP: PHP Manual - Manual
PHP Tutorial
SQL Tutorial

Tech Interpreter Links:
Why Web Developers are Excited About HTML5 and CSS3
Using Scripts in HTML Pages
What is Web Content?
What is Dynamic Content?
Web Development Concepts: Client and Server
How to Get Started with Web Development

Friday, 29 June 2012

Using XML Schemas (XSDs) to Create Database Tables

Introduction

Developers often need to change the type of data source used for Web projects. Deciding to change from XML to a Relational database system is a common task, and means you can base your Websites and applications on a system that is robust, reliable and easy to connect to using Server Side scripting.

Creating your database tables from XML content or XSD Schema Definitions is generally straightforward. In most cases the XML tree structure can be translated into a Relational database structure, with all of the data elements and relationships represented accurately.

Model

Create a model for your database structure by referring to the elements in your XML and XSD. There are many ways to model database structure, but the most common is to simply list your tables on paper. Coming up with a good design before you build your database is essential for efficiency. Look at your XML and XSD Schema, and list all of the elements that have child elements, as the element "person" does in this example:


John Smith
Postman


It is not necessarily the case that these elements will become tables in your database, but this is the starting point for you to figure that out.

Translate

Translate each element of data in your XML into either a table, or a column in a table, by listing each of them in turn. Starting with your list of elements that have child elements, create a draft list of tables.

Initially assume that the elements you listed are all going to become tables, and list their child elements (for example "name" in the "person" element) as columns. List the name of each column and decide on a data type for it, which may be indicated in your XSD as in the following example, where "occupation" is listed as being a text string:



Structure

Check the structure of your tables by making sure every item of data in your XML can be contained. Also check that no items of data have been repeated, as this will result in an unreliable, inefficient data structure. If your data contains any information that you have not listed among your tables and columns, extend your list to include it, considering whether it makes more sense as a new table, or a column within an existing table.

Columns in existing tables are for items that provide additional information about another item, rather than existing as a core item of data in themselves. In the "person" example, "name" and "occupation" would be columns in the "person" table.

Relationships

Look at your list of tables and identify any relationships between them, for example between tables that were child and parent elements in the original XML. If the "person" element was a child of the following example "country" element, "country" would have a table of its own in the database as well:


Canada

John Smith
Postman


Jane Doe
Teacher



The "country" and "person" tables have a "one to many" relationship, meaning that one "country" item can be associated with many "person" items. To reflect this relationship, you need to add a "Foreign Key" column to the list for your "person" table indicating which "country" the "person" is associated with, which you will do when you create your SQL.

Implement

Create your database tables using your chosen Relational Database Management System, either through a Web interface provided by your Web host, or by creating SQL statements according to the following syntax:

CREATE TABLE Country
( Country_ID int, Country_name varchar(50) )

Each table you create should have a column listing an ID as in the above example, and this ID will be included as a "Foreign Key" linking tables as follows:

CREATE TABLE Person
( Person_ID int, Name varchar(50),
Occupation varchar (20),
Country_ref int )

Each record in the "Person" table will include the "Country_ID" listed in the relevant "Country" record, in its "Country_ref" column, creating a relationship between the two. Enter data from your XML into your new database to make sure it can accommodate everything you need.

Notes

  • If you find data items in your XML content that do not have a logical home in your database, you may need to make additions to your database design, or to amend it in some way.
  • It is not simply the case that elements in XML should be represented as tables in a database, and that their attributes become columns, as this will not always result in the most effective design. Make sure you think carefully about the elements and attributes in your data, and optimize them to suit Relational database structures.

Links