microsoft access / sql tutorial part i - math.ucla.eduactuary/meetingsandworkshops/2017... ·...

21
Bruin Actuarial Society | University of California, Los Angeles | 2017 ManYee Yu President Brittani Kellogg Vice President Dan Sui Director of Professional Development Greta Xiong Corporate Liaison Cullen Im Corporate Liaison Annie Thornton Director of Finance Microsoft Access / SQL Tutorial Part I Presented by Bruin Actuarial Society in Winter 2017 as part of the Technical Series Workshops at UCLA

Upload: ngokhanh

Post on 31-Mar-2018

215 views

Category:

Documents


1 download

TRANSCRIPT

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

Microsoft Access / SQL

Tutorial Part I

Presented by Bruin Actuarial Society in Winter 2017 as part of the Technical Series Workshops at UCLA

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

First Things First SQL (pronounced sequel) is a language that we use to interact with databases SQL stands for Structured Query Language What’s a database? A database is a set of data tables (spreadsheets with rows and columns). Imagine an Excel workbook on steroids.

Data Storage Systems Data Storage System: A structure used to add, delete, update and retrieve data SQL statements are entered into a computer to communicate with a database The statement is delivered to and executed by the server Oracle Database and Teradata are examples of SQL platforms used in industry We’ll be using Microsoft Access

Our Business

Let’s say the whole actuary thing doesn’t work out for any of us. Our backup? Being an accountant? Nah. Opening our own café. Any business has lots of data to keep track of, and a database is the way to do it Open CaféDatabase.accdb Your job: Every business has a database, and every database has a manager.

Overview of Access

List of All Access Objects on the left (Tables, Queries, Forms and Reports) Double click an object to open Once an object is open, you can right click on its tab to change View Tables have two views: Datasheet and Design Queries have three views: Datasheet, Design and SQL

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

The MENU table contains information on the items and drinks we’ll be selling.

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

The EMPLOYEE table contains info on our employees.

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

The LOCATIONS table contains info on each of the locations we have opened across the country.

The SELECT Statement

SELECT is used to recall data from the data tables. SELECT ItemName, Price, Calories FROM Menu; ItemName, Price and Calories are columns in the Menu table

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

SELECT * FROM Menu; The asterisk represents all columns.

SELECT DISTINCT Type FROM Menu; This shows us all the different kinds of items found on our menu.

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

The WHERE Clause

The WHERE clause is attached to SELECT statements to limit the rows from which the computer will recall data. For health-conscious hipsters who want to watch their caloric intake, SELECT ItemName, Calories FROM Menu WHERE Calories < 200;

Let’s say this hipster is broke, too. SELECT ItemName, Calories, Price FROM Menu WHERE Calories < 200 AND Price < 2.50; OR is also valid to use.

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

For customers who want an espresso and a baked good, SELECT ItemName, Price FROM Menu WHERE Type IN (‘Espresso’ , ‘Baked’); NOT IN is also valid to use.

The KEY to it all

Every data table has a column whose entries serve as the PRIMARY KEY of the table Every row will have a unique PRIMARY KEY. Our student ID numbers can be thought of as primary keys for UCLA students. SELECT * FROM Menu;

ItemNo is the primary key for the Menu table

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

A FOREIGN concept

Tables may also have Foreign Keys; these make references to rows in other tables SELECT * FROM Locations;

Manager is a foreign key in Locations referring to ItemNo in Employees SELECT * FROM Employees;

Foreign Keys cannot point to nothing! A Manager in Locations cannot be a number that is not in EmpNo in Employees. This is called referential integrity.

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

NULL and void

Null values are cells that are blank in the data table They aren’t the same as zeros or blank spaces! Primary Keys cannot be null SELECT ItemName, Type, Price, Calories FROM Menu WHERE Type = ‘Espresso’; The Dark Chocolate Latte is a new item; we haven’t set its price yet. It’s a null value (different from $0.00, or free!)

But we can arrange for null values to be treated as zeros SELECT ItemName, Type, NZ(Price, 0) AS Price2, Calories FROM Menu WHERE Type = ‘Espresso’;

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

Smooth Operators

Coffee beans just became more expensive due to war in <insert random country>. SELECT ItemName, Price AS OldPrice, 1.15*Price AS NewPrice FROM Menu WHERE Type IN (‘Coffee’, ‘Iced’);

The arithmetic operators: + - * / % The comparison operators = > >= < <= can be used in the WHERE clause Fields can be concatenated with the + sign. SELECT FirstName + ‘ ‘ + LastName AS Name FROM Employees;

For all employees whose last names start with the letter J SELECT FirstName, LastName FROM Employees where LastName LIKE ‘J*’;

* represents any combination of letters ? represents a single letter

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

Functions

We can concatenate strings using the ampersand: SELECT FirstName & ‘ ‘ & LastName AS Name FROM Employees;

We can extract substrings: SELECT MID(FirstName,1,3) AS Abbrev, LastName, Title FROM Employees;

We can find the number of characters in a string: SELECT FirstName, LEN(FirstName) as LengthofFirstName FROM Employees;

Et cetera: INSTR(‘MISSISSIPPI’,’S’) = 3

ROUND (45.926 , 2) = 45.93 1600 MOD 300 = 100

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

On a Case-by-case Basis

SQL has Case-Conversion Functions UCASE(‘bob’) = “BOB’ LCASE(‘BOB’) = ‘bob’ StrConv(‘boB’,3) = ‘Bob’ (StrConv works like UCASE is the second parameter is a 1 and LCASE if it is a 2)

A GROUP Effort

SQL features group functions, i.e., functions that operate across multiple rows. SELECT Title, AVG (PayRate) AS AveragePayRate FROM Employees GROUP BY Title;

COUNT (), MAX (), MIN (), SUM () are also group functions.

HAVING a good time yet?

To restrict the groups that we select, we use the HAVING clause. (The WHERE clause is not permitted with GROUP BY.) SELECT Type, AVG (Calories) AS AverageCalories FROM Menu GROUP BY Type HAVING Type IN ('Coffee','Espresso','Iced','Hot Tea');

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

That’s all for tonight, folks. Part II of the Microsoft Access/SQL Tutorial will cover the following topics: 1. Inner, left, right, and outer joins 2. Switch statements 3. Subqueries 4. How to create, update and delete tables, columns and rows See you on Thursday!

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

Microsoft Access / SQL

Tutorial Part II

Presented by Bruin Actuarial Society in Winter 2017 as part of the Technical Series Workshops at UCLA

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

JOINing Together

Use joins to display data from more than one data table. Let’s say that we need employee names together with their location names (The Employee table contains location numbers, but not location names) SELECT * FROM Employees INNER JOIN Locations ON Employees.LocationNo = Locations.LocationNo;

Why do we use dot notation? To prevent ambiguity, the table name must accompany column names that appear in multiple tables.

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

Putting the Cartesian Product before the Horse

Why do we have to use INNER JOIN? Let’s see what happens if we don’t. SELECT * FROM Employees, Locations;

Whoa! What happened? To the server, FROM Employees, Locations denotes the Cartesian Product of these two tables. Every row in Employee is matched with every row in Location, but we only need the rows where LocationNo matches.

Venn Aquí

In a normal inner join, what gets excluded? Locations without managers and employees without a location.

How do we show the new Location for which we don’t have a manager yet? SELECT * FROM Locations LEFT JOIN Employees on Employees.EmpNo = Locations.Manager;

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

LEFT JOIN means that Locations without corresponding Employees (Managers) will still be included. RIGHT JOIN also works. For a full outer join: SELECT * FROM Locations LEFT JOIN Employees on Employees.EmpNo = Locations.Manager UNION SELECT * FROM Locations RIGHT JOIN Employees on Employees.EmpNo = Locations.Manager; If you’re confused as to why these joins are called LEFT, RIGHT, INNER and outer, imagine a Venn diagram.

Switching it Up

The SWITCH statement works like an IF-ELSE statement. Let’s say there has been upheaval in the commodities markets. Coffee prices must rise by 5%, and baked goods prices by 7%. SELECT ItemName, Type, Price AS OldPrice, SWITCH(Type = 'Coffee', Price*1.05, Type = 'Iced', Price*1.05, Type = 'Baked', Price*1.07) AS NewPrice FROM Menu;

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

SUBQUERY

Subqueries are SELECT statements in the WHERE clause of another SELECT statement. What drinks have higher prices than a Danish? Is a query. Well, what’s the price of a Danish? <— This is the subquery. SELECT ItemName, Price FROM Menu WHERE Price >= (SELECT Price FROM Menu WHERE ItemName = 'Danish');

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

Back to Square One: Creating Database Objects

CREATE TABLE Employees2( EmpNo INTEGER,

FirstName TEXT, LastName TEXT, Title TEXT, PayRate CURRENCY, Location BYTE, CONSTRAINT Employees_EmpNo_pk PRIMARY KEY (EmpNo), CONSTRAINT Employees_LocationNo_fk FOREIGN KEY (Location) REFERENCES Locations (LocationNo)

);

DML Statements: Database Manipulation Language We’re opening a new location! Let’s add it to our Locations table. INSERT INTO Locations (LocationNo, LocationName, County, State, ZIP) VALUES (9, "San_Francisco" , "San_Francisco" , "CA" , 94103);

Bruin Actuarial Society | University of California, Los Angeles | 2017

ManYee Yu President

Brittani Kellogg Vice President

Dan Sui Director of Professional

Development

Greta Xiong Corporate Liaison

Cullen Im Corporate Liaison

Annie Thornton Director of Finance

We hired a manager for this new location. We need to add him to our roster of employees. INSERT INTO Employees VALUES (1031, “John” , “Doe” , “Manager” , 22 , 9);

UPDATE Locations SET Manager = 1031 WHERE LocationNo = 9;

A Philz Coffee opens next door and takes all of our customers, and we have to close. DELETE FROM Locations WHERE LocationNo = 9;

FIN