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.

Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

Embed Size (px)

Citation preview

Page 1: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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.

Page 2: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

SQL Basic Data Types

Data Type Description

integer 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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

Create person table

sql> 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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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: Introduction to Information and Computer Science Databases and SQL Lecture c This material (Comp4_Unit6c) was developed by Oregon Health and Science University,

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