cis20.2 relational design and implementation of software applications 2 spring 2010 lecture # II.2 • a relational consists of multiple tables today’s topics: • each is defined as a number of fields • relational databases • is stored in a table so that a single entry in a table, called a record, provides one data element for each field • SQL • a table can be thought of as a spreadsheet, the fields are columns in the spreadsheet, and the records are rows • records can have “unique” fields, which are called keys • if a record does not have a value for a particular field, then a NULL value is entered • “relational” databases consist of multiple tables that relate to each other by having one (field) in common

cis20.2-spring2010-sklar-lecII.2 1 cis20.2-spring2010-sklar-lecII.2 2

SQL example: create, describe, ,

• SQL = Structured • tblUser userID lastname firstname • MySQL is a free database management system (DBMS) that implements SQL 1 sklar elizabeth http://www.mysql.com 2 mouse mickey • basic data definition commands: 3 mouse minnie – CREATE — to create a table 4 potter harry – DESCRIBE — to describe a table’s definition • The userID uniquely identifies a single person in the tblUser table. – DROP — to a table • tblBday • basic data manipulation commands: bdayID month day 1 12 11 – INSERT — to put data into a table 2 10 9 – SELECT — to see what is in a table 3 8 7 – UPDATE — to edit data that is already in a table • The bdayID uniquely identifies a single birth date in the tblBday table. – DELETE — to remove data a table • These are connected using a “” called rltUserBday.

cis20.2-spring2010-sklar-lecII.2 3 cis20.2-spring2010-sklar-lecII.2 4 • rltUserBday • Here are the commands to generate the example. userID bdayID Note the convention of putting MySQL keywords in all CAPS. 1 1 • First, create the user table: 2 2 3 3 > CREATE TABLE tblUser ( 4 1 userID INT(11) NOT NULL AUTO_INCREMENT, lastname TEXT, • rltUserBday tblUser tblBday The table is used to the table to the table in order firstname TEXT); to look up a person’s birthday. • Note that users with userID=1 and userID=4 have the same birthday! Note the keywords: • Here is the “join” command in mysql: – “NOT NULL” means that the field can never be (empty) – “PRIMARY KEY” means that the field must be unique – SELECT * “AUTO INCREMENT” means that MySQL will generate a automatically FROM tblUser, tblBday, rltUserBday • WHERE tblUser.userID=rltUserBday.userID Second, create the birthday table: AND tblBday.bdayID=rltUserBday.bdayID; mysql> CREATE TABLE tblBday ( bdayID INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, month INT, day INT); cis20.2-spring2010-sklar-lecII.2 5 cis20.2-spring2010-sklar-lecII.2 6

• Third, create the relation: • Next, put data into your user table using the “INSERT” command:

mysql> CREATE TABLE rltUserBday ( mysql> INSERT INTO tblUser (lastname, firstname) userID INT(11) NOT NULL PRIMARY KEY, VALUES (’sklar’, ’elizabeth’); bdayID INT(11) NOT NULL); mysql> INSERT INTO tblUser (lastname, firstname) • Now look at your tables: VALUES (’mouse’, ’mickey’);

mysql> SHOW TABLES; mysql> INSERT INTO tblUser (lastname, firstname) to get a list of all the tables in your database VALUES (’mouse’, ’minnie’);

mysql> INSERT INTO tblUser (lastname, firstname) mysql> DESCRIBE tblUser; VALUES (’potter’, ’harry’);

to look at the definition of the user table and look at your data:

mysql> SELECT * FROM tblUser;

cis20.2-spring2010-sklar-lecII.2 7 cis20.2-spring2010-sklar-lecII.2 8 • Then, put data into your bday table: • Then, populate the user-bday relation. You can do this manually:

mysql> INSERT INTO tblBday (month, day) mysql> INSERT INTO rltUserBday (userID,bdayID) VALUES (1,1); VALUES (12, 11); which requires that you know what the values of userID and bdayID are mysql> INSERT INTO tblBday (month, day) • You can also do this with a query that looks up the ID values and inserts them VALUES (10, 9); automatically into the relation:

mysql> INSERT INTO tblBday (month, day) INSERT INTO rltUserBday (userID,bdayID) VALUES (8, 7); SELECT userID,bdayID FROM tblUser,tblBday WHERE lastname=’sklar’ and look at your data: AND firstname=’elizabeth’ mysql> SELECT * FROM tblBday; AND month=1 AND day=1;

cis20.2-spring2010-sklar-lecII.2 9 cis20.2-spring2010-sklar-lecII.2 10

updating table values deleting entries from tables

• use the UPDATE command to change the values in a table • use the DELETE command to remove an entry () from a table

UPDATE tblUser mysql> DELETE lastname=’mantle’ FROM tblUser WHERE firstname=’mickey’; WHERE lastname=’mouse’;

• WARNING: be careful when deleting! I always run a SELECT command first, to make sure that I am deleting the row(s) that I wanted:

mysql> SELECT * FROM tblUser WHERE lastname=’mouse’;

mysql> DELETE FROM tblUser WHERE lastname=’mouse’;

cis20.2-spring2010-sklar-lecII.2 11 cis20.2-spring2010-sklar-lecII.2 12 deleting tables

• you can delete an entire table using the DROP command

mysql> DROP TABLE tblUser;

cis20.2-spring2010-sklar-lecII.2 13