CSE134A LECTURE NOTES

October 3, 2001
 
 

ANNOUNCEMENTS

The deadline for the second project is Monday October 22, 4:30pm.  I'm handing out the second project description now.

Some issues are very important in all 134A projects: good user interface design, good software design, teamwork, and a well-written report.  I'll be talking more later about the others.  About the report:

You should start early, and you are welcome to bring your outline or your draft to a TA office hour to discuss it and get ideas for improving it. The report should be detailed enough to be interesting, but it should also be six pages single-spaced at most. You should use the report-writing skills that you learned in your general education and lab science classes.  These skills are important for graduate school, for software jobs with top companies, and for project leader and management jobs in all organizations.
 
 

QUERIES IN SQL

For example:        select author, date from messages where date > '2000-11-11'

The answer from a query is called a "result set."  Conceptually, a result set is the same as a new table, except that result sets are not stored persistently in the database.

Result sets can be ordered:
        select author, date
            from messages
            where date > '2000-11-11'
            order by date

Each line that adds detail to what should be selected is called a "clause."  The example above has from, where, and order by clauses.  By default order is increasing.
 
 

UPDATING TABLES

So far we seen how to do queries but not inserts, changes, or deletes.
delete from messages where date < '2000-11-11';
update messages set author = 'John Smith' where author = 'Smith';
insert messages (author, date) values('Brown',curdate());
Syntax note: curdate() is a builtin SQL function, not a PHP function.  Semantics note: the where clause is optional in delete and update commands.  If you leave it out, every record will be affected.  This is almost never what you want.

Usually your PHP script will build up an SQL command by concatenating various substrings, and then send the command to be executed by the MySQL server.  For example:

$cmd = "insert messages (author, date) values('Brown'," . today() . ")";
$result = mysql_query($cmd, $link);
In the PHP code above, today() is a PHP function.

What happens when we create a new tuple and only provide values for some of its fields?  Then the database server fills in default values for the remaining fields.  The most common default value is NULL.

What happens when an insert command fails to specify a value for a field that is supposed to be NOT NULL?  I couldn't find the answer in our MySQL book!
 
 

UNIQUE IDENTIFIERS

Often it is convenient to have an id number for each record in a table.  You can also specify primary key as a modifier in a field declaration.  For example:
create table messages (
    ...
    id    integer unsigned not null auto_increment primary key
    ... )
Now every record should be inserted with a NULL value for the field named id.  The server will then automatically assign a value to this field that is one larger than the largest value ever assigned previously to a record for this table.
 
 

SUMMARIES IN "SELECT" QUERIES

Here is a sequence of select queries illustrating how to generate reports about the information in a database.
select count(*) from messages;

select count(*) from messages where author = 'Smith';

select max(date) from messages where author = 'Smith';

select distinct author from messages order by author;

select count(distinct author) from messages;

select dayofweek(date) as d, count(*) from messages group by d;

The last example above reports how many messages are in the database from each of the seven days of the week.

Note that distinct is a keyword, written without parentheses, but dayofweek() is a builtin function.  Other builtin group-based functions include min, max, sum, and avg.
 
 

JOIN QUERIES

Suppose that we have information about messages in two different tables.  We need to use both to decide which messages to display.  For example:
select messages.*, address.zip from messages, address
   where messages.author = address.author
This type of query is called a "join" query.

Notice that the join is specified in the where clause, just like a logical condition on values in a single record.
 
 



Copyright (c) by Charles Elkan, 2001.