Chapter 6 Logical Database Design and the Relational Model

advertisement
IST 331.
Fall 2000
V. Matos
Project – Part 3
Physical Database Modeling (PDM)
In this phase of the project you will create a Physical Database Model for the Mountain View Community Hospital.
A PDM Diagram is attached. The diagram shows the tables, their attributes, keys, and the inter-table relationships
derived from the Primary/Foreign key connections.
For each table you should create a script similar to the one below. The script contains the code to generates and
populate a table in a SQL environment (particularly in Oracle). Your DBMS should be MS-Access. Certain
datatypes must be corrected, such as TEXT instead of VARCHAR2.
Due date: Th. Oct 26. (Give your instructor a listing of your scripts.)
/*----------------------------------------------------------------------*
MakeWORK.SQL
Victor Matos. 21-oct-94
*
This command file implements the following actions:
*
1 - creates the WORKSON table using ORACLE DBMS
*
2 - populates the WORKSON table inserting several records
*
3 - shows the status of the table after insertions are performed
*-----------------------------------------------------------------------*/
drop table WORKS_ON;
create table WORKS_ON (
MS-Access
essn
varchar2(9),
uses the Text
pno
number,
data type
hours
number(5,1) );
insert into WORKS_ON values ( 123456789, 1, 32.5 );
insert into WORKS_ON values ( 123456789, 2, 7.5 );
Running the Scripts in MS Access
Put the following code in an Access Module.
Public Sub makeTemp()
'The example shows the execution of SQL action queries
'this subroutine could be called from the form: myMakeTables
Dim myDB As DAO.Database
Dim mySQL As String
On Error Resume Next 'ignore errors
'connect to the current MS-Access database
Set myDB = CurrentDb()
'assemble and execute SQL statements
'erase previous versions of the TEMP table
mySQL = "drop table TEMP"
myDB.Execute mySQL
'create a new image of the TEMP table
mySQL = "create table TEMP (theKey number, theDescription text)"
myDB.Execute mySQL
'populate the TEMP table
mySQL = "insert into TEMP values (123, 'ABC')"
myDB.Execute mySQL
End Sub
Public Sub makeTempVersion2()
'An example to show the execution of SQL action queries
'this subroutine reads a script file containing the SQL
'code required to create and populate the TEMP table.
Dim myDB As DAO.Database
Dim mySQLLine As String
'On Error Resume Next 'ignore errors
'connect to the current MS-Access database
Set myDB = CurrentDb()
'open the disk file – assume each command fits in one line
'(each complete SQL line ends with a semicolon)
Open "c:\Temp\mySQLScript.txt" For Input As #1 ' Open file.
Do While Not EOF(1) ' Loop until end of file.
Line Input #1, mySQLLine ' Read line into variable.
MsgBox mySQLLine
myDB.Execute mySQLLine
Loop
Close #1
' Close file.
End Sub
Private Sub cmdVersion2_Click()
'invoke routine which reads the script file
'containing the SQL statements
Call makeTempVersion2
MsgBox "Done (version2) !!!"
End Sub
The file looks like:
drop table TEMP;
create table TEMP (theKey number, theDescription text);
insert into TEMP values (456, 'XYZ');
NEW. FOR ACCESS 2000 USERS !!!
The preferred way of programming VBA in Access2000 is based on the ADODB control –rather than DAO.
Chapter 12 of the reference book (Mastering Microsoft 2000 Development by Alison Balter) explains how to
program the ADODB hierarchy.
A fragment follows to illustrate the way of programmatically manage the database resources using the Connection,
and the Recordset objects of the ADODB control.
Example1. Executing an Action Query.
Sub MakeTable1( )
Dim mySQL as String
Dim myConn as ADODB.Connection
Set myConn = New ADODB.Connection
MyConn.open CurrentProject.Connection
mySQL = “create table JUNK(theID as number, theName as Text)”
MyConn .Execute mySQL
MyConn.Close
End Sub
Example2. Creating a recordset using a connection string
Sub CreateRS2( )
Dim myRS as ADODB.Recordset
Dim mySQL as String
Set myRS = New ADODB.Recordset
MySQL = “Select * from Employee where sex = ‘F’ “
MyRS.open mySQL, CurrentProject.Connection
While not myRS.EOF
Debug.Print myRS(“Lname”) & “ “ & myRS(“Fname”)
MyRS.MoveNext
Wend
MyRS.Close
Set myRS = Nothing
End Sub
Chapter 6 Logical Database Design and the Relational Model
Relational schemas for Mountain View Community Hospital.
WARD
Ward_No
Ward_Name
Employee_No
ASSIGNED
Ward_No
Employee_No
Hours
EMPLOYEE
Employee_No
Employee_Name
BED
Bed_No
Ward_No
Room_No Patient_No
PATIENT
Patient_No
Patient_Name Physician_ID
PERFORMS
Patient_No
Physician_ID
Treatment_No
PHYSICIAN
Physician_ID
TREATMENT
Physician_Name
CONSUMES
Patient_No
Item_No
Date
Quantity
ITEM
Item_No
Results
Description Unit_Cost
Treatment_No
Treatment_Name
a.
Schema for EER diagram (Exercise 1, Chapter4):
PERSON
Person_ID
Name
Address
Birth_Date
City_State_Zip
PATIENT
PA_Person_ID
Contact_Date
PH_Person_ID
PHYSICIAN
PH_Person_ID
Pager_No
Specialty
VOLUNTEER
V_Person_ID
Skill
EMPLOYEE
E_Person_ID
Date_Hired
NURSE
N_Person_ID
Certificate
Name
STAFF
S_Person_ID
Job_Class
TECHNICIAN
T_Person_ID
Skill
LAB ASSIGN
T_Person_ID
Name
LABORATORY
Name
Location
CARE CENTER
Name
Location
Phone
To PATIENT
RESIDENT
R_Person_ID
Date_Admitted
OUTPATIENT
O_Person_ID
(Other)
BED
Bed_No
Room_No
R_Person_ID
VISIT
O_Person_ID
Date
Comments
Following are some sample CREATE TABLE commands.
CREATE TABLE PATIENT
(PATIENT_NO
PATIENT_NAME
PHYSICIAN_ID
PRIMARY KEY (PATIENT_NO),
FOREIGN KEY (PHYSICIAN_ID)
(PHYSICIAN_ID));
INTEGER
VARCHAR(25)
VARCHAR(10)
Not Null,
Not Null,
Not Null,
REFERENCES PHYSICIAN
CREATE TABLE PHYSICIAN
(PHYSICIAN_ID
INTEGER
PHYSICIAN_NAME
VARCHAR(25)
PRIMARY KEY (PHYSICIAN_ID));
Not Null,
Not Null,
Download