NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan

advertisement
NMED 3850 A
Advanced Online Design
January 12, 2010
V. Mahadevan
Overview




Some background material.
Simple SQL.
Lab Tasks.
References.
3-tier Client-Server Architecture




Client: makes requests for information. E.g. web browser, ftp
client.
Server: responds to requests for information from clients. E.g.
web server, ftp server.
Serving static web pages to a web browser is often
implemented using a 2-tier Client-Server Architecture.
Dynamic (interactive) content is typically implemented using a
3-tier Client-Server Architecture.
3-tier Client-Server Architecture (cont.)
3-tier Client-Server Architecture (cont.)



Presentation tier: Provides the user interface and displays
results in a human readable format.
Business Logic tier: Performs calculations and data
processing.
Database tier: Provides storage and retrieval of
information.
3-tier Client-Server Architecture (cont.)

The order of operations is as follows:






A request is made by the user at the Presentation Tier (for example,
a form in a web page).
The request is then submitted to the Business Logic Tier for
processing (for example, an application running on a web server).
The Business Logic Tier then queries the Database for the required
information.
The Database retrieves the required information and returns it to
the Business Logic Tier.
The Business Logic Tier then does any required processing of the
information and returns the result to the Presentation Tier.
The Presentation Tier presents the result in human readable format
to the user.
Apache httpd, PHP, and MySQL




Apache httpd: The most popular web server on the world
wide web.
PHP: Scripting language for dynamic web page creation.
MySQL: Advanced relational database management
system.
Combine the above ingredients to build interactive,
feature-rich websites.
Simple SQL



SQL: Structured Query Language.
Standardized language to retrieve data from relational
databases.
Earlier, non-relational databases consisted simply of flat files
that stored data separated by a delimiter (such as a carriage
return (“\n”). For example, consider a plain text file:
last_name, first_name, age
Smith, Bob, 21
Jones, Robert, 23

The above is a database, but it is rather difficult to extract
information from it without writing a text parser. Additionally,
it becomes troublesome when there are a large number of
records as you need to parse the file sequentially to find a
particular data element (of which there could be thousands).
Simple SQL (cont.)



A relational database uses the concept of relations
(tables) to store data (much like a spreadsheet):
person_id
last_name
first_name
age
1
Smith
Bob
21
2
Jones
Robert
23
SQL allows you to write a query to quickly find the data
you are looking for e.g. find all people with the last name
“Jones” without having to sequentially parse the entire
database file.
Each row in the table is known as a tuple.
Simple SQL (cont.)

Once the database table(s) are defined, several operations
can be performed on them.






Inserting data;
Retrieving data;
Updating data;
Deleting data.
SQL has a standard syntax for each of these operations.
But you have to CREATE the table first.
Simple SQL (cont.)

To create the table Person described earlier using
standard SQL syntax:


CREATE table Person (person_id INT, last_name
VARCHAR(20), first_name VARCHAR(20), age INT);
To confirm that the table has indeed been created, use
the following command:

SHOW tables;
Simple SQL (cont.)



So what exactly did we just do?
Created a table called Person in the MySQL database
management system (DBMS).
The syntax is broken down as follows:





CREATE table Person  create a table called “Person”.
(person_id INT,  create an attribute called “person_id” with the
data type INT i.e. an Integer (whole number).
last_name VARCHAR(20),  create an attribute called “last_name”
with the data type VARCHAR(20) i.e. text 20 characters long.
first_name VARCHAR(20),  create an attribute called “first_name”
with the data type VARCHAR(20) i.e. text 20 characters long.
Age INT);  create an attribute called “age” with the data type INT
i.e. an Integer (whole number).
Simple SQL (cont.)



So now that we have the table created, we can populate it
with data.
Inserting data into a table is achieved using the INSERT
keyword.
For example, to insert 2 rows into the table Person, the
following syntax can be used:


INSERT INTO Person VALUES(1, 'Smith', 'Bob', 21);
INSERT INTO Person VALUES(2, 'Jones', 'Robert', 23);
Simple SQL (cont.)

The syntax is broken down as follows:


INSERT INTO Person  insert a row into the table Person.
VALUES(1, 'Smith', 'Bob', 21);  the row will have the values 1,
‘Smith’, ‘Bob’, 21 which correspond to the table definition of
person_id, last_name, first_name, age in that order.
Simple SQL (cont.)


Once some data is inserted into the table, we can then
retrieve it by running SELECT queries on the table.
Here are some examples of SELECT queries:






SELECT * FROM Person;
The above query returns all the rows and columns from table
Person.
SELECT * FROM Person WHERE last_name = ‘Jones’;
The above query returns all the rows and columns from table
Person where the last name is “Jones”.
SELECT * FROM Person WHERE age = 23;
The above query selects all the rows and columns from table
Person where the age is 23.
Simple SQL (cont.)

The syntax can be broken down as follows:



SELECT * FROM Person  select all columns from table
Person.
WHERE age = 23;  return only the rows where the age
attribute is equal to 23.
SELECT statements can be modified in different ways.
For example, you can select just the first column
(last_name) or use a greater than operator (>) for age:


SELECT last_name FROM Person;
SELECT * from Person WHERE age > 18;
Simple SQL (cont.)


Existing data in a table can also be updated using the
keyword UPDATE.
An example of updating the last name:



UPDATE Person SET last_name = ‘Graham’ WHERE person_id
= 2;
The above will set the last_name to “Graham” on the row
where the person_id field is equal to 2.
Another example, updating the first name based on the
last name condition:

UPDATE Person SET first_name = ‘Will’ WHERE last_name =
“Smith”;
Simple SQL (cont.)

Lastly, exisiting rows can also be deleted from a table as
follows:




DELETE FROM Person WHERE person_id = 1;
The above will delete the row where person_id is equal to 1.
DELETE FROM Person WHERE last_name = ‘Graham’;
The above will delete the row where last_name is equal to
“Graham”;
Lab Tasks


Login to the Linux server “newmedia.sfa.uleth.ca” using
PuTTY.
Login to the MySQL command line using the command:




mysql --user=<username> --password=<password> <dbname>
<username> is your provided username, password is your
provided password, dbname is your provided database name.
Run all the example SQL statements provided in the
previous slides.
When you are done, destroy the entire Person table using
the command:

DROP TABLE Person;
Lab Tasks (cont.)






Think of a new table to add to the database with at least
5 attributes i.e. 5 columns.
CREATE the table in your MySQL database.
INSERT at least 5 rows of data into the table.
Write at least 3 SELECT statements, each based on
different selection criteria (i.e. different WHERE clauses).
Write at least 3 UPDATE statements, each based on
different updating criteria (i.e. different WHERE clauses).
Write at least 3 DELETE statements, each based on
different deletion criteria (i.e. different WHERE clauses).
Lab Tasks (cont.)



Copy and paste the output from your MySQL session
into a Word document.
Write a paragraph or two describing your understanding
of tables, namely creation, insertion of data, selection of
data, updating of data, and deletion of data. Is there
anything confusing about the process? What sort of data
are you trying to store in your new table?
E-mail the word document to me.
Hosting Services




Most web hosts today offer web hosting, scripting (PHP,
JSP, ASP, etc.), and databases (MySQL, PostgreSQL) in a
standard package.
Management is provided via a web interface or command
line interface.
Uploads of user files are done via ftp or sftp.
Site for web host reviews:
http://www.webhostingreview.ca/
References

MySQL 5.0 Reference Manual:
http://dev.mysql.com/doc/refman/5.0/en/index.html
Download