5. Tables Relate to Each Other “Keys” to success Fields that relate tables to each other are called “ keys ”. “ Primary key ” is the field (column) containing a unique id for each record. Keys prevent duplication of info in database. donations volunteers campaigns users
6.
7. Tables Relate to Each Other “Keys” to success Fields that relate tables to each other are called “ keys ”. “ Primary key ” is the field (column) containing a unique id for each record. Keys prevent duplication of info in database. donations volunteers campaigns users
8. Structured Query Language SQL A standard language for querying and modifying relational databases. Query Using SQL to ask the database a question. mySQL An Open Source Software relational database management system that uses SQL. mySQL Query Browser One of many available programs that allows you to connect to a database and run queries on it.
9. Structured Query Language Use SQL SELECT, UPDATE, INSERT, or DELETE data. SELECT statements ask the database to display data. SQL is the standard language used to speak to databases Use a SQL client to connect to the database and execute SELECT, UPDATE, INSERT, or DELETE statements.
[3]{35} Q: Why should I stay awake for this? A: Campaigning is all about lists. Tables contain lists. Databases are sets of tables. Supporters Volunteers Donors Voters Legislators Others?
{10 min} Get the Picture: How it all fits together (TAKE A LONG TIME ON THIS SLIDE, bringing in pieces one at a time.) Server Houses Database Website / Web server Share data back and forth via a Server-side application – e.g. ASP, PHP, CF; Can folks think of others? At MoveOn, we use a scripting language called Mason, which is basically Perl inside packets that let web pages process it. All good fun, but the point is to share data with Personal computers – single; collective is the WWW So you collect data from them using Secure data transfer via forms – e.g. donation form, petition form; What are some other examples? Great, but what do you do with that data? You can publish it back to the public via the website (over the Internet ) – e.g. donation thermometer, blog comments; Other? Or use a database client – e.g. mySQL QB – to really get your hands on the data. Use SQL to ask the database questions, like: Have we raised enough $ today to buy pizza for the volunteers? How many volunteers do we have in that precinct? Or, what’s the ratio of volunteers to residents in that precinct? Are there enough supporters in that DMA to have a successful media event next week?
{5 min} Definitions (clarity over accuracy) Database In practice, a set of tables. Relational databases (like the ones that run websites) have tables that are related to each other by common fields. Table A table is a collection of data organized into columns (fields) and rows (records) and related to other tables by common fields. Field A column. A field contains all the same type of data; each cell refers to a different unit. Record A row. A record contains all the different types of data about a unit of analysis (e.g. user, order, letter).
{10 min} TELL THEM THIS IS THE MOST IMPORTANT SLIDE TO UNDERSTAND TAKE SOME TIME ON IT {Don’t click yet} In a world without relational database management systems, we might have a person in charge of fundraising with a list of donors. Back 5 years (or today on smaller campaigns) this might be kept in Excel. Back 15, this might have been a little box of index cards. Before that, stone tablets. The volunteer coordinator might have another list, and never the twain shall meet. Today, we keep all our data (I hope) in RDBMS that neatly link together data that all arms of the campaign collect: who gave online, who signed a petition on the street, what fundraising campaigns are we running, and who gave to each of those campaigns, or volunteered for them; which precincts are we canvassing, with which volunteers, etc. The idea of a relational database is that there’s no (or little) duplication of data. Once we collect contact info for a person, we don’t reenter it somewhere else in the database each time she takes an action, we record the action she took and relate it back to her contact info. All this data is related in the database through unique record identifiers called keys . {Click to bring in arrow that shows how users is related to donations} This “users” table contains supporters’ contact info. User_id is the primary key. Each primary key is unique in this table. That way, if two people named “Jim Smith” sign up, we won’t get them confused. One is user_id = 1 and the other is user_id = 5. Everything Sarah Jones does is linked back to her contact info using her user_id, 2. If Sarah donates, she shows up in the “volunteers” table, but not as “Sarah Jones” as user_id #2. {Click to bring up Volunteers and Campaigns tables} If Sarah volunteers, she shows up in the “volunteers” table, but not as “Sarah Jones” as user_id #2. What other kind of info might we want to record?
{5 min} Answer these questions using the tables provided. Go around and make sure people get the right answers. Note and help them if not.
{5 min} Review answers in group BE SURE TO DO Q&A BEFORE MOVING ON
{5 min for this and next 2 slides – Defining SQL} SQL A standard language for querying and modifying relational databases. Query Using SQL to as the database a question. mySQL MySQL is an Open Source Software relational database management system that uses SQL. mySQL Query Browser One of many available programs that allows you to connect to a database and run queries on it.
If you are not the DBA, and you probably won’t be, then you don’t have direct access to the database. Since you don’t have access to the server where the database lives, it is as if it’s in another dimension, “the other side”. You can’t communicate with it directly. You need a medium: an SQL client, like mySQL QB. Use QB to connect to the database and communicate with it, by executing SQL statements.
This is mySQL Query Browser, which is a medium for communicating with the database. There are lots of SQL clients – this is just an example. You can see here, you put in info about the server, a username and password (just like FTP, for those familiar with that). The QB brings up a map of the tables, and you can write SQL statements to query the data in those tables. We’re not going to use mySQL QB today, we’re just going to talk about how to create a simple SQL query.
{10 min for this + next 4 slides} The most important type of SQL statement is SELECT. In a SELECT statement, you are required to give 2 pieces of info: which fields and which tables… Next screen to show basic query.
* = all fields Result would be the entire table.
* = all fields Result would be one column, user_id, that contained a row for every record.
The next most useful clause is the WHERE clause. Not required, but this is where you limit the results by defining criteria. Go to next slide for example.
{8 min} Write SQL queries for as many of these as you can in the next 8 minutes. Turn in to Justin via email by end of session.