Presentation is loading. Please wait.

Presentation is loading. Please wait.

NURS6803 Clinical DB Design Katherine Sward, PhD, RN

Similar presentations


Presentation on theme: "NURS6803 Clinical DB Design Katherine Sward, PhD, RN"— Presentation transcript:

1 NURS6803 Clinical DB Design Katherine Sward, PhD, RN
Even more SQL… NURS6803 Clinical DB Design Katherine Sward, PhD, RN 1

2 Union queries Up to this point we joined tables horizontally, with additional columns select demog.pt_id, name, rbc, wbc from demog, blood where demog.pt_id = blood.pt_id pt_id name rbc wbc 10 Brown, Mike 5.62 9.4 102 Costner, Kevin 3.32 16 3.55 12 103 Cole, Nat-King 4.39 5.8 3.36 3.7 104 Mason, Perry 4.05 7.5 105 Go, King-Fung 5 109 Bond, James 4.24

3 Union queries student (unid, lastname, firstname, email, major)
faculty (unid, lastname, firstname, , department) Get a list of unid, name, and for all students and faculty Process: create a query for each table, then paste the results together vertically into a single list that is a UNION query

4 Union queries SELECT unid, lastname, firstname , ‘student’ as PersonType FROM students UNION SELECT unid, lastname, firstname, , ‘faculty’ as PersonType FROM faculty; unid lastname firstname PersonType u Smith Sam student u Jones Joe u Anders Sally u Williams Greg faculty Dodge Kathy

5 Union queries Queries must be “Union compatible”
return same structure – same number of columns, matching data types (convert if necessary) columns should conceptually match

6 Self join Recursive relationship: there is a relationship between different rows in the same table

7 Table data employeeID name dateOfHire managerID u0000001 Bossman, Bob
1/1/2005 u Overseer, Susie 1/2/2005 u Worker, William 2/1/2005 u Smith, Sam u Jones, Jenny 8/15/2010 u Little, Larry 8/21/2011 u Mann, Macho 7/17/2006 u Pipsqueak, Pippy 4/15/2007 u Doer, Danny 6/6/2006 u Jones, Jeff 2/8/2009

8 Self join Who are Susie’s employees? or
Show the ID and name for an employee, and the name of the employee’s manager

9 employeeID name dateOfHire managerID u Bossman, Bob 1/1/2005 u Overseer, Susie 1/2/2005 u Worker, William 2/1/2005 u Smith, Sam u Jones, Jenny 8/15/2010 u Little, Larry 8/21/2011 u Mann, Macho 7/17/2006 u Pipsqueak, Pippy 4/15/2007 u Doer, Danny 6/6/2006 u Jones, Jeff 2/8/2009

10 Self join Process: pretend there are two copies of the table
Use the table name twice Alias at least one of the copies Join the tables (managerID as FOREIGN KEY) Employee (Copy 1: Emp) employeeID name dateOfHire managerID Employee (Copy 2: Mgr) employeeID name dateOfHire managerID M

11 Self join SELECT EMP.employeeID, EMP.name, MGR.name
FROM employee as EMP, employee as MGR WHERE EMP.managerID = MGR.employeeID employeeID EMP.name MGR.name u Overseer, Susie Bossman, Bob u Worker, William u Little, Larry u Mann, Macho u Jones, Jeff u Smith, Sam u Jones, Jenny u Pipsqueak, Pippy u Doer, Danny

12 Multiple relationships
Normally there is ONE relationship between two tables (that goes both directions) Occasionally there is actually two relationships between the tables Different “roles”

13 Multiple relationships
Suppose you had a database tracking baseball games You have a “Teams” table and a “games” table A team can participate in a game as the “home” team, OR a team can participate in a game as the “away” team (but never both at the same time). These are two separate relationships

14 Table structure Team (TeamID, TeamName, Mascot)
BallGame (GameID, GameDate, Location, HomeTeamID, HomeScore, AwayTeamID, AwayScore) To find the names for teams participating in a game, you need to get one row of data from the Teams table to get the name of the home team, and a different row of data in the teams table to get the name of the away team.

15 Process Like with did with self join
Pretend like the Team table exists twice

16 Ballgame GameID GameDate Location HomeTeamID HomeScore AwayTeamID AwayScore Team TeamID TeamName Mascot Team TeamID TeamName Mascot

17 The query Select GameID, GameDate, H.TeamName As HomeTeam,
A. TeamName as AwayTeam From BallGame AS G Team AS H Team AS A Where G.HomeTeamID = H.TeamID  link the home copy and G. AwayTeamID = A.TeamID;  link the away copy

18 Multiple functions Let’s look at the demog table in the SNDB. It’s structure is Demog (pt_id, name, zip, gender, race). We need to know what’s the highest pt_id in the table. However, every column in this table was created as a TEXT field. So we can’t just sort the table and scan for the last entry – the data will sort alphabetically, so 1001 will show up early in the list, but 999 will show up late in the list.

19 Multiple functions We can use a data type conversion function to pretend that the pt_id is a number. (Like we did with sorting data). Cint(pt_id) or to_number(pt_id) Access Oracle

20 Multiple functions Then once we have it as a number, we can find the maximum value using the max() function Those functions can be nested together. Just be careful to do the parentheses correctly. SELECT max(cint(pt_id)) from demog; If I run this, I see that the highest pt_id in my table is 1255 (your copy may differ slightly).

21 finally READ the description of other DML query types
Insert (add row of data) Update (change existing row) Delete (get rid of a row of data)


Download ppt "NURS6803 Clinical DB Design Katherine Sward, PhD, RN"

Similar presentations


Ads by Google