Due: You will do this lab in class on Wednesday, November 21. You need not turn anything in.
Goals:Collaboration: Please work in groups of 2-3. At least one member of each group should have their Wiki-20 project handy (we're going to use it as a sandbox for experimentation).
Contents:Open a terminal windows (or tabs). In one of them,
run tunnel-mysql. In another, find your
Wiki-20 directory.
You may also wish to have the readings for today open in your web browser.
If you have not done so, you may wish to finish the first SQL lab through exercise 5.
creator to our links. To get it working in SQLObject will take a few machinations.model.py, add a Person class that inherits from SQLObject. Person should have two Unicode columns: name and homepage.tg-admin shell and tell the Person class to create its table. Create some instances of Person. Quit the shell.model.py, add the following to the Link class: creator = ForeignKey('Person', name="creator")mysql -u <username> -p -h 127.0.0.1
ALTER TABLE link ADD creator_id INT;
tg-admin shell again. Verify
that this worked by first creating a new person, and then creating a
link which has that person as its creator. Once you are satisfied, quit
python for the time being.model.py, add a MultipleJoin column to the Person class as follows. links = MultipleJoin('Link', joinColumn='creator_id')tg-admin shell again.Person.get(1).links
mysql -u <username> -p -h 127.0.0.1
desc link; to test your hypothesis.desc person; to test your hypothesis.select * from link, person; to be? Try it and see. (The result is called a cross join because it is the cross-product of the two tables.)WHERE clause to the preceding select statement so that only the rows where the person is the link's creator are selected.tag as follows. CREATE TABLE tag (
id INT AUTO_INCREMENT PRIMARY KEY,
tagphrase text
);
desc tag;
to verify the table schema is correct. Insert a few tags into the table
(for example, add a tag where the phrase is "CSC223"), and then select
all rows from the tag table to check your work.link_tag and it should have two int columns: one named link_id and one named tag_id. If a tag describes a link, then a row should appear in the table with that link's id and that tag's id.Here
is the result of describing that table after it has been created; write
your own statement with to create the table. Write your own CREATE TABLE statement to create this table. Then, use desc link_tag; to verify that it worked. Here is an example description:
mysql> desc link_tag;
+---------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------+------+-----+---------+-------+
| link_id | int(11) | YES | | NULL | |
| tag_id | int(11) | YES | | NULL | |
+---------+---------+------+-----+---------+-------+
2 rows in set (0.00 sec)
SELECT href, tagphrase FROM tag, link, link_tag
WHERE link.id = link_tag.link_id
AND tag.id = link_tag.tag_id;
model.py, add a Tag class. Its columns should correspond to those of the tag table we created in the last exercise.tg-admin shell.Tag.get(1) to verify that the Tag class works. You shouldn't need to use Tag.createTable() to create the table; why not?model.py add a RelatedJoin property to the Link class: tags = RelatedJoin('Tag')Tag class and save your work.tg-admin shell again. Verify that each link object has a tags attribute (with the appropriate values) and that each tag object has a links attribute.link_tag
table that exactly match the names SQLObject would have used itself.
What if we hadn't been so careful? How would we get the RelatedJoins to
use different table and column names?
If you still have some time, switch to your project database (USE
databasename) and explore the joins in your project.
Or, explore the use of ORDER BY, GROUP BY, and LIMIT.
Be sure to quit the MySQL client (using exit
or CTRL-D) when you are done.
Janet Davis (davisjan@cs.grinnell.edu)
Created November 20, 2007