sql workshop

13
Undergraduate Economics Association 1 By: Spencer Petitti (VP of Editorial Content) Week 6: SQL

Upload: massimiliano-hasan

Post on 11-Nov-2014

120 views

Category:

Education


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: SQL Workshop

Undergraduate Economics Association

1

By: Spencer Petitti (VP of Editorial Content)

Week 6: SQL

Page 2: SQL Workshop

Undergraduate Economics Association

SQL Workshop

1. What is SQL?2. What practical applications does it have?3. Syntax and Use

2

Page 3: SQL Workshop

Undergraduate Economics Association

SQL

3

• A very high level programming language, used to access information stored on a database system

• SQL let you manipulate data easily through a series of commands

• Why use SQL?• Relational Databases vs. Flat Database

• http://office.microsoft.com/en-us/training/relational-means-data-in-multiple-tables-RZ010253998.aspx?section=3

Page 4: SQL Workshop

Undergraduate Economics Association

SQL

4

• Main ideas:• Data is stored in tables

• Fields (Columns)• Records (Rows)

Page 5: SQL Workshop

Undergraduate Economics Association

Practical Applications

5

1. MySQL, SQLite1. Relational Database Management Systems

2. Microsoft Access1. ACCDB file type

3. SAS, STATA1. .dta file type, not a database

Page 6: SQL Workshop

Undergraduate Economics Association

Practical Applications

6

1. SQLite

Page 7: SQL Workshop

Undergraduate Economics Association

Practical Applications

7

Page 8: SQL Workshop

Undergraduate Economics Association

Syntax (selecting)

8

• The Basics (the SQL query):• SELECT

• Choosing your data to manipulate or view by field (column)

• FROM• Choosing your data by table

• WHERE• The parameters of your query, will define

what results you see• http://sqlzoo.net

Page 9: SQL Workshop

Undergraduate Economics Association

Syntax (updating)

9

• A little more advanced:• UPDATE

• Choosing your data by table • SET

• Choosing your data to manipulate by field (column)

• WHERE• The parameters of your query, will define

what results you see• IF YOU DO NOT SET A WHERE CLAUSE, YOU

LOSE ALL DATA!• An update query will not show results without a select

query!

Page 10: SQL Workshop

Undergraduate Economics Association

Syntax (inserting)

10

• A little more advanced:• Two ways:

• INSERT INTO• Choosing your data entry point manipulate

by table(field,field)• SELECT

• Choosing your data by field• FROM

• Choosing your data by table

OR• INSERT INTO• VALUES ('2012',’Sydney'),('2016',’Athens'),

(‘2020’,’Beijing’);

• An update query will not show results without a select query!

Page 11: SQL Workshop

Undergraduate Economics Association

Primary Key and Foreign Key

11

• The Primary Key is a unique identifier that defines each row• Ex. Your BU ID number

• Data without an assigned Primary Key will create errors in your database• Cannot contain NULL values

• Foreign Keys point to Primary Keys in other tables, linking them together• Persons linked to Orders table, cannot create

new order without P_id link

Page 12: SQL Workshop

Undergraduate Economics Association

Syntax

12

• Google is your friend• http://stackoverflow.com• “Introduction to Access SQL”, Microsoft website

• Also: Microsoft Office training courses• http://www.w3schools.com/sql

Page 13: SQL Workshop

Undergraduate Economics Association

Upcoming:

13

• Email reports to [email protected]• The UEA will be compiling the information into a

release for the journal, each individual will be credited for their contribution