Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Weeks Schedule
CamelWeb Creations – 8 weeks plan
Table of Contents
-2-
CamelWeb Creations – 8 weeks plan
Goals:
Chapter Documentation:
http://en.wikipedia.org/wiki/Server-side_scripting
http://www.tutebox.com/976/computers/programming/basics-of-php-part1/
http://inpics.net/phpbasics.html
-3-
CamelWeb Creations – 8 weeks plan
SQL Basics
Goals:
MYSQL Server
Why MySQL?
The MySQL database has become the world's most popular open source database because of
its high performance, high reliability and ease of use. It is also the database of choice for a
new generation of applications built on the LAMP stack (Linux, Apache, MySQL, PHP / Perl /
Python.)
Many of the world's largest and fastest-growing organizations including Facebook, Google,
Adobe, Alcatel Lucent and Zappos rely on MySQL to save time and money powering their high-
volume Web sites, business-critical systems and packaged software.
MySQL runs on more than 20 platforms including Linux, Windows, Mac OS, Solaris, IBM AIX,
giving you the kind of flexibility that puts you in control. Whether you're new to database
technology or an experienced developer or DBA, MySQL offers a comprehensive range of
database tools, support, training and consulting services to make you successful.
Elements
-4-
CamelWeb Creations – 8 weeks plan
Predicates, which specify conditions that can be evaluated to SQL three-valued logic
(3VL) or Boolean (true/false/unknown) truth values and which are used to limit the
effects of statements and queries, or to change program flow.
Queries, which retrieve the data based on specific criteria. This is the most important
element of SQL.
Statements, which may have a persistent effect on schemata and data, or which may
control transactions, program flow, connections, sessions, or diagnostics.
IMPORTANT: SQL statements also include the semicolon (";") statement terminator.
Though not required on every platform, it is defined as a standard part of the SQL grammar.
NOTE: Insignificant whitespace is generally ignored in SQL statements and queries, making it
easier to format SQL code for readability.
Queries
The most common operation in SQL is the query, which is performed with the declarative
SELECT statement. SELECT retrieves data from one or more tables, or expressions. Standard
SELECT statements have no persistent effects on the database. Some non-standard
implementations of SELECT can have persistent effects, such as the SELECT INTO syntax that
exists in some databases.
Queries allow the user to describe desired data, leaving the database management system
(DBMS) responsible for planning, optimizing, and performing the physical operations necessary
to produce that result as it chooses.
A query includes a list of columns to be included in the final result immediately following the
SELECT keyword. An asterisk ("*") can also be used to specify that the query should return all
columns of the queried tables.
SELECT is the most complex statement in SQL, with optional keywords and clauses that
include:
The FROM clause which indicates the table(s) from which data is to be retrieved. The
FROM clause can include optional JOIN subclauses to specify the rules for joining tables.
-5-
CamelWeb Creations – 8 weeks plan
The WHERE clause includes a comparison predicate, which restricts the rows returned
by the query. The WHERE clause eliminates all rows from the result set for which the
The GROUP BY clause is used to project rows having common values into a smaller set
eliminate duplicate rows from a result set. The WHERE clause is applied before the
GROUP BY clause.
The HAVING clause includes a predicate used to filter rows resulting from the GROUP
BY clause. Because it acts on the results of the GROUP BY clause, aggregation functions
The ORDER BY clause identifies which columns are used to sort the resulting data, and
in which direction they should be sorted (options are ascending or descending). Without
Example:
The following is an example of a SELECT query that returns a list of expensive books
SELECT *
FROM Book
WHERE price > 100.00
ORDER BY title;
Data manipulation
The Data Manipulation Language (DML) is the subset of SQL used to add, update and delete
data:
INSERT adds rows (formally tuples) to an existing table, e.g.,:
INSERT INTO My_table
(field1, field2, field3)
VALUES
('test', 'N', NULL);
UPDATE modifies a set of existing table rows, e.g.,:
-6-
CamelWeb Creations – 8 weeks plan
UPDATE My_table
SET field1 = 'updated value'
WHERE field2 = 'N';
MERGE is used to combine the data of multiple tables. It combines the INSERT and
UPDATE elements. It is defined in the SQL:2003 standard; prior to that, some
databases provided similar functionality via different syntax, sometimes called "upsert".
Data types
Each column in an SQL table declares the type(s) that column may contain. ANSI SQL includes
the following data types
Character strings
• CHARACTER(n) or CHAR(n) — fixed-width n-character string, padded with spaces as
needed
• CHARACTER VARYING(n) or VARCHAR(n) — variable-width string with a maximum size
of n characters
• NATIONAL CHARACTER(n) or NCHAR(n) — fixed width string supporting an international
character set
• NATIONAL CHARACTER VARYING(n) or NVARCHAR(n) — variable-width NCHAR string
Bit strings
• BIT(n) — an array of n bits
• BIT VARYING(n) — an array of up to n bits
Numbers
• INTEGER and SMALLINT
• FLOAT, REAL and DOUBLE PRECISION
• NUMERIC(precision, scale) or DECIMAL(precision, scale)
TIP: For example, the number 123.45 has a precision of 5 and a scale of 2. The precision is a
positive integer that determines the number of significant digits in a particular radix (binary or
-7-
CamelWeb Creations – 8 weeks plan
decimal). The scale is a non-negative integer. A scale of 0 indicates that the number is an
integer. For a decimal number with scale S, the exact numeric value is the integer value of the
significant digits divided by 10S.
SQL provides a function to round numerics or dates, called TRUNC (in Informix, DB2,
PostgreSQL, Oracle and MySQL) or ROUND (in Informix, Sybase, Oracle, PostgreSQL and
Microsoft SQL Server)
SQL provides several functions for generating a date / time variable out of a date / time string
(TO_DATE, TO_TIME, TO_TIMESTAMP), as well as for extracting the respective members
(seconds, for instance) of such variables. The current system date / time of the database
server can be called by using functions like NOW.
Keys/Index
Relational databases like SQL Server use indexes to find data quickly when a query is
processed. Creating and removing indexes from a database schema will rarely result in
changes to an application's code; indexes operate 'behind the scenes' in support of the
database engine. However, creating the proper index can drastically increase the performance
of an application.
The SQL Server engine uses an index in much the same way a reader uses a book index. For
example, one way to find all references to INSERT statements in a SQL book would be to begin
-8-
CamelWeb Creations – 8 weeks plan
on page one and scan each page of the book. We could mark each time we find the word
INSERT until we reach the end of the book.
This approach is pretty time consuming and laborious. Alternately, we can also use the index in
the back of the book to find a page number for each occurrence of the INSERT statements.
This approach produces the same results as above, but with tremendous savings in time.
When a SQL Server has no index to use for searching, the result is similar to the reader who
looks at every page in a book to find a word: the SQL engine needs to visit every row in a
table. In database terminology we call this behavior a table scan, or just scan.
A table scan is not always a problem, and is sometimes unavoidable. However, as a table
grows to thousands of rows and then millions of rows and beyond, scans become
correspondingly slower and more expensive.
Consider the following query on the Products table of the Northwind database. This query
retrieves products in a specific price range:
The database engine can use indexes to boost performance in a number of different queries.
Sometimes these performance improvements are dramatic. The query optimizer's job is to find
the fastest and least resource intensive means of executing incoming queries. An important
part of this job is selecting the best index or indexes to perform the task
The most obvious use for an index is in finding a record or set of records matching a WHERE
clause. Indexes can aid queries looking for values inside of a range (as we demonstrated
earlier), as well as queries looking for a specific value.
-9-
CamelWeb Creations – 8 weeks plan
By way of example, the following queries can all benefit from an index on UnitPrice:
DELETE FROM Products WHERE UnitPrice = 1
UPDATE Products SET Discontinued = 1 WHERE UnitPrice > 15
SELECT * FROM PRODUCTS WHERE UnitPrice BETWEEN 14 AND 16
Indexes work just as well when searching for a record in DELETE and UPDATE commands as
they do for SELECT statements.
Sorting Records
When we ask for a sorted dataset, the database will try to find an index and avoid sorting the
results during execution of the query. We control sorting of a dataset by specifying a field, or
fields, in an ORDER BY clause, with the sort order as ASC (ascending) or DESC (descending).
For example, the following query returns all products sorted by price:
With no index, the database will scan the Products table and sort the rows to process the
query. However, the index we created on UnitPrice (IDX_UnitPrice) earlier provides the
database with a presorted list of prices. The database can simply scan the index from the first
entry to the last entry and retrieve the rows in sorted order.
The same index works equally well with the following query, simply by scanning the index in
reverse:
Grouping Records
We can use a ”GROUP BY” clause to group records and aggregate values, for example,
counting the number of orders placed by a customer. To process a query with a GROUP BY
clause, the database will often sort the results on the columns included in the GROUP BY.
- 10 -
CamelWeb Creations – 8 weeks plan
The following query counts the number of products at each price by grouping together records
with the same UnitPrice value.
The database can use the IDX_UnitPrice index to retrieve the prices in order. Since matching
prices appear in consecutive index entries, the database is able count the number of products
at each price quickly. Indexing a field used in a GROUP BY clause can often speed up a query.
Maintaining a Unique Column
Columns requiring unique values (such as primary key columns) must have a unique index
applied. There are several methods available to create a unique index. Marking a column as a
primary key will automatically create a unique index on the column. We can also create a
unique index by checking the Create UNIQUE checkbox in the dialog shown earlier.
The screen shot of the dialog displayed the index used to enforce the primary key of the
Products table. In this case, the Create UNIQUE checkbox is disabled, since an index to enforce
a primary key must be a unique index. However, creating new indexes not used to enforce
primary keys will allow us to select the Create UNIQUE checkbox. We can also create a unique
index using SQL with the following command:
The above SQL command will not allow any duplicate values in the ProductName column, and
an index is the best tool for the database to use to enforce this rule. Each time an application
adds or modifies a row in the table, the database needs to search all existing records to ensure
none of values in the new data duplicate existing values. Indexes, as we should know by now,
will improve this search time.
SQL Join
SQL joins are used to query data from two or more tables, based on a relationship between
certain columns in these tables.
The JOIN keyword is used in an SQL statement to query data from two or more tables, based
on a relationship between certain columns in these tables. Tables in a database are often
- 11 -
CamelWeb Creations – 8 weeks plan
A primary key is a column (or a combination of columns) with a unique value for each row.
Each primary key value must be unique within the table. The purpose is to bind data together,
across tables, without repeating all of the data in every table.
Note that the "P_Id" column is the primary key in the "Persons" table. This means that no two
rows can have the same P_Id. The P_Id distinguishes two persons even if they have the same
name.
Note that the "O_Id" column is the primary key in the "Orders" table and that the "P_Id"
column refers to the persons in the "Persons" table without using their names.
Notice that the relationship between the two tables above is the "P_Id" column.
- 12 -
CamelWeb Creations – 8 weeks plan
We will list the types of JOIN you can use, and the differences between them:
JOIN: Return rows when there is at least one match in both tables
LEFT JOIN: Return all rows from the left table, even if there are no matches in the right
table
RIGHT JOIN: Return all rows from the right table, even if there are no matches in the
left table
FULL JOIN: Return rows when there is a match in one of the tables
User rights
DCL commands are used to enforce database security in a multiple user database
environment. Two types of DCL commands are GRANT and REVOKE. Only Database
Administrator's or owner's of the database object can provide/remove privileges on a databse
object.
privilege_name is the access right or privilege granted to the user. Some of the access
rights are ALL, EXECUTE, and SELECT.
object_name is the name of an database object like TABLE, VIEW, STORED PROC and
SEQUENCE.
user_name is the name of the user to whom an access right is being granted.
user_name is the name of the user to whom an access right is being granted.
- 13 -
CamelWeb Creations – 8 weeks plan
For Example: REVOKE SELECT ON employee FROM user1;This commmand will REVOKE a
SELECT privilege on employee table from user1.When you REVOKE SELECT privilege on a table
from a user, the user will not be able to SELECT data from that table anymore. However, if the
user has received SELECT privileges on that table from more than one users, he/she can
SELECT from that table until everyone who granted the permission revokes it. You cannot
REVOKE privileges if they were not initially granted by you.
1) System privileges - This allows the user to CREATE, ALTER, or DROP database objects.
2) Object privileges - This allows the user to EXECUTE, SELECT, INSERT, UPDATE, or
- 14 -
CamelWeb Creations – 8 weeks plan
NOTE: The above rules also apply for ALTER and DROP system privileges.
Roles: Roles are a collection of privileges or access rights. When there are many users in a
database it becomes difficult to grant or revoke privileges to users. Therefore, if you define
roles, you can grant or revoke privileges to users, thereby automatically granting or revoking
privileges. You can either create Roles or use the system roles pre-defined by oracle.
Some of the privileges granted to the system roles are as given below:
- 15 -
CamelWeb Creations – 8 weeks plan
Creating Roles:
The Syntax to create a role is:
For example: To create a role called "developer" with password as "pwd",the code will be as
follows:
CREATE ROLE testing
[IDENTIFIED BY pwd];
It's easier to GRANT or REVOKE privileges to the users through a role rather than assigning a
privilege direclty to every user. If a role is identified by a password, then, when you GRANT or
REVOKE privileges to the role, you definetely have to identify it with the password.
For example: To grant CREATE TABLE privilege to a user by creating a testing role:
First, create a testing Role:
CREATE ROLE testing
Second, grant a CREATE TABLE privilege to the ROLE testing. You can add more
privileges to the ROLE:
GRANT CREATE TABLE TO testing;
• To revoke a CREATE TABLE privilege from testing ROLE, you can write:
REVOKE CREATE TABLE FROM testing;
- 16 -
CamelWeb Creations – 8 weeks plan
Server-side processing
Server-side processing in DataTables is exceptionally useful if you want display large data
sources in a quick and easy manner, with controls such as filtering, sorting and paging. Since
all of the heavy lifting is done on the server-side, there needs to be a script which will do this
work on the server, and DataTables can ask for the data to display.
This can be a little tricky to implement correctly initially, but presented here are a number of
scripts which can be used and customised to your specific needs.
Each script uses the same data source (the schema and data are available here) to make each
implementation basically the same in functionality as it is presented.
There are times when reading data from the DOM is simply too slow or unwieldy, particularly
when dealing with thousands or millions of data rows. To address this DataTables' server-side
processing feature provides a method to let all the "heavy lifting" be done by a database
engine on the server-side (they are after-all highly optimised for exactly this kind of thing),
and then have that information drawn in the user's web-browser. As such you can display
tables consisting of millions of rows with ease.
When using server-side processing, DataTables will make an XHR request to the server for
each draw of the information on the page (i.e. when paging, sorting, filtering etc). DataTables
will send a number of variables to the server to allow it to perform the required processing,
and then return the data in the format required by DataTables.
JSON return - DataTables is extremely flexible in the JSON data that it can consume from the
server through the mDataProp option which allows you to get data from arrays (the default) or
Javascript objects, including nested-objects and array.
- 17 -
CamelWeb Creations – 8 weeks plan
Initialisation parameters
fnServerData This parameter allows you to override the default function which obtains
the data from the server ($.getJSON) so something more suitable for your
application. For example you could use POST data, or pull information from
a Gears or AIR database.
fnServerParams It is often useful to send extra data to the server when making an Ajax
request - for example custom filtering information, and this callback
function makes it trivial to send extra information to the server. The
passed in parameter is the data set that has been constructed by
DataTables, and you can add to this or modify it as you require.
sAjaxDataProp By default DataTables will look for the property 'aaData' when obtaining
data from an Ajax source or for server-side processing - this parameter
allows that property to be changed. You can use Javascript dotted object
notation to get a data source for multiple levels of nesting.
sAjaxSource You can instruct DataTables to load data from an external source using this
parameter (use aData if you want to pass data in you already have).
Simply provide a url a JSON object can be obtained from. This object must
include the parameter 'aaData' which is the data source for the table.
sServerMethod Set the HTTP method that is used to make the Ajax call for server-side
processing or Ajax sourced data.
Implemented features
Each of the implementations differs in the feature set that it implements (the feature set is
based upon the data that is sent to the server-side process through the passed parameters.
Note that the PHP with MySQL version will always be kept up to data with support for the full
feature server that DataTables provides (with the exception of regex filtering support due to
performance of regex on large tables) - others are updated as the implementation authors
develop each individual script.
- 18 -
CamelWeb Creations – 8 weeks plan
Chapter Documentation:
http://en.wikipedia.org/wiki/SQL
http://www.mysql.com/
http://dev.mysql.com/doc/refman/5.0/en/tutorial.html
- 19 -
CamelWeb Creations – 8 weeks plan
Javascript
Goals:
Client server scripting
Javascript syntax
Tampering HTML via Javascript
Alerts and Form validation
JSON
Documentation
http://www.webteacher.com/javascript/
http://www.w3schools.com/js/
http://homepage.ntlworld.com/kayseycarvey/
- 20 -