CS162 Discussion Notes March 31, 2011 Jorge Ortiz ACKNOWLEDGEMENTS Paul A. Strassmann, George Mason University Rightscale.com Kirk Anne, SUNY Geneseo Randy Katz, UC Berkeley Outline • Cloud computing – What is it? • Virtualization – Hypervisor configurations • Amazon EC2 • Relational databases – Entity relationship diagrams • Data models – Imperative versus declarative • SQL What is cloud computing? • Cloud computing is Internet-based computing, whereby shared resources, software and information are provided to computers and other devices on-demand, like electricity – Wikipedia Data Centers Google’s Containerized Datacenter Microsoft Chicago Datacenter 6 What runs on these machines? • All types of applications! • How can we make best use of the resources? Virtualization (2) Virtualization (3) Amazon EC2 Architecture Applications • • • • Web applications Databases Compute-intensive jobs Client-Server applications – Project 3! • Chat Server will be in the cloud • Clients will be outside the cloud • Application state stored in database Parts of a database Record Tables Attribute/Field • Records become “rows” • Attributes/fields become “columns” • Rules determine the relationship between the tables and tie the data together to form a database Basic SQL Commands • • • • • • Creating tables with CREATE Adding data with INSERT Viewing data with SELECT Removing data with DELETE Modifying data with UPDATE Destroying tables with DROP Creating tables with CREATE • Generic form CREATE TABLE tablename ( column_name data_type attributes…, column_name data_type attributes…, … ) • Table and column names can’t have spaces or be “reserved words” like TABLE, CREATE, etc. Phone Book/Contact Table CREATE TABLE contacts ( Name VARCHAR(40), Address VARCHAR(60), Company VARCHAR(60), Phone VARCHAR(11), URL VARCHAR(80), Age INT, Height FLOAT, Birthday DATE, WhenEntered TIMESTAMP ); Plan your tables very carefully! Once created, they are difficult to change! Data Types • Binary – Database specific binary objects (BLOB) • Boolean – True/False values (BOOLEAN) • Character – Fixed width (CHAR) or variable size (VARCHAR) • Numeric – Integer (INT), Real (FLOAT), Money (MONEY) • Temporal – Time (TIME), Date (DATE), Timestamp (TIMESTAMP) Adding data with INSERT • Generic Form INSERT INTO tablename (column_name,…) VALUES (value,…) Inserting a record into ‘contacts’ INSERT INTO contacts (contactid,name,address,company,phone,url,a ge,height,birthday,whenentered) VALUES (1,‘Joe’,’123 Any St.’,’ABC’, ’800-555-1212’,‘http://abc.com’,30,1.9, ’6/14/1972’, now()); Inserting a partial record INSERT INTO contacts (contactid,name,phone) VALUES (2,’Jane’,’212-555-1212’); Automatic key generation • CREATE SEQUENCE contactidseq; • Change the ContactID line in the CREATE TABLE to: ContactID INT DEFAULT nextval(‘contactidseq’) PRIMARY KEY • Or when inserting into a table INSERT contacts (contactid,name,phone) VALUES (nextval(‘contactidseq’),’Jack’, ‘716-555-1212’); Viewing data with SELECT • Generic Form SELECT column,… FROM table,… WHERE condition GROUP BY group_by_expression HAVING condition ORDER BY order_expression • The most used command • Probably the most complicated also • If used improperly, can cause very long waits because complex computations A few simple SELECTs • SELECT * FROM contacts; – Display all records in the ‘contacts’ table • SELECT contactid,name FROM contacts; – Display only the record number and names • SELECT DISTINCT url FROM contacts; – Display only one entry for every value of URL. Refining selections with WHERE • The WHERE “subclause” allows you to select records based on a condition. • SELECT * FROM contacts WHERE age<10; – Display records from contacts where age<10 • SELECT * FROM contacts WHERE age BETWEEN 18 AND 35; – Display records where age is 18-35 Additional selections • The “LIKE” condition – Allows you to look at strings that are alike • SELECT * FROM contacts WHERE name LIKE ‘J%’; – Display records where the name starts with ‘J’ • SELECT * FROM contacts WHERE url LIKE ‘%.com’; – Display records where url ends in “.com” Removing data with DELETE • Generic Form DELETE FROM table WHERE condition; DELETE FROM contacts WHERE age<13; Modifying data with UPDATE • Generic Form UPDATE table SET column=expression WHERE condition; UPDATE contacts SET company=‘AOL’ WHERE company=‘Time Warner’; Destroying tables with DROP • Generic Form DROP TABLE tablename; DROP TABLE contacts; Joining together tables • SELECT name,phone,zip FROM people, phonenumbers, address WHERE people.addressid=address.addressid AND people.id=phonenumbers.id; PhoneNumbers PhoneID Id Phone 1 1 5532 2 1 2234 3 1 3211 4 2 3421 5 3 2341 6 3 6655 People Id Name AddressID 1 Joe 1 2 Jane 2 3 Chris 3 Address AddressID Company Address Zip 1 ABC 123 12345 2 XYZ 456 14454 3 PDQ 789 14423 GROUP BY/HAVING • The “GROUP BY” clause allows you to group results together with “aggregate functions” – AVG(), COUNT(), MAX(), MIN(), SUM() – COUNT DISTINCT • HAVING allows you to search the GROUP BY results Important links • http://aws.amazon.com/documentation/ec2/ • http://www.mysql.com/