hap 709 – healthcare databases

43
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009

Upload: tab

Post on 26-Jan-2016

33 views

Category:

Documents


6 download

DESCRIPTION

HAP 709 – Healthcare Databases. SQL Data Manipulation Language (DML) Updated Fall, 2009. SQL Components. SQL. DCL. DDL. DML. Data I/O. RDBMS Structure. DBA Activities. C reate Record. Create/Delete DBs. Create Users. R ead Record. Delete Users. Create/Delete Tables. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: HAP 709 – Healthcare Databases

HAP 709 – Healthcare Databases

SQL Data Manipulation Language (DML)

Updated Fall, 2009

Page 2: HAP 709 – Healthcare Databases

SQL Components

SQL

DCL DDL DML

DBA Activities

Create UsersDelete UsersGrant privilegesImplement AccessSecurity

RDBMS Structure

Create/Delete DBs

Create/Delete TablesAlter Tables

Data I/O

Create Record

Read Record

Update Record

Delete Record

Page 3: HAP 709 – Healthcare Databases

SQL manipulates entire column of data

No need to repeat the commands for each record

Page 4: HAP 709 – Healthcare Databases

SQL is non-procedural

You do not need to tell the computer how to do the tasks. All you need to tell the

computer is what you want to see and the computer will figure out how to produce the

results you want to see

Page 5: HAP 709 – Healthcare Databases

Typical Commands

• Details of commands are provided online. Here we review a select few commands

• Key words are reserved for command specifications. These words cannot be used as names for fields or tables.

Page 6: HAP 709 – Healthcare Databases

Data Type Must be Specified

• Number– Integer, Small integer, Big integer, Numeric data (with

fraction), Decimal (with precision)

• String– Character, Large character, National character

• Boolean• Date/times

– Date, Time with and without time zone, Timestamp with and without time zone

• Intervals

Page 7: HAP 709 – Healthcare Databases

Data Manipulation Commands

• Combined numeric values

• Calculate intervals among dates/times

• Process a series of Boolean statements

• Concatenate strings together

Page 8: HAP 709 – Healthcare Databases

Logical Connectives

• Allows you to build complex predicates out of simple ones

• Set functions– Count, Max, Min, Sum, Avg, Stdev

• Sub-queries

Page 9: HAP 709 – Healthcare Databases

Using SQL with Microsoft Access

• Open database and select queries from objects

• Select create query in design view• Add tables and close add tables button• Choose SQL view• Delete the select statement and enter

commands you want• When finished save and enter a name for

the query

Page 10: HAP 709 – Healthcare Databases

INSERT INTO Syntax

INSERT INTO <myTable> VALUES(<Field1> <DataType>, <Field2> <DataType>,…);

Page 11: HAP 709 – Healthcare Databases

INSERT INTO Syntax

INSERT INTO <myTable> VALUES(<Field1> <DataType>, <Field2> <DataType>,…);

INSERT INTO PAT VALUES(983883,'JOHN','MARTINEZ');

Page 12: HAP 709 – Healthcare Databases

INSERT INTO in MS Access

Note: In MS Access the INSERT INTO is called an Append Query

Page 13: HAP 709 – Healthcare Databases

UPDATE Statement (1)

UPDATE <myTable> SET<Field1> = <Value1> WHERE {condition};

Page 14: HAP 709 – Healthcare Databases

UPDATE Statement (1)

UPDATE <myTable> SET<Field1> = <Value1> WHERE {condition};

UPDATE PAT SET PAT_FNM = 'JOHNNY' WHERE PAT_ID = 983883;

Page 15: HAP 709 – Healthcare Databases

MS Access Example

Page 16: HAP 709 – Healthcare Databases

Updating Multiple Records(1)

A new field needs to be populated after modification of the original table structure

Page 17: HAP 709 – Healthcare Databases

UPDATE Statement (2)

UPDATE <myTable> {join} SET<Field1> = <Value1> WHERE {condition};

Page 18: HAP 709 – Healthcare Databases

UPDATE Statement (2)

UPDATE <myTable> {join} SET<Field1> = <Value1> WHERE {condition};

UPDATE PAT INNER JOIN TEMP ON PAT.PAT_ID = TEMP.PAT_ID SET PAT.PAT_TITLE = TEMP.TITLE ;

Page 19: HAP 709 – Healthcare Databases

Multiple Updates in MS Access(1)

Page 20: HAP 709 – Healthcare Databases

Updating Multiple Records(2)

The medical procedure cost table needs to be reflect a 12.5% increase

Page 21: HAP 709 – Healthcare Databases

UPDATE Statement (3)

UPDATE <myTable> SET<Field1> = <Value1> WHERE {condition};

Page 22: HAP 709 – Healthcare Databases

UPDATE Statement (3)

UPDATE <myTable> SET<Field1> = <Value1> WHERE {condition};

UPDATE MED_PROCEDURE SET COST = 1.125 * COST ;

Page 23: HAP 709 – Healthcare Databases

Multiple Updates in MS Access(2)

Page 24: HAP 709 – Healthcare Databases

Deleting a Record

DELETE FROM <myTable> {condition};

Page 25: HAP 709 – Healthcare Databases

Deleting a Record

DELETE FROM PAT WHERE PAT_ID = 983883;

DELETE FROM <myTable> {condition};

Page 26: HAP 709 – Healthcare Databases

MS Access Example

Page 27: HAP 709 – Healthcare Databases

Reading the Data: the SELECT Statement

SELECT {fields || *} FROM <myTable> {condition};

Page 28: HAP 709 – Healthcare Databases

Reading the Data: the SELECT Statement

SELECT {fields || *} FROM <myTable> {condition};

SELECT PAT_LNM FROM PAT;

SELECT * FROM PAT;

SELECT MED_PROC_NM FROM MED_PROCEDURE WHERE COST > 20000;

Page 29: HAP 709 – Healthcare Databases

MS Access

Page 30: HAP 709 – Healthcare Databases

MS Access

Page 31: HAP 709 – Healthcare Databases

MS Access

Page 32: HAP 709 – Healthcare Databases

JoinsWho is the primary physician for patient Mary Lindfors?

Page 33: HAP 709 – Healthcare Databases

Natural Join

SELECT PAT_FNM, PAT_LNM, CLNCIAN_NM FROM PAT, CLNCIANWHERE PAT.PAT_ID = CLNCIAN.PAT_IDAND PAT_FNM = 'MARY'AND PAT_LNM = 'LINDFORS';

Who is/are the primary physician(s) for patient Mary Lindfors?

Page 34: HAP 709 – Healthcare Databases

MS Access

Page 35: HAP 709 – Healthcare Databases

Outer Joins: RIGHT JOIN

SELECT PAT.PAT_FNM, PAT.PAT_LNM, CLNCIAN.CLNCIAN_NMFROM CLNCIAN RIGHT JOIN PAT ON CLNCIAN.PAT_ID = PAT.PAT_ID;

Shows all the records from PAT and those records from CLNCIAN where the PAT_ID values are equal in both tables

Page 36: HAP 709 – Healthcare Databases

LEFT JOIN

SELECT PAT.PAT_FNM, PAT.PAT_LNM, CLNCIAN.CLNCIAN_NMFROM CLNCIAN LEFT JOIN PAT ON CLNCIAN.PAT_ID = PAT.PAT_ID;

Shows all the records from CLNCIAN and those records from PAT where the PAT_ID values are equal in both tables

Page 37: HAP 709 – Healthcare Databases

Union Operator

• The tables must have the same number of columns

• Corresponding columns must all have identical data types and lengths

• Command syntaxSelect * From <First Table name>

Union

Select * From <Second Table name>;

Page 38: HAP 709 – Healthcare Databases

Union of Two Tables

RecalledMedication in Use

Page 39: HAP 709 – Healthcare Databases

Union of Two Tables

RecalledMedication in Use

Page 40: HAP 709 – Healthcare Databases

Union of Two Tables

RecalledMedication in Use

Page 41: HAP 709 – Healthcare Databases

Intersect

• Only rows of data that appear in both source tables are selected

• Command SyntaxSelect * From <Table name>

Intersect Corresponding (<Fieldname>, <Fieldname>, …)

Select * From <Second Table name>;

Page 42: HAP 709 – Healthcare Databases

Except

• Return all rows that appear in first table but not in the second table

Select * From <Table name>

Except Corresponding (<Fieldname>, <Fieldname>, …)

Select * From <Second Table name>;

Page 43: HAP 709 – Healthcare Databases

Take Home Lessons

It is possible to write your own SQL for data manipulation