Simple Programs. 3-Tier Architectures. Databases and the Web. 3-Tier Architectures. Thin-Client 3-Tier Models. Thick-Client 3-Tier Models

Similar documents
A practical introduction to database design

CGS 3066: Spring 2017 SQL Reference

Consistency The DBMS must ensure the database will always be in a consistent state. Whenever data is modified, the database will change from one

MySQL. A practical introduction to database design

Chapter 3: Introduction to SQL

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

Networks and Web for Health Informatics (HINF 6220)

Review The Big Picture

Chapter 3: Introduction to SQL

The SQL data-definition language (DDL) allows defining :

This lab will introduce you to MySQL. Begin by logging into the class web server via SSH Secure Shell Client

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Access Intermediate

EE221 Databases Practicals Manual

Informatics 1: Data & Analysis

Module 3 MySQL Database. Database Management System

EECS 647: Introduction to Database Systems

An Incredibly Brief Introduction to Relational Databases: Appendix B - Learning Rails

Relational Database Design Part I. Announcement. Relational model: review. CPS 116 Introduction to Database Systems

CSC 3300 Homework 3 Security & Languages

Access Intermediate

CSC 453 Database Technologies. Tanu Malik DePaul University

The DBMS accepts requests for data from the application program and instructs the operating system to transfer the appropriate data.

Relational terminology. Databases - Sets & Relations. Sets. Membership

Lecture 5. Monday, September 15, 2014

MIS Database Systems Entity-Relationship Model.

The Entity-Relationship Model (ER Model) - Part 2

Database Applications

Announcements. PS 3 is out (see the usual place on the course web) Be sure to read my notes carefully Also read. Take a break around 10:15am

Databases (MariaDB/MySQL) CS401, Fall 2015

II. Review/Expansion of Definitions - Ask class for definitions

CS 146 Database Systems

Operating systems fundamentals - B07

SQL stands for Structured Query Language. SQL is the lingua franca

The Next Step: Designing DB Schema. Chapter 6: Entity-Relationship Model. The E-R Model. Identifying Entities and their Attributes.

Announcement. Relational Database Design Part I. Keys. Relational model: review. Schema vs. data. More examples of keys

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

The Entity Relationship Model

DRACULA. CSM Turner Connor Taylor, Trevor Worth June 18th, 2015

CS 582 Database Management Systems II

You can write a command to retrieve specified columns and all rows from a table, as illustrated

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

Chapter 6: Entity-Relationship Model. The Next Step: Designing DB Schema. Identifying Entities and their Attributes. The E-R Model.

Unit 27 Web Server Scripting Extended Diploma in ICT

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

Book IX. Developing Applications Rapidly

CSCC43H: Introduction to Databases

Working with Databases and Java

FIT 100 More Microsoft Access and Relational Databases Creating Views with SQL

Relational Model. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

G64DBS Database Systems. Lecture 7 SQL SELECT. The Data Dictionary. Data Dictionaries. Different Sections of SQL (DDL) Different Sections of SQL (DCL)

COMP Instructor: Dimitris Papadias WWW page:

CISC 3140 (CIS 20.2) Design & Implementation of Software Application II

Lecture 6 - More SQL

SQL Data Definition Language: Create and Change the Database Ray Lockwood

Introduction to Data Management. Lecture #4 E-R Model, Still Going

Applied Databases. Sebastian Maneth. Lecture 5 ER Model, Normal Forms. University of Edinburgh - January 30 th, 2017

Go to SQA Academy the website address is

Databases Model the Real World. The Entity- Relationship Model. Conceptual Design. Steps in Database Design. ER Model Basics. ER Model Basics (Contd.

Bruce Moore Fall 99 Internship September 23, 1999 Supervised by Dr. John P.

Create a simple database with MySQL

Physical DB Issues, Indexes, Query Optimisation. Database Systems Lecture 13 Natasha Alechina

CS121 MIDTERM REVIEW. CS121: Relational Databases Fall 2017 Lecture 13

Quick Web Development using JDeveloper 10g

The Relational Model. Week 2

MIS2502: Data Analytics SQL Getting Information Out of a Database Part 1: Basic Queries

Enterprise Reporting -- APEX

What is SQL? Toolkit for this guide. Learning SQL Using phpmyadmin

In This Lecture. Yet More SQL SELECT ORDER BY. SQL SELECT Overview. ORDER BY Example. ORDER BY Example. Yet more SQL

Relational Database Development

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Instructor: Craig Duckett. Lecture 03: Tuesday, April 3, 2018 SQL Sorting, Aggregates and Joining Tables

Lesson 13 Transcript: User-Defined Functions

COMP 430 Intro. to Database Systems. Encapsulating SQL code

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Database Management System (15ECSC208) UNIT I: Chapter 2: Relational Data Model and Relational Algebra

Simple sets of data can be expressed in a simple table, much like a

Can We Trust SQL as a Data Analytics Tool?

Chapter 1: Introduction

Introduction to Database Systems CSE 344

WHAT IS A DATABASE? There are at least six commonly known database types: flat, hierarchical, network, relational, dimensional, and object.

SQL: Structured Query Language Nested Queries

Introduction to Databases

12B. Laboratory. Databases. Objective. References. Write simple SQL queries using the Simple SQL applet.

SQL. Dean Williamson, Ph.D. Assistant Vice President Institutional Research, Effectiveness, Analysis & Accreditation Prairie View A&M University

Chapter 3. The Relational Model. Database Systems p. 61/569

CMPT 354: Database System I. Lecture 2. Relational Model

CIS 45, The Introduction. What is a database? What is data? What is information?

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CS 405G: Introduction to Database Systems

Overview of the Class and Introduction to DB schemas and queries. Lois Delcambre

3. Getting data out: database queries. Querying

Introduction to Databases

Session 6: Relational Databases

Relational Model. Topics. Relational Model. Why Study the Relational Model? Linda Wu (CMPT )

Relational Database Design Part I. Announcements (September 5) Relational model: review. CPS 116 Introduction to Database Systems

Database Systems CSE 414

ER Modeling ER Diagram ID-Dependent and Weak Entities Pg 1

Relational databases and SQL

Intro. Scheme Basics. scm> 5 5. scm>

Transcription:

3-Tier Architectures (or 3-Tier Applications) Adapted from Chuck Cusack s otes CSE 56 Simple Programs The programs you have written so far have probably had the following properties: Input/output was text-based, GUI, and/or with files Ran locally that is, they had to be downloaded to the machine on which you (or your T.A.) ran them The data had to be on the same machine as your program Were not able to be run simultaneously by more than one person. Had no interaction with other programs In the real world, most software is much more complicated than this CSE 56 2 Databases and the Web Many real applications are different than what you have programmed in two important ways They make extensive use of databases to store data They are accessible from anywhere by multiple people simultaneously via the World Wide Web Sites like amazon.com and ebay.com are entirely database driven that is, each time you go to the site, what you see is determined by data in their database As we will see, applications that use databases and are web accessible can be implemented using a 3-tier architectural model 3-Tier Architectures Definition: A 3-tier architecture is one which has a client tier, a middle tier, and a database tier. The database tier manages the database The middle tier contains most of the logic and communicates between the other tiers The client tier is the interface between the user and the system Definition: An n-tier architecture is one which has n tiers, usually including a database tier, a client tier, and n-2 tiers in between. CSE 56 3 CSE 56 4 Thin-Client 3-Tier Models The thin-client 3-tier model has these tiers: The database management system (DBMS) The main application software A web browser Examples http://cse.unl.edu/~sscott/teach http://contests.unl.edu http://ebay.com * http://amazon.com * Lotus otes Web Client * ( * These may actually be n-tier) CSE 56 5 Thick-Client 3-Tier Models The thick-client 3-tier model has these tiers: The database management system (DBMS) The main application software Some sort of interface software which must be installed on each client machine Examples: Lotus otes Desktop applets that display weather, etc. RealPlayer and other applications that download CD information from the Web CSE 56 6

Another 3-Tier Model Another common model has these tiers: The database management system (DBMS) and a persistence manager which controls all data flow into and out of the database The main application software A GUI (thin or thick) The main difference here is that the main application software is not allowed to interact directly with the database You could also think of this as a 4-tier architecture: The database management system (DBMS) A persistence manager The main application software A GUI (thin or thick) CSE 56 7 2- and n-tier Models The 2-tier model is more simple, but more limited, than a 3-tier model, and often includes The database management system (DBMS) The main application software, including GUI Here, the entire application is generally run on the client machine (certainly a thick-client) In some contexts, the 2-tier model is also know as the client-server model, where the server can be something other than a database In general an n-tier model will have The database management system (DBMS) (n-2) application layers A GUI (thin or thick) CSE 56 8 n-tier Questions The following are important questions one must ask when thinking about n-tier architectures How many tiers should be used? What tasks should be done by each tier? In other words, how exactly should the layers be divided? Should I use thin or thick clients? Should the application be web-accessible? How should connections to the database be managed? What database management system (DBMS) should be used? What languages(s), platform(s), and software should the system use? CSE 56 9 n-tier Answers The purpose of these notes is not to Present clear answers to all of the questions on the previous slide Be the authoritative source for information about n-tier architectures Make you an expert in n-tier architectures Rather, the purpose is to Introduce you to the concept of n-tier architectures Get you to start thinking about the issues involved Give you partial answers to some of the question CSE 56 0 Database Choices There are many popular database management systems (DBMSs), including IBM DB2 Oracle Microsoft SQL Server Microsoft Access MySQL Which one you should use depends on many factors, including number of expected users, size of the application and/or the database, budget, etc. Fortunately, the interfaces to these DBMSs have a lot in common, so if you learn to use one, most of what you learn is transferable to the others CSE 56 Middle Tier Choices Almost anything is possible, with some common choices being Java JSP PHP C++ Perl Visual Basic C#.ET ASP However, whether or not the client will be thin or thick will influence this choice For a thin client, the obvious middle tier choices are Java applets, JSP, PHP, ASP, and Perl Of course with all of these, HTML is involved as well CSE 56 2

Client Choices Thin clients are generally web browsers, so the important choice was made in the middle tier For thick clients, we might use Java applications C++ applications with GUI provided by MFC (Microsoft Foundation Classes) Tcl/Tk (Tool command language) GTK (Gimp ToolKit) Qt Examples You could build a 3-tier application which has Oracle DBMS, C++ middle, and C++/MFC client MySQL DBMS, PHP middle, and web browser client (http://cse.unl.edu/~sscott/teach uses these choices) IBM DB2 DBMS, JSP middle, and Java applet client MySQL DBMS, Java Applet middle, and web browser client MySQL DBMS, JSP middle, and web browser client (http://contests.unl.edu uses these choices) Microsoft SQL Server DBMS with ASP access control, ASP middle, and web browser client CSE 56 3 CSE 56 4 Our Choices In this class, we will use the following: Thin-client 3-tier architecture model Database tier: MySQL Middle tier: PHP/HTML and Java applets Client tier: Your favorite web browser It is important that you realize that Thin clients are not always the best choice Three is not always the best number of tiers Our database and language choices are not the best or only choices, but are reasonable for this class The approach we take to design and implement 3-tier applications is not the only, and not necessarily the best way References Hugh E. Williams & David Lane, Web Database Applications with PHP and MySQL, 2 nd Ed., O Reilly, 2004 Stephen McHenry, 3-Tier Architecture An Introduction, Advanced Software Technologies, Ltd. (http://www.softi.com), 997 CSE 56 5 CSE 56 6

A Brief Introduction to Relational Databases Course Roster File # Consider an instructor who wishes to store a list of his courses and students enrolled in those courses. Below is how one might store the information in a file There are several problems with this: Some information is repeated several times Some entries are incomplete It is hard to see who is in what course It is hard to see how many students or courses there are Updating student information can be problematic because of repeats Course Courseame When StudentID ame Major Adapted from Chuck Cusack s otes 56 55 30 Intro to Computer Science II Intro to Computer Science I Data Structures and Algorithms Spring 2004 Fall 2003 Fall 2003 505555555 Mary Johnson Phil Philson Mary Johnson Computer Engineering Art Computer Engineering 54320987 John Cusack Theatre CSE 56 30 30 Data Structures and Algorithms Data Structures and Algorithms Spring 2004 CSE 56 2 Fall 2003 23456789 John Smith Computer Science Course Roster File #2 Could store the information so it is a little easier to get rosters for each course as shown below There are still several problems with this format Student records are still repeated, making updating tough It is hard to see how many courses a student is enrolled for It is also hard to see how : 505555555, Phil Philson, Art many students there are total 56 Intro to Computer Science II Spring 2004 :, Mary Johnson, Computer Engineering 55 Intro to Computer Science I Fall 2003 30 Data Structures and Algorithms Fall 2003 : 23456789, John Smith, Computer Science 2:, Mary Johnson, Computer Engineering 30 Data Structures and Algorithms Spring 2004 ot Enrolled in any courses CSE 56 3 : 54320987, John Cusack, Theatre Course Roster: A Better Solution It turns out that no matter how you store the information in a simple text file, there will be problems. The bottom line is that sometimes storing things in a simple text file is not the best way A better solution would use a database to store the information. As we will see, the benefits will include Duplication is minimized Updating information is easier Getting information like lists for each course or number of courses or roster for students Getting more complex information can also be easily accomplished CSE 56 4 EnrollID 2 3 4 Course Roster Database One way to store the same information in a database is to use the following tables We will discuss the design aspect of the database later Enrollment StudentID CourseID 3 23456789 505555555 2 StudentID Firstame 23456789 John Mary 505555555 Phil 54320987 John Students Lastame Smith Johnson Philson Cusack Major Computer Science Computer Engineering Art Theatre Database Terminology Relational databases store information in a set of tables Each table has a unique name which describes what type of information is stored in the table Each column (field, attribute) of the table stores a single piece of information Each row (record) of the table represents one object One or more columns in each table are the primary key, which uniquely identifies each record, and is indicated by underlining the attribute name CourseID 2 3 4 Department Courseumber 56 55 30 30 Courses Courseame Semester Year Introduction to Computer Science II Spring 2004 Introduction to Computer Science I Fall 2003 Data Structures and Algorithms Fall 2003 CSE 56 Data Structures and Algorithms Spring 5 2004 Enrollment EnrollID StudentID CourseID 2 3 3 23456789 4 505555555 2 Students StudentID Firstame Lastame Major 23456789 John Smith Computer Science Mary Johnson Computer Engineering 505555555 Phil Philson Art CSE 56 54320987 John Cusack Theatre 6

EnrollID 2 3 4 Relationships If two entries in different tables are related in some way, foreign keys are used A foreign key is a reference to the primary key in another table We will discuss relationships in more detail later Enrollment StudentID CourseID 3 23456789 505555555 2 StudentID Firstame 23456789 John Mary 505555555 Phil 54320987 John Students Lastame Smith Johnson Philson Cusack Major Computer Science Computer Engineering Art Theatre Database Properties The order of the rows and columns is not meaningful Rows can easily be added and deleted Each attribute has a type, like integer or character or string, and can only store entries of that type Every row in a table must have a unique primary key The value of a foreign key must correspond to the value of some primary key There are often other constraints on the values of attributes, such as The value cannot be ULL. In other words, there has to be a value CourseID 2 3 4 Department Courseumber 56 55 30 30 Courses Courseame Semester Year Introduction to Computer Science II Spring 2004 Introduction to Computer Science I Fall 2003 Data Structures and Algorithms Fall 2003 CSE 56 Data Structures and Algorithms Spring 7 2004 EnrollID 2 3 4 Enrollment StudentID CourseID 3 23456789 505555555 2 Students StudentID Firstame Lastame Major 23456789 John Smith Computer Science Mary Johnson Computer Engineering 505555555 Phil Philson Art CSE 56 54320987 John Cusack Theatre 8 EnrollID 2 3 4 Database Design Each table should store a specific type of information Each attribute should be a single piece of information For instance, first names and last names, street number and street name, department and course number should be stored as two attributes, not one. If a column exists for course description, it might consist of several sentences describing the course. This is still a single thing, so it is O.K. Information should (almost) never be duplicated Results from computations should not be stored in a table Enrollment StudentID CourseID 3 23456789 505555555 2 Students StudentID Firstame Lastame Major 23456789 John Smith Computer Science Mary Johnson Computer Engineering 505555555 Phil Philson Art CSE 56 54320987 John Cusack Theatre 9 Accessing Databases To access a database, you need a database management system (DBMS), like MySQL or Microsoft Access Most DBMSs use SQL (Structured Query Language) as the interface language SQL is a (mostly) standard language that allows the user to Create, update, and delete tables Add, update, and delete single entries Query the database to extract information The details of SQL are the subject of another lecture CSE 56 0 References Hugh E. Williams & David Lane, Web Database Applications with PHP and MySQL 2nd Ed., O Reilly, 2004 CSE 56

Designing Relational Databases Stephen Scott Introduction ow that we know basic database terminology, it s time to delve into designing one The problem: given a text-based description (specification) of a design problem, come up with a concrete specification of the database and the SQL statements to implement it Where to begin???? CSE 56 CSE 56 2 The General Process. Identify the entities (tables), and the attributes (fields) that are associated with each one Kind of like naming the objects in a software design problem E.g. a winery is an object, and each has a name 2. Identify the relationships between the entities, and their cardinality E.g. a wine comes from a single winery, but one winery makes multiple wines 3. Use this information to build an initial entityrelationship (ER) model A graphical representation of our database Represents each entity, their attributes, and the relationships (including cardinality) between the entities The General Process (cont d) 4. Identify or add a primary key for each table Must be unique Many possibilities Keep it simple, if possible! 5. Label the weak entities of the ER model Entities that depend on other entities 6. Convert the ER model to SQL Start with regular entities, then the weak ones, then do the relationships: one-to-one, then one-to-many, then many-to-many CSE 56 3 CSE 56 4 An Example Specification The owner of a local scuba store wants to track his customers and their purchases. Each customer has a first and a last name and has a highest certification level (e.g. Open Water, Advanced OW) from some agency (e.g. PADI, AUI, SSI). Also, each customer has purchased a (possibly empty) set of items from {BCD, regulator, wet suit, dry suit, mask, tank, fins, snorkel}, but at most one item of each type. Each such item has a manufacturer, and multiple manufacturers make similar items. Each manufacturer has a name and address. For simplicity, assume that each manufacturer makes only one item of each type. (Also assume that store owner is insane and destroys all serial numbers and other identifying information on each product.) CSE 56 5 Step What are the entities and their attributes? CSE 56 6

Step What are the entities and their attributes?. Customer First name (string) Last name (string) 2. Certification Level (one of {Open Water, Advanced OW, }) Agency (one of {PADI, AUI, SSI, }) 3. Item Type (one of {BCD, regulator, }) 4. Manufacturer ame (string) Address (string) CSE 56 7 Step 2 What are the relationships between the entities? CSE 56 8 Step 2 What are the relationships between the entities?. A customer has a certification level Each customer has one highest level, and each level can be held by multiple customers One-to-many 2. A customer purchases items Each customer can purchase multiple items, and each item can be purchased by multiple customers E.g. each customer can own 0 or more from {BCD, regulator, }, and a regulator can be owned by multiple customers Step 2 (cont d) What are the relationships between the entities? 3. An item is made by a manufacturer Each item has only one manufacturer, but a manufacturer can make multiple items One-to-many Many-to-many CSE 56 9 CSE 56 0 Step 3: Initial ER Model Entities represented by rectangles Attributes represented by ellipses, connected to entities by lines Relationships represented by diamonds Lines connect entities to relationships, cardinality labeled by letters CSE 56 Step 3: Initial ER Model first name last name itemtype customer buys item M has makes level certification address manufacturer name agency CSE 56 2

Step 4: Primary Keys Combination of level and agency uniquely defines a certification Customer may not be uniquely identified by first and/or last name, so we ll add a customer ID (int) Even if combination is unique for each customer, a numeric ID is less prone to spelling errors when inserting, querying, etc. We ll do the same with manufacturer With respect to a particular manufacturer, type uniquely identifies item More on this later CSE 56 3 Step 4: Primary Keys first name last name customer M buys custid has level certification agency manufacid address itemtype item makes manufacturer name CSE 56 4 Step 5: Label Weak Entities Are there any entities that cannot exist without another? CSE 56 5 Step 5: Label Weak Entities Are there any entities that cannot exist without another? A manufacturer and a customer and a certification can each exist on its own An item cannot exist unless it is manufactured (but it doesn t depend on a customer) So item fully participates in the makes relation, and is connected by a double line Also, type uniquely defines an item, but only when a manufacturer is specified (since multiple manufacturers make e.g. BCDs, but each manufacturer only makes one) So item is a weak entity and gets a double box CSE 56 6 Step 5: Label Weak Entities firstname lastname customer buys M custid has level certification agency manufacid address itemtype item makes manufacturer name CSE 56 7 Step 6: Convert to SQL. Regular (non-weak) entities: CREATE TABLE customer ( customerid int OT ULL AUTO_ICREMET, firstname varchar(30) OT ULL, lastname varchar(30) OT ULL, PRIMARY KEY (customerid) ) type=myisam; CREATE TABLE manufacturer ( manufacid int OT ULL AUTO_ICREMET, name varchar(50) OT ULL, address varchar(00) OT ULL, PRIMARY KEY (manufacid) ) type=myisam; CREATE TABLE certification ( level enum( Open Water, Advanced OW, Rescue Diver,...) OT ULL, agency enum( PADI, AUI, SSI,...) OT ULL, PRIMARY KEY (level,agency) ) type=myisam; CSE 56 8

Step 6: Convert to SQL (cont d) 2. Weak entity: item is uniquely identified if manufacturer is also specified, so add manufacid as a primary key for item CREATE TABLE item ( itemtype enum( BCD, regulator, wet suit,...) OT ULL, manufacid int OT ULL, PRIMARY KEY (manufacid,itemtype) ) type=myisam; CSE 56 9 Step 6: Convert to SQL (cont d) 3. o one-to-one relationships, so do one-to-many: The idea is as follows: if a certification can be held by multiple customers, then we need to relate each customer to the held certification Easiest way is to add certification s primary key(s) to item, to serve as foreign keys: CREATE TABLE customer ( customerid int OT ULL AUTO_ICREMET, firstname varchar(30) OT ULL, lastname varchar(30) OT ULL, level enum( Open Water, Advanced OW, Rescue Diver,...) OT ULL, agency enum( PADI, AUI, SSI,...) OT ULL, PRIMARY KEY (customerid) ) type=myisam; We have a similar case for the makes relation, but in this case we don t need to update the item table, since we already added manufacid to it (since item was weak) ote that if item were not weak, then we would add manufacid as a foreign (but not primary) key, to capture the relationship CSE 56 20 Step 6: Convert to SQL (cont d) 4. Many-to-many relationships: Each item can be purchased by multiple customers, and each customer can purchase multiple items Thus we need to be able to relate each customer to multiple purchases, and vice-versa Create a new table that takes the primary keys of the related entities as its primary keys ote that since item is weak, one of its primary keys is manufacid, so we need to add that as well Step 6: Convert to SQL (cont d) 4. Many-to-many relationships (cont d): CREATE TABLE purchase ( itemtype enum( BCD, regulator, wet suit,...) OT ULL, manufacid int OT ULL, customerid int OT ULL, PRIMARY KEY (manufacid,itemtype,customerid) ) type=myisam; (Where d the AUTO_ICREMET go?) CSE 56 2 CSE 56 22 Final otes. Relationships can have attributes as well! E.g. if a purchase has a date CREATE TABLE purchase ( itemtype enum( BCD, regulator, wet suit,...) OT ULL, manufacid int OT ULL, customerid int OT ULL, dateofpurchase timestamp(8), PRIMARY KEY (manufacid,itemtype,customerid) ) type=myisam; Final otes (cont d) In ER diagram: customer M buys item (could also add date and certification number to has relationship) 2. Also, be careful about one-to-one relationships! Ask if they re really necessary dateofpurchase CSE 56 23 CSE 56 24

A Brief MySQL Primer Stephen Scott CSE 56 Introduction Once you ve designed and implemented your database, you obviously want to add data to it, then query it to process its information This process is pretty simple, once you have the syntax down, but some more complex queries require a bit of thought We ll focus on MySQL, which mostly conforms to SQL standards This will be brief; you ll have to refer to the manuals and cheat sheets for more details, e.g. for the homework CSE 56 2 Outline Getting started Creating a database and inserting data Updating and deleting data Basic queries via SELECT statement: WHERE, ORDER BY, GROUP BY, HAVIG Joins Temporary tables and nested queries CSE 56 3 Getting Started From the cse (UIX) command line: mysql -uuser -ppasswd o space after p, space optional after u The user option is optional if your account name = cse login name (typical) If you don t want to put your password on command line, use -p option without password, then you ll be prompted CSE 56 4 Getting Started (cont d) Can also load a database at startup: mysql p < file file contains commands to create tables and insert rows Can also use SOURCE command within MySQL Getting Started (cont d) Important note: You only have one database, named the same as your MySQL account name! Thus your first SQL command will be USE username; This also means that you ll have to modify some db source files, e.g. winestore.data Change DROP DATABASE winestore to a series of DROP TABLE IF EXISTS table statements CSE 56 5 CSE 56 6

Creating a Database To create a new table, use the (duh!) CREATE TABLE command: CREATE TABLE tablename ( field fieldtype [opts], PRIMARY KEY (keys) ); opts are options like OT ULL, AUTO_ICREMET, and DEFAULT Creating a Database (cont d) SHOW TABLES; will list all tables in the current database DESCRIBE table; will list table s columns CSE 56 7 CSE 56 8 Inserting Into a Database ISERT ITO tablename VALUES ( value, value2,, valuen ); Must have one-to-one correspondence with fields in the order the fields were specified in the CREATE TABLE statement Use the describe command to get the order ISERT ITO table SET field=value, field2=value2, ; Can use any subset of fields, in any order Updating and Deleting UPDATE table SET field=value, field2=value2, [WHERE condition(s)]; WHERE option specifies a subset of records (rows) to update: WHERE Firstame = John WHERE Lastame like '%Smith%' DELETE FROM table [WHERE condition(s)]; CSE 56 9 CSE 56 0 SELECT SELECT is the command you ll use most SELECT * FROM table; Shows every column and every row in table SELECT col, col2, FROM table [WHERE condition(s)]; Shows the specified columns from rows that match WHERE conditions ORDER BY Adding an ORDER BY clause to SELECT specifies the column(s) to sort the rows by Can also use DESC to sort in descending order CSE 56 CSE 56 2

GROUP BY and HAVIG GROUP BY gathers rows into sets Use this when using MI, MAX, AVG, etc. SELECT city, MI(birth_date) FROM customer GROUP BY city; Prints each city, and lists the birth date of oldest person in that city Can also use HAVIG clause to further condition inclusion in groups Use HAVIG only with GROUP BY, never as a substitute for WHERE WDBA, p. 56-7 Joins SELECT is fine, but if we have a relational database, shouldn t we relate things? E.g. say we want to list all the items manufactured by AquaLung This information is in the database, but in separate tables: manufacturer and item Could use a SELECT to get AquaLung s manufacid, then use it in a second SELECT in item table to get the list: SELECT manufacid FROM manufacturer WHERE name= AquaLung ; [let this number be x] SELECT itemtype FROM item WHERE manufacid=x; CSE 56 3 CSE 56 4 Joins (cont d) Is there a better way??? YES!!! List both tables in the SELECT, and join them in the WHERE clause: SELECT itemtype FROM manufacturer, item WHERE manufacturer.name= AquaLung AD item.manufacid=manufacturer.manufacid; otes: Can drop the table name in front of. if field name unique Can use > 2 tables The last part of the WHERE is very important!! CSE 56 5 Beware the Cartesian Product! Recall that the Cartesian product of sets A={a,b,c} and B={d,e} is the set of all pairs of something from A and something from B: A x B = {(a,d),(b,d),(c,d),(a,e),(b,e),(c,e)} A join of two tables A and B generates a new table whose rows are from the Cartesian product of A and B So if table A has 00 rows and table B has 50 rows, the join has 50 x 00 = 5000 rows, most of which are worthless (e.g. the manufacid s don t match, so rows are unrelated) Thus we add a WHERE to this to filter out rows that are not related CSE 56 6 Temporary Tables Some tasks cannot be done in a single query E.g. you use a join to collect data across multiple orders, then sum it up, and want to find the max of all the sums CREATE TEMPORARY TABLE tmpname SELECT f, f2, SUM(quantity) AS sum FROM WHERE ; SELECT MAX(sum) FROM tmpname WHERE ; CSE 56 7 ested Queries More recent feature of MySQL Can use a query result as part of a WHERE condition E.g. to list the full name of the customer who maximizes ordercount, can do: SELECT max(ordercount) AS max FROM table; [returns 60] SELECT lastname, firstname, ordercount AS max FROM table WHERE ordercount = 60; Or: SELECT lastname, firstname, ordercount AS max FROM table WHERE ordercount=(select MAX(ordercount) FROM table); CSE 56 8