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.
Databases and SQL
Learning 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)
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
2
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
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
3
Example
• Make a space for the tables by creating the
database
– create database <name>
sql> create database contacts
• To remove:
– drop database <name>
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
4
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>
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
5
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).
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
6
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));
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
7
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));
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
8
View Tables
6.15 Figure: SQL command and output (PD-US, 2011).
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
9
View Table Columns
6.16 Figure: View table columns (PD-US, 2011).
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
10
Same Basic Operations
•
•
•
•
Add an entry
Retrieve an entry
Delete an entry
Modify an entry
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
11
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');
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
12
Add Persons
• Now add people and their associated
companies:
sql> insert into person (first_name, last_name,
-> values ('Bill', 'Robeson', 1);
sql> insert into person (first_name, last_name,
-> values ('Walter', 'Schmidt', 2);
sql> insert into person (first_name, last_name,
-> values ('Mary', 'Stahl', 2);
sql> insert into person (first_name, last_name,
-> values ('Albert', 'Brookings', 1);
sql> insert into person (first_name, last_name,
-> values ('Catherine', 'David', 2);
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
company_id)
company_id)
company_id)
company_id)
company_id)
13
Retrieve an Entry
6.17 Figure: Retrieve an entry (PD-US).
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
14
Add Sorting
6.18 Figure: Add sorting (PD-US, 2011).
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
15
Add Selectivity
6.19 Figure: Add selectivity (PD-US, 2011).
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
16
Retrieve from Multiple Tables
6.20 Figure: Retrieve from multiple tables (PD-US, 2011).
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
17
Create a Complex SQL Statement
6.21 Figure: Create a Complex SQL Statement (PD-US, 2011).
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
18
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
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
19
Modify an Entry
• update <table> set <column>=<data> where
<constraints>;
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
20
New Company Name
6.22 Figure: Modify company name (PD-US, 2011).
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
21
New Company Name
6.23 Figure: New company name (PD-US, 2011).
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
22
Verify Again
6.24 Figure: Verify again (PD-US, 2011).
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
23
Databases and SQL
Summary – Lecture c
• SQL is a language for creating, accessing and
updating databases
– Create tables
– Insert data
– Update data
– Retrieve (select) data
– Delete data
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
24
Databases and SQL
References – 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).
Health IT Workforce Curriculum
Version 3.0/Spring 2012
Introduction to Information and Computer Science
Databases and SQL
Lecture c
25
Descargar

comp4_unit6c_lecture_slides