Use two different relational algebra to do a query - relational-algebra

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

Related

In Eloquent, is there a way to use sql functions in relationship query

I am working in Eloquent with Laravel, and I have a table named invoices.
Each invoice has a prefix that identifies the dealer id.
For example,
01-1234 is an invoice from dealer 01. 02-5678 is an invoice from dealer 02.
Further complicating things is that the dealer's id is not 01 or 02, but DEALER01 or DEALER02.
I would like to know if it's possible to get a query like this to work, so that the invoice object has an associated dealer object, without having to write a lot of repetitive code to loop through the invoices and manually link them.
public function dealer() {
return $this->hasOne(Invoice::class, 'dealerId', 'concat(\'DEALER\', substr(Invoice.invoice, 1, 2))')
}
Here are a few fields from the table structure of invoice:
invoice
cust
date
01-1234
1
2021-06-05
02-1235
2
2021-06-06
And here are a few fields from the structure of dealer:
dealerId
county
default_sales_acct
DEALER01
Some county
1234
DEALER02
Some other county
5678
The only fields needed to link the two tables in this case are dealerId and the first two characters of invoice.

Is this natural join operation used correctly? (Relational Algebra)

I have the following task given from the professor:R-E Modell
Assume the companies may be located in several cities. Find all companies located in every city in which “Small Bank Corporation” is
located.
Now the professor's solution is the following:
s ← Π city (σ company_name=’Small Bank Corporation’ (company))
temp1 ← Π comp_id, company_name (company)
temp2 ← Π comp_id, company_name ((temp1 × s) − company)
result ← Π company_name (temp1 − temp2)
I for myself found a completely different solutions with a natural join operation which seems much simpler:
What I tried to do was using the natural joint operation which whe defined as following that a relation r and s are joined on their common attributes. So I tried to get all city names by using a projection on a selection of all companies with the company_name "Small Bank Cooperation". After that I joined the table with the city names with the company table, so that I get all company entrys which have the city names in it.
company ⋈ Π city (σ company_name=”Small Bank Cooperation” (company)))
My question now is if my solution is also valid, since it seems a little bit to trivial?
Yours isn't the same.
My answer here says how to query relationally. It uses a version of the relational algebra where headings are sets of attribute names. My answer here summarizes it:
Every query expression has an associated (characteristic)
predicate--statement template parameterized by attributes. The tuples
that make the predicate into a true proposition--statement--are in
the relation.
We are given the predicates for expressions that are relation names.
Let query expression E have predicate e. Then:
R ⨝ S has predicate r and s
R ∪ S has predicate r or s
R - S has predicate r and not s
σ p (R) has predicate r and p
π A (R) has predicate exists non-A attributes of R [r]
When we want the tuples satisfying a certain predicate we find a way
to express that predicate in terms of relation operator
transformations of given relation predicates. The corresponding query
returns/calculates the tuples.
Your solution
company ⋈ Π city (σ company_name=”Small Bank Corporation” (company)))
is rows where
company company_id named company_name is in city
AND FOR SOME company_id & company_name [
company company_id named company_name is in city
AND company_name=”Small Bank Corporation”]
ie
company company_id named company_name is in city
AND FOR SOME company_id [
company company_id named ”Small Bank Corporation” is in city]
ie
company company_id named company_name is in city
AND some company named ”Small Bank Corporation” is in city
You are returning rows that have more columns than just company_name. But your companies are not the requested companies.
Projecting your rows on company_name gives rows where
some company named company_name is in some city
AND some company named ”Small Bank Corporation” is in that city
After that I joined the table with the city names with the company
table, so that I get all company entrys which have the city names in
it.
That isn't clear about what you get. However the companies in your rows are those in at least one of the SBC cities. The request was for those in all of the SBC cities:
companies located in every city in which “Small Bank Corporation” is located
The links I gave tell you how to compose queries but also convert between query result specifications & relational algebra expressions returning a result.
When you see a query for rows matching "every" or "all" of some other rows you can expect that that part of your query involves relational-division or some related idiom. The exact algebra depends on what is intended by the--frequently poorly/ambiguously expressed--requirements. Eg whether "companies located in every city in which" is supposed to be no companies (division) or all companies (related idiom) when there are no such cities. (The normal mathematical interpretation of your assignment is the latter.) Eg whether they want companies in exactly all such cities or at least all such cities.
(It helps to avoid "all" & "every" after "find" & "return", where it is redundant anyway.)
Database Relational Algebra: How to find actors who have played in ALL movies produced by “Universal Studios”?
How to understand u=r÷s, the division operator, in relational algebra?
How to find all pizzerias that serve every pizza eaten by people over 30?

Oracle SQL Developer - How to add constraints to er diagram

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))

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?

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

Resources