comp4 unit6c lecture slides

25
Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University, funded by the Department of Health and Human Services, Office of the National Coordinator for Health Information Technology under Award Number IU24OC000015.

Upload: health-it-workforce-curriculum-2012

Post on 06-May-2017

235 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Comp4 Unit6c Lecture Slides

Introduction to Information and Computer ScienceDatabases and SQL

Lecture cThis material (Comp4_Unit6c) was developed by Oregon Health and Science University, funded by the Department of Health

and Human Services, Office of the National Coordinator for Health Information Technology under Award Number IU24OC000015.

Page 2: Comp4 Unit6c Lecture Slides

Databases and SQLLearning Objectives

• Define and describe the purpose of databases (Lecture a)• Define a relational database (Lecture a)• Describe data modeling and normalization (Lecture b)• Describe the structured query language (SQL) (Lecture c)• Define the basic data operations for relational databases and how to

implement them in SQL (Lecture c)• Design a simple relational database and create corresponding SQL

commands (Lecture c)• Examine the structure of a healthcare database component (Lecture

d)

2Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 3: Comp4 Unit6c Lecture Slides

SQL

• Used to manage and access database information

• ANSI, ISO and IEC standard– DBMS have custom extensions!

• Extensive language– We’ll look at a few basic commands

3Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 4: Comp4 Unit6c Lecture Slides

Example

• Make a space for the tables by creating the database– create database <name>

sql> create database contacts• To remove:

– drop database <name>

4Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 5: Comp4 Unit6c Lecture Slides

Create the Tables

• create table <name> (<column information>);• Column information consists of column names

and data types• Tables also have extra information• To remove:

– drop table <name>

5Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 6: Comp4 Unit6c Lecture Slides

SQL Basic Data Types

Data Type Descriptioninteger or int Whole numbers

float Floating point number

date Date

time Time

char (length) Fixed number of characters

varchar (length) Variable number of characters

6.14 Table: SQL Basic Data Types (PD-US, 2011).

6Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 7: Comp4 Unit6c Lecture Slides

Create Company Table

sql> create table company (id integer auto_increment,

-> name varchar(50), -> address varchar(50), -> city varchar(50), -> state char(2), -> primary key(id));

7Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 8: Comp4 Unit6c Lecture Slides

Create person tablesql> create table person(id integer auto_increment,

-> first_name varchar(50), -> last_name varchar(50), -> company_id integer, -> primary key(id),

-> foreign key(company_id) references company(id));

8Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 9: Comp4 Unit6c Lecture Slides

View Tables

6.15 Figure: SQL command and output (PD-US, 2011).

9Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 10: Comp4 Unit6c Lecture Slides

View Table Columns

6.16 Figure: View table columns (PD-US, 2011).

10Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 11: Comp4 Unit6c Lecture Slides

Same Basic Operations

• Add an entry• Retrieve an entry• Delete an entry• Modify an entry

11Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 12: Comp4 Unit6c Lecture Slides

Add an Entry

• Insert into <table> (columns) values (values);

• First add company entries:sql> insert into company (name, address, city, state) -> values ('Community Hospital, Inc.', '1312 Main',

'Portland', 'OR');sql> insert into company (name, address, city, state) -> values ('Oakland Providers LLC', '14 12th St.',

'Oakland', 'CA');

12Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 13: Comp4 Unit6c Lecture Slides

Add Persons• Now add people and their associated

companies:sql> insert into person (first_name, last_name, company_id) -> values ('Bill', 'Robeson', 1);sql> insert into person (first_name, last_name, company_id) -> values ('Walter', 'Schmidt', 2);sql> insert into person (first_name, last_name, company_id) -> values ('Mary', 'Stahl', 2);sql> insert into person (first_name, last_name, company_id) -> values ('Albert', 'Brookings', 1);sql> insert into person (first_name, last_name, company_id) -> values ('Catherine', 'David', 2);

13Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 14: Comp4 Unit6c Lecture Slides

Retrieve an Entry

6.17 Figure: Retrieve an entry (PD-US).

14Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 15: Comp4 Unit6c Lecture Slides

Add Sorting

6.18 Figure: Add sorting (PD-US, 2011).

15Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 16: Comp4 Unit6c Lecture Slides

Add Selectivity

6.19 Figure: Add selectivity (PD-US, 2011).

16Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 17: Comp4 Unit6c Lecture Slides

Retrieve from Multiple Tables

6.20 Figure: Retrieve from multiple tables (PD-US, 2011).

17Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 18: Comp4 Unit6c Lecture Slides

Create a Complex SQL Statement

6.21 Figure: Create a Complex SQL Statement (PD-US, 2011).

18Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 19: Comp4 Unit6c Lecture Slides

Delete an Entry

• delete from <table> where <constraints>;

• Remove Mary from contact list:sql> delete from person where first_name='Mary' and last_name='Stahl';

1 row affected

19Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 20: Comp4 Unit6c Lecture Slides

Modify an Entry

• update <table> set <column>=<data> where <constraints>;

20Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 21: Comp4 Unit6c Lecture Slides

New Company Name

6.22 Figure: Modify company name (PD-US, 2011).

21Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 22: Comp4 Unit6c Lecture Slides

New Company Name

6.23 Figure: New company name (PD-US, 2011).

22Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 23: Comp4 Unit6c Lecture Slides

Verify Again

6.24 Figure: Verify again (PD-US, 2011).

23Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 24: Comp4 Unit6c Lecture Slides

Databases and SQLSummary – Lecture c

• SQL is a language for creating, accessing and updating databases– Create tables– Insert data– Update data– Retrieve (select) data– Delete data

24Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c

Page 25: Comp4 Unit6c Lecture Slides

Databases and SQLReferences – Lecture c

References• Chen, P. P.-S. (1976). The Entity-Relationship Model - Toward a Unified View of Data. ACM Transactions on

Database Systems, 1(1).• International Organization for Standardization. (2008). Information technology -- Database languages -- SQL (No.

ISO/IEC 9075-(1-4,9-11,13,14)).• Kent, W. (1983). A simple guide to five normal forms in relational database theory. Communications of the ACM,

26(2).

Charts, Tables, Figures:• 6.14 Table: SQL Basic Data Types (PD-US, 2011).• 6.15 Figure: View tables (PD-US, 2011). • 6.16 Figure: View table columns (PD-US, 2011). • 6.17 Figure: Retrieve an entry (PD-US, 2011). • 6.18 Figure: Add sorting (PD-US, 2011). • 6.19 Figure: Add selectivity (PD-US, 2011). • 6.20 Figure: Retrieve from multiple tables (PD-US, 2011).• 6.21 Figure: Create a Complex SQL Statement (PD-US, 2011).• 6.22 Figure: Modify company name (PD-US, 2011). • 6.23 Figure: New company name (PD-US, 2011). • 6.24 Figure: Verify again (PD-US, 2011).

25Health IT Workforce Curriculum Version 3.0/Spring 2012

Introduction to Information and Computer Science Databases and SQL

Lecture c