Oracle SQL Developer - How to add constraints to er diagram - oracle

I need to create an ER diagram using oracle SQL developer, I have created it, but I am struggling to add the constraints, does anyone have any advice on how to do this?
I am developing a hospital data model. Thanks

So reconsider your entities Think of it in terms of What "physical objects do you have and what can those objects do"
Start off by identifying them all and then combining like ones.
For example staff, dr, GP and patient are all "People" and share like information just There are just different "Types" of people so combine them!
Each ward has staff on it so you have People, Ward, and WardStaff
Each person could have address information.
A Person can be admitted to a ward
A persons who has been admitted can be treated by multiple drs and have multiple ailments
Remember if you have to update information in more than one place to keep it accurate it needs to be consolidated.
Consider Cardinality between the entities: Does a ward HAVE to have nurses assigned? is a Nurce always on a ward? Can a ward have zero, one or many nurses?
Can a patient have zero one or many ailments?
Can a dr have zero one or many patients?
Are dr's limited to wards?
Do Dr's have specialties too? (can they have more than one?)
Here's the entities I see after that read:
Ward
WardType
WardStaff
Admittance
Person
PersonType
Address
PatentAdmittanceAilments
AilmentType
TreatmentType
And then here's how I see them relate. Re-read the 4 pages and see if this looks right. ask what's wrong and ask what's missing. and is there too much?
Wards
WardID (int) PK
Name (varchar(10))
WardTypeID (int)
WardStaff
WardID (int) PK
StaffID (Varchar(6)) PK (Unique Constraint) as a nurse can only work in 1 ward
LeadEffective Date Shows when the nurse became lead of ward
LeadNurseID (varchar(6)) FK to PersonID
Admittance
AdmittanceID (int) PK
PatientID (VARCHAR(6))
WardID (Int) FK to Ward
AdmittanceDate (date)
DischargeDate (date)
Person
PersonID (varchar(6)) PK
PersonTypeID (Integer)
Name (varchar(30))
DOB Date (Date)
GPID (6,0)
AddressID (int) FK to Address
Address
AddressID (Int) PK
Address# (varchar(10))
BuildingName (varchar(30))
Unit# (varchar(10))
City (Varchar(50))
Street (varchar(50))
State (varchar(02))
Country (varchar(10))
ZipCode (varchar(10))
PatientAdmittanceAilments
AdmittanceID (Int) PK_1of3
DrID (varchar(06)) FK to Person
AilmentID (int) FK to Ailment PK2of3 A trigger needs ot be created to
ensure a compliantID
AilmentAdditional(varchar(40)) doesn't overlap the DTS/DTE
TreatmentID (int) FK to Treatment
TreatmentAdditional(varchar(40))
DTS (Date) PK3of3
DTE (Date)
PersonType
PersonTypeID (Int) PK
Description (Varchar(30)) (Examples: Staff, Patient, Dr, GP)
WardType
WardTypeID (int) PK
Description (varchar(20)) (Examples: Orthopaedic, geriatric...)
AilmentType
AilmentTypeID (int) PK
Description (varchar(40))
Treatments
TreatmentTypeID (int) PK
Description (varchar(40))

Related

Find the names of students who are not enrolled in any course - Students, Faculty, Courses, Offerings, Enrolled

Given the database below, project the names of the students who are not enrolled in a course using relational algebra.
Students(snum, sname, major, standing, age, gpa)
Faculty(fid, fname, deptid)
Courses(cnum, cname, course_level, credits)
Offerings(onum, cnum, day, starttime, endtime, room, max_occupancy, fid)
Enrolled(snum, onum)
I can get the snum of all students not enrolled in a course with:
π snum Students - π snum Enrolled
But how do I project the sname of the student with the snums that I find?
Every base table holds the rows that make a true proposition (statement) from some (characteristic) predicate (statement template parameterized by columns). The designer gives the predicates. The users keep the tables updated.
-- rows where student [snum] is named [sname] and has major [major] and ...
Students
-- rows where student [snum] is enrolled in offering [onum]
Enrolled
Every query result holds the rows that make a true proposition from some predicate. The predicate of a relation expression is combined from the predicates of its argument expressions depending on its predicate nonterminal. The DBMS evaluates the result.
/* rows where
student [snum] is named [sname] and has major [major] and ...
AND student [snum] is enrolled in offering [onum]
*/
Student ⨝ Enrolled
AND gives NATURAL JOIN, ANDcondition gives RESTRICTcondition, EXISTScolumns gives PROJECTother columns. OR & AND NOT with the same columns on both sides give OR & MINUS. Etc.
/* rows where
THERE EXISTS sname, major, standing, age & gpa SUCH THAT
student [snum] is named [sname] and has major [major] and ...
*/
π snum Students
/* rows where
THERE EXISTS onum SUCH THAT
student [snum] is enrolled in offering [onum]
*/
π snum Enrolled
/* rows where
( THERE EXISTS sname, major, standing, age & gpa SUCH THAT
student [snum] is named [sname] and has major [major] and ...
AND NOT
THERE EXISTS onum SUCH THAT
student [snum] is enrolled in offering [onum]
)
AND student [snum] is named [sname] and has major [major] and ...
*/
(π snum Students - π snum Enrolled) ⨝ Students
You can project out any columns that you don't want from that.
(Notice that we don't need to know constraints to query.)
Relational algebra for banking scenario
Forming a relational algebra query from an English description
Is there any rule of thumb to construct SQL query from a human-readable description?

Use two different relational algebra to do a query

Here is the relation schema:
MANUFACTURER (Name, Country, Contact_Person, Phone)
CAR (Plates, Manufacturer, Model, Year, Class)
MAINTENANCE (RepairNo, Car, RepairDate, Procedure, Mileage, TotalTime) CUSTOMER (CustNo, Name, Address, Phone)
RENTAL (Car, Customer, StartDate, ReturnDate, Cost)
I want to use 2 different relational algebra to query that show all car models produced by manufacturers from Italy,
The 2 solutions that I came up with are
And
Please let me know if I'm wrong about this

Relational Database - Finding instructors who taught the most courses in 2009

We have the following schema:
instructor(ID, name, dept name, salary)
teaches(ID, course id, sec id, semester, year)
Find instructors who taught the most courses in 2009. Can someone please help me? I'm confused how to write this out in relational algebra.
This must be homework ;-) So I'll give you some hints...
Since I haven't done tuple relational calculus since college (http://en.wikipedia.org/wiki/Relational_algebra), here is an approximation in sql,
select instructor.ID, instructor.name, count(teaches.ID)
from instructor
join teaches on teaches.ID = instructor.ID
and count(teaches.ID) >= ...
group by ...
Leaving you to fill in the group by and >= values.
Think about how you calculate how many courses each teacher teaches,
select teaches.ID, count(*)
from teaches
group by teaches.ID
This might help: MySQL count maximum number of rows

Where two or more values match condition?

I have been asked this question;
You list county names and the surnames of the representatives if the representatives in the counties have the same surname.
and I have the following tables;
***REPRESENTATIVE***
REPI SURNAME FIRSTNAME COUNTY CONS
---- ---------- ---------- ---------- ----
R100 Gorege Larry kent CON1
R101 shneebly john kent CON2
R102 shneebly steve kent CON3
I cant seem to figure out the correct way to ask Orical to display a surname that exists more then twice and the surnames are in the same country.
I know how to ask WHERE something = something, but that's doesn't ask what I want to know.
It sounds like you want to use the HAVING clause after doing a GROUP BY
SELECT surname, county, count(*)
FROM you_table
GROUP BY surname, county
HAVING count(*) > 1;
If you really mean "more than twice" as you wrote, none of the data you'd want HAVING count(*) > 2 but then none of your sample data would be returned.
In words, this SQL statement says
Group the data into buckets by surname and county. Each distinct combination of surname and county is a separate bucket.
Count the number of rows in each bucket
Return those buckets where there are at least two rows

Include variable as field name in linq statement

Hi I have a link statement against a database table I did not create... The data structure is
Tbl_BankHols
BHDate .... Datetime
E ......... Bit
S ......... Bit
W ......... Bit
I ......... Bit
Basically it has a list of dates and then a value of 0 or 1 in E, S, W, I which indicate if that date is a bank holiday in England, Scotland, Wales and/or Ireland.
If I want to find out if a date is a bank holiday in any of the countries my Linq statement is
Dim BHQ = From d in db.Tbl_BankHols _
Where d.BHDate = chkDate _
Select d.BHDate
Where chkDate is the date I am checking. If a result is returned then the date is a bank holiday in one of the countries.
I now need to find out if chkDate is a bank holiday in a particular country how do I introduce that into the where statement?
I'm asking if this is possible before I think about changing the structure of the database. I was thinking of just having a single country field as a string which will contain values like E, EW, EWS, EWSI, I and other similar combinations and then I just use WHERE BCountry LIKE %X% (where X is the country I'm interested in). Or is there a better way?
Erm stop,
Your suggestion for extra denormalisation and using LIKE is a really "wrong" idea.
You need one table for countries, lets call it Country and another table for holidays, lets call it Holiday. The Country table should contain a row for each country in your system/model. The Holiday table should have two columns. One for the Date and a foriegn key to country, lets call it CountryId.
Then your linq could look something like,
db.Holiday.Any(Function(h) h.Country.Name =
"SomeCountry" AndAlso h.Date = someDate)
The reasons why you shoudn't use LIKE for this are manifold but two major objections are.
LIKE doesn't perform well, its hard for an index to support it, and,
Lets imagine a situation where you need to store holidays for these countries,
Ecuador
El Salvador
Estonia
Ethiopia
England
Now, you have already assigned the code "E" to England, what code will you give to the others? No problem you say, "EL", "ET" ... but, already your LIKE "%E" condition is broken.
Here are the scripts for the schema I would go with.
CREATE TABLE [Country](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [nvarchar](100) NOT NULL,
CONSTRAINT [PK_Country] PRIMARY KEY CLUSTERED
(
[Id] ASC
));
CREATE TABLE [Holiday](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Date] [date] NOT NULL,
[CountryId] [int] NOT NULL FOREIGN KEY Country(Id),
CONSTRAINT [PK_Country] PRIMARY KEY CLUSTERED
(
[Id] ASC
));
Instead of changing the structure of your table the way you wrote, you could introduce a new Region (Code, Description) table and add a foreign key to your table pointing to the regions table. Your bank holidays table will then contain one record per (date/region) combination.
And your linq statement:
Dim BHQ = From d in db.Tbl_BankHols _
Where d.BHDate = chkDate And d.Region = "England" _
Select d.BHDate
before I think about changing the structure of the database
You can do it by composing a query with OR predicates by using LINQKit.
Dim IQueryable<BankHoliday> BHQ = ... (your query)
Dim pred = Predicate.False(Of BankHolifday)
If countryString.Contains("E")
pred = pred.Or(Function(h) h.E)
EndIf
If countryString.Contains("S")
pred = pred.Or(Function(h) h.S)
EndIf
...
Return query.Where(pred.Expand())
(I'm not fluent in VB so there may be some error in there)
See this answer for a similar example.
While the DB structure is not optimal, if you are working with legacy code and there's no effort given for a full refactor, you're gonna have to make do (I feel for you).
The simplest option I think you have is to select not just the date, but also the attributes.
Dim BHQ = From d in db.Tbl_BankHols _
Where d.BHDate = chkDate _
Select d
This code will give you d.S, d.E, d.W, and d.I, which you can use programmatically to determine if the holiday applies to whatever country you are currently working on. This means outside the query, you will have a separate if statement which would qualify if the holiday applies to the country you are processing.

Resources