Sie sind auf Seite 1von 11

Property Rentals

Our company arranges rentals of properties owned by both private and business owners.
We assign every property owner a unique owner number for identification, we record its address
(consisting of a street, street number, town or city, and province), the owners name (consisting
of first, middle, and last name for a person or name of a business), and the owners email
addresses and the owner phone numbers. For a business owner, we record the type (description)
of its business. Each property is identified by a unique property number, we record its address
and its type. Each property may be placed in several advertisements. Each such advertisement
may be displayed in many newspapers on several dates. The newspapers are identified by unique
names.
The term renter refers to a private person or a business who signed a rental agreement for a
property. Each such rental agreement is identified in our database by a unique rental number. We
record the date of the singing of the rental agreement, the starting and ending date of the rental
agreement. A renter can rent many properties. A renter, prior to accepting the rental agreement
may view the property repeatedly and we record the date of viewing. For each renter, we record
its address, its name, its email address and phone numbers. Each renter has a unique renter
number in our database.
Our agency is organized into branches and every staff member is allocated to exactly one branch.
Each branch has one manager who is a member of the staff. In our database, we identify the staff
by a unique staff number. For each staff member we record address, name, email address, phone
numbers, sex, position, and salary. Each property is in care of one of our branches. Each renter
refers to the branch that is in care of the property it rents. Each property is overseen by a unique
staff member. Each branch has an address, phone number, and a unique branch number.

Property Rentals Logical Model

*SQL CODE
DROP
DROP
DROP
DROP
DROP
DROP
DROP
DROP
DROP
DROP
DROP
DROP
DROP
DROP
DROP
DROP
DROP

TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE
TABLE

OWNER CASCADE CONSTRAINTS;


OWNER_EMAIL CASCADE CONSTRAINTS;
OWNER_PHONE CASCADE CONSTRAINTS;
BRANCH CASCADE CONSTRAINTS;
BRANCH_EMAIL CASCADE CONSTRAINTS;
STAFF CASCADE CONSTRAINTS;
BRANCH_PHONE CASCADE CONSTRAINTS;
ADVERTISEMENT CASCADE CONSTRAINTS;
NEWSPAPER CASCADE CONSTRAINTS;
VIEWING CASCADE CONSTRAINTS;
RENTER CASCADE CONSTRAINTS;
RENTER_PHONE CASCADE CONSTRAINTS;
RENTER_EMAIL CASCADE CONSTRAINTS;
STAFF_EMAIL CASCADE CONSTRAINTS;
STAFF_PHONE CASCADE CONSTRAINTS;
PROPERTY CASCADE CONSTRAINTS;
RENTAL_AGREEMENT CASCADE CONSTRAINTS;

Create table Branch(


Branch_Number INT NOT NULL,
Branch_Manager varchar (15) NOT NULL,
Branch_StreetNumber varchar(15) NOT NULL,
Branch_StreetName Varchar(15) NOT NULL,
Branch_City varchar(10) NOT NULL,
Branch_Province varchar(10) NOT NULL,
Branch_Zip INT NOT NULL,
Primary Key(Branch_Number)
);
Create table Staff(
Staff_Number varchar(15) Primary Key NOT NULL,
Staff_Branch INT NOT NULL,
Staff_FName varchar(35) NOT NULL,
Staff_LName varchar(35) NOT NULL,
Staff_MName varchar(35) NOT NULL,
Staff_StreetNumber varchar(10) NOT NULL,
Staff_StreetName char(15) NOT NULL,
Staff_City CHAR(10) NOT NULL,
Staff_Province CHAR(10) NOT NULL,
Staff_Zip Char(10) NOT NULL,
Satff_Gender Char(10) NOT NULL,
Staff_Salary varchar(15) NOT NULL
);
Create Table Owner (
Owner_Number Integer Primary Key,
Owner_Fname Varchar(35) NOT NULL,
Owner_LName Varchar(35) NOT NULL,
Owner_MName Varchar(35) NOT NULL,
Owner_StreetNumber char(7) NOT NULL,
Owner_StreetName char(15) NOT NULL,
Owner_City CHAR(10) NOT NULL,
Owner_Province CHAR(10) NOT NULL,
Owner_PostalCode Char(5) NOT NULL,
Owner_Bustype Char(15) NOT NULL
);
CREATE TABLE Renter (
Renter_Number int NOT NULL,
Renter_Fname Char(20) NOT NULL,
Renter_Lname Char(20) NOT NULL,
Renter_Mname Char(20) NOT NULL,
Renter_StreetNumber char(7) NOT NULL,
Renter_StreetName char(15) NOT NULL,
Renter_City CHAR(10) NOT NULL,

Renter_Province CHAR(10) NOT NULL,


Renter_Zip Char(5) NOT NULL,
Renter_Bustype Char(15) NOT NULL,
Primary Key (Renter_Number)
);
Create Table Property (
Property_Number INT PRIMARY KEY NOT NULL,
Property_OwnedBy INT NOT NULL,
Property_OverseenBy VARCHAR(15) NOT NULL,
Property_StreetNumber INT NOT NULL,
Property_StreetName Char(20) NOT NULL,
Property_City Char(20) NOT NULL,
Property_Province Char(20) NOT NULL,
Property_PostalCode Char(20) NOT NULL,
Property_Type Char(20) NOT NULL,
FOREIGN KEY(Property_OwnedBy) REFERENCES OWNER(OWNER_NUMBER),
FOREIGN KEY(Property_OverseenBy) REFERENCES STAFF(STAFF_NUMBER)
);
CREATE TABLE Rental_Agreement(
Rental_PropNumber int NOT NULL,
Rental_Number int NOT NULL,
Renter_Number int NOT NULL,
Rental_SignDate Varchar(35) NOT NULL,
Rental_StartDate Varchar(35) NOT NULL,
Rental_EndDate Varchar(35) NOT NULL,
PRIMARY KEY(Rental_Number),
FOREIGN KEY(Rental_PropNumber) References Property(Property_Number),
FOREIGN KEY(Renter_Number) References Renter(Renter_NumBER)
);
Create Table Renter_Email(
Renter_Email Varchar (100) NOT NULL,
Renter_Number INT NOT NULL,
Primary Key (Renter_Email),
Foreign Key (Renter_Number) References RENTER(Renter_NumBER)
);
Create Table Staff_Email (
Staff_Number varchar(15) NOT NULL,
Staff_EmailAddress varchar(15) NOT NULL,
Primary Key (Staff_EmailAddress)
);
Create table Owner_Email(
Owner_Number int NOT NULL,
Owner_EmailAddress char(35) NOT NULL,
Primary key (Owner_EmailAddress),
Foreign Key (Owner_Number) References Owner(Owner_Number)
);
Create table Branch_Email(
Branch_Number INT NOT NULL,
Branch_Email varchar(35) NOT NULL,
Primary Key(Branch_Email),
FOREIGN KEY(Branch_Number) References Branch(Branch_Number)
);
Create Table Renter_Phone(
Renter_Number INT NOT NULL,
Renter_AreaCode INT NOT NULL,
Renter_PhoneNumber Varchar (35) NOT NULL,
Renter_Ext INT NOT NULL,
Primary Key (Renter_PhoneNumber),
Foreign Key (Renter_Number) References RENTER(Renter_NumBER)
);
Create Table Staff_Phone (

Staff_AreaCode INT NOT NULL,


Staff_PhoneNumber INT NOT NULL,
Staff_Number VARCHAR(15) NOT NULL,
Staff_Extension INT NOT NULL,
Primary Key (Staff_AreaCode,Staff_PhoneNumber),
FOREIGN KEY(STAFF_NUMBER) REFERENCES STAFF(STAFF_NUMBER)
);
Create table Owner_Phone(
Owner_Number INT NOT NULL,
Owner_AreaCode INT NOT NULL,
Owner_PhoneNumber INT NOT NULL,
Owner_Extension INT NOT NULL,
Primary Key(Owner_PhoneNumber),
Foreign Key(Owner_Number) References Owner(Owner_Number)
);
Create Table Branch_Phone(
Branch_Number INT NOT NULL,
Branch_AreaCode INT NOT NULL,
Branch_PhoneNumber INT NOT NULL,
Branch_Extension INT NOT NULL,
Primary Key(Branch_PhoneNumber),
FOREIGN KEY(BRANCH_NUMBER) REFERENCES BRANCH(BRANCH_NUMBER)
);
Create Table Viewing (
Property_Number INT NOT NULL,
Viewing_Date Varchar (35) NOT NULL,
Renter_Number INT NOT NULL,
Primary Key (Property_Number,Viewing_Date,Renter_Number),
FOREIGN KEY(PROPERTY_NUMBER) REFERENCES PROPERTY(PROPERTY_NUMBER),
FOREIGN KEY(RENTER_NUMBER) REFERENCES RENTER(RENTER_NUMBER)
);
Create Table Newspaper (
Newspaper_Name Varchar(35) NOT NULL,
Primary Key (Newspaper_Name)
);
Create Table Advertisement(
Newspaper_Name Varchar(35) NOT NULL,
Advertisement_Number INT NOT NULL,
Property_Number INT NOT NULL,
Ad_Date Varchar (35) NOT NULL,
Primary Key (Advertisement_Number),
FOREIGN KEY(NEWSPAPER_NAME) REFERENCES NEWSPAPER(NEWSPAPER_NAME),
FOREIGN KEY(PROPERTY_NUMBER) REFERENCES PROPERTY(PROPERTY_NUMBER)
);

INSERT INTO BRANCH


VALUES('1111','0001','4321','sup','dallas','texas','75068');
INSERT INTO BRANCH
VALUES('1112','0001','123','upp','dallas','texas','75068');
INSERT INTO BRANCH
VALUES('1113','0004','421','hello','dallas','texas','75068');
INSERT INTO BRANCH
VALUES('1114','0001','2156','campbell','dallas','texas','75068');
INSERT INTO STAFF
Values ('0001','1111','Captain','Falcon','Who','123','South','Vice','Texas','75080','Male','50000');
INSERT INTO STAFF

Values ('0002','1112','Captain','Ahab','Who','124','Nouth','Vice','Texas','75080','Male','60000');
INSERT INTO STAFF
Values ('0003','1113','Doctor','What','Who','133','West','Vice','Texas','75080','Male','75000');
INSERT INTO STAFF
Values ('0004','1114','Captain','Admiral','General','1333','South','Vice','Texas','75080','Male','100000');
INSERT INTO OWNER
VALUES ('001', 'Luis', 'Garcia', 'Fernando','1105','One','Frisco','Texas','75035','Profit');
INSERT INTO OWNER
VALUES ('002', 'Kien', 'Hoang', 'Ayy','2205','two','Dallas','Texas','75022','Profit');
INSERT INTO OWNER
VALUES ('003', 'Adam', 'Samadzada', 'eyy','3305','three','richardson','Texas','75080','NonProfit');
INSERT INTO OWNER
VALUES ('004', 'Dawn', 'Owens', 'Marie','4405','four','plano','Texas','75026','Profit');
INSERT INTO RENTER
VALUES ('1000','James','Murphy','Pled','678','one','Dallas','Texas','75068','Private');
INSERT INTO RENTER
VALUES ('1001','Adam','Murp','delve','548','two','Dallas','Texas','75068','private');
INSERT INTO RENTER
VALUES ('1002','James','Mahy','Pled','6578','three','Dallas','Texas','75068','business');
INSERT INTO RENTER
VALUES ('1003','Mister','Gunit','tru','67878','four','Dallas','Texas','75068','business');
INSERT INTO Property
Values ('500','001','0001', '123', 'Franky', 'Richardson', 'Texas', '75000', ' NonProfit');
INSERT INTO Property
Values ('600','002','0002', '124', 'Kranky', 'Nichardson', 'Florida', '75001', ' NonProfit');
INSERT INTO Property
Values ('700','003','0003', '125', 'Lranky', 'Bichardson', 'Texas', '75002', 'Profit');
INSERT INTO Property
Values ('800', '004','0004', '126', 'Tranky', 'Michardson', 'Florida', '7500', ' NonProfit');
INSERT INTO Rental_Agreement
VALUES ('500', '100', '1000','2-10-2013', '3-10-2014', '5-10-14');
INSERT INTO Rental_Agreement
VALUES ('600', '200', '1001','3-11-2013', '4-10-2014', '6-10-14');
INSERT INTO Rental_Agreement
VALUES ('700', '300', '1002','4-11-2012', '3-14-2014', '5-11-14');
INSERT INTO Rental_Agreement
VALUES ('800', '400', '1003','2-15-2013', '3-20-2014', '5-20-14');
INSERT INTO RENTER_EMAIL
VALUES ('RENTERONE@GMAIL.COM','1000');
INSERT INTO RENTER_EMAIL
VALUES ('RENTERTWO@GMAIL.COM','1001');
INSERT INTO RENTER_EMAIL
VALUES ('RENTERTHREE@GMAIL.COM','1002');
INSERT INTO RENTER_EMAIL
VALUES ('RENTERFOUR@GMAIL.COM','1003');
INSERT INTO STAFF_EMAIL

VALUES ('0001','falc@gmail.com');
INSERT INTO STAFF_EMAIL
VALUES ('0002','ahab@gmail.com');
INSERT INTO STAFF_EMAIL
VALUES ('0003','who@gmail.com');
INSERT INTO STAFF_EMAIL
VALUES ('0004','gen@gmail.com');
INSERT INTO Owner_Email
VALUES ('001', 'Robert.James52@yahoo.com');
INSERT INTO Owner_Email
VALUES ('002', 'Robert.James0122@yahoo.com');
INSERT INTO Owner_Email
VALUES ('003', 'Robert.James902@yahoo.com');
INSERT INTO Owner_Email
VALUES ('004', 'Robert.James9342@yahoo.com');
INSERT INTO BRANCH_EMAIL
VALUES ('1111','ONEBRANCH@GMAIL.COM');
INSERT INTO BRANCH_EMAIL
VALUES ('1112','TWOBRANCH@GMAIL.COM');
INSERT INTO BRANCH_EMAIL
VALUES ('1113','THREEBRANCH@GMAIL.COM');
INSERT INTO BRANCH_EMAIL
VALUES ('1114','FOURBRANCH@GMAIL.COM');
INSERT INTO RENTER_PHONE
VALUES ('1000','210','8446488','6488');
INSERT INTO RENTER_PHONE
VALUES ('1001','210','3944088','4088');
INSERT INTO RENTER_PHONE
VALUES ('1002','210','6886886','6886');
INSERT INTO RENTER_PHONE
VALUES ('1003','340','3456487','6487');
INSERT INTO STAFF_PHONE
VALUES ('210','6106577','0001','6577');
INSERT INTO STAFF_PHONE
VALUES ('214','6106566','0002','6566');
INSERT INTO STAFF_PHONE
VALUES ('451','6106337','0003','6337');
INSERT INTO STAFF_PHONE
VALUES ('210','6186577','0004','6577');
INSERT INTO Owner_Phone
Values ('001','972','7306676','6676');
INSERT INTO Owner_Phone
Values ('002','973','7306673','6673');
INSERT INTO Owner_Phone
Values ('003','974','7306677','6677');
INSERT INTO Owner_Phone

Values ('004','976','7366666','6666');
INSERT INTO BRANCH_PHONE
VALUES ('1111','469','123456','01');
INSERT INTO BRANCH_PHONE
VALUES ('1112','469','123789','02');
INSERT INTO BRANCH_PHONE
VALUES ('1113','469','123987','03');
INSERT INTO BRANCH_PHONE
VALUES ('1114','469','123000','04');
INSERT INTO VIEWING
VALUES ('500','3-11-12','1000');
INSERT INTO VIEWING
VALUES ('600','3-21-12','1001');
INSERT INTO VIEWING
VALUES ('700','4-21-12','1002');
INSERT INTO VIEWING
VALUES ('800','5-11-12','1003');
INSERT INTO Newspaper
Values ('Dallas Today');
INSERT INTO Newspaper
Values ('Texas Today');
INSERT INTO Newspaper
Values ('The Mercury');
INSERT INTO Newspaper
Values ('University News');
INSERT INTO Advertisement
VALUES ('Dallas Today','100','500','1-12-12');
INSERT INTO Advertisement
VALUES ('Texas Today','200','600','1-22-12');
INSERT INTO Advertisement
VALUES ('The Mercury','300','700','1-12-13');
INSERT INTO Advertisement
VALUES ('University News','400','800','1-12-14');

Property Rentals Queries


Query 1: Give the staff number of each of the staff members whose salary is greater than
5000. Please, sort them by the staff number.
SELECT STAFF_NUMBER
FROM STAFF
WHERE STAFF_SALARY>5000

ORDER BY STAFF_NUMBER

Query 2: Give the renter number of each of the renters who has a viewing record. Please avoid
duplications.
SELECT DISTINCT RENTER_NUMBER
FROM VIEWING

Query 3: Give the dates of all the advertisements posted in a particular newspaper. Please, avoid
duplications.
SELECT DISTINCT AD_DATE
FROM ADVERTISEMENT
WHERE NEWSPAPER_NAME = 'The Mercury'

Query 4: Give the email addresses and the renter number for all the private renters.
Please, sort them by the renter number.
SELECT RENTER_EMAIL.RENTER_EMAIL, RENTER.RENTER_NUMBER
FROM RENTER_EMAIL, RENTER
WHERE RENTER_EMAIL.RENTER_NUMBER = RENTER.RENTER_NUMBER
AND Renter_Bustype like '%private%'
Order By Renter.Renter_number
Query 5: Find the properties that are already advertised but not yet rented. Please, avoid
duplications.
SELECT DISTINCT PROPERTY_NUMBER
FROM ADVERTISEMENT
WHERE PROPERTY_NUMBER NOT IN (SELECT DISTINCT
RENTAL_PROPNUMBER FROM RENTAL_AGREEMENT)
Query 6: Give the names and the branch numbers of all the staff members working in the branch
which is located in Hamilton. The names should be listed in an alphabetic order (by last, then by
first, then by middle names).
SELECT STAFF_FNAME,STAFF_MNAME,STAFF_LNAME,STAFF_BRANCH
FROM STAFF,BRANCH
WHERE STAFF.STAFF_BRANCH=BRANCH.BRANCH_Number

AND BRANCH.BRANCH_CITY='HAMILTON'
ORDER BY STAFF_LNAME,STAFF_FNAME,STAFF_MNAME
Query 7: List the number of advertisements for each property.
SELECT COUNT(ADVERTISEMENT_NUMBER)
FROM ADVERTISEMENT
Query 8: Find the branch number and the average salary of the branch that has the highest
average salary. Please, call the branch number as branch_no and the average salary as avg_salary.
SELECT branch.BRANCH_NUMBER AS "branch_no", AVG(staff.STAFF_SALARY) AS
"avg_salary"
from Branch,Staff
Where branch.branch_number = staff.staff_branch
group by branch.BRANCH_NUMBER, staff.STAFF_SALARY

Query 9: Find the owners and renters who have 2 or more phone numbers. Call the owner/renter
number as customer_no, set the value of type_of_customer to 'owner' if the customer is an
owner, and to 'renter' if he/she is a renter. Please, only list the customer_no and
type_of_customer.
SELECT Owner_Number AS "CUSTOMER_NO", 'OWNER' AS
"TYPE_OF_CUSTOMER"
FROM OWNER
WHERE OWNER_NUMBER IN (SELECT OWNER_NUMBER FROM
OWNER_PHONE GROUP BY OWNER_NUMBER HAVING COUNT(*)>=2)
UNION
SELECT RENTER_NUMBER AS "CUSTOMER_NO", 'RENTER' AS
"TYPE_OF_CUSTOMER"
FROM RENTER
WHERE RENTER_NUMBER IN (SELECT RENTER_NUMBER FROM
RENTER_PHONE GROUP BY RENTER_NUMBER HAVING COUNT(*)>=2)
Query 10: Assuming that each advertisement costs 100 dollars, give the branch number and the
amount spent on the advertisements for each branch. Name the branch number as branch_no, and
the amount as ad_cost.
SELECT BRANCH_NUMBER AS "branch_no", (COUNT
(ADVERTISEMENT_NUMBER) *100) AS "AD_COST"
FROM ADVERTISEMENT, BRANCH
GROUP BY BRANCH_NUMBER

Das könnte Ihnen auch gefallen