- Create a database called
apartments
- Using this database, create two tables, one for
owner
s and one forproperty
s (table names shoud be lowercase and singular) - Keep this relationship in mind when designing your schema:
- One owner can have many properties
Place your answers in part-1.sql
.
- The owners table should consist of:
id
(this should be the primary key as well as a unique number that increments automatically)name
- name of ownerage
- age of owner
- The properties table should consist of:
id
(this should be the primary key as well as a unique number that increments automatically)name
- name of propertyunits
- number of unitsowner_id
- reference to owner table- Remember to create a foreign key constraint that references the owners table
Place your answers in part-2.sql
.
-
Insert the following owners
- Donald - age 29
- John - age 33
- Jane - age 43
- Add 3 more people (you choose name / age)
-
Insert the following properties (you can pick and choose the property owners)
- Archstone - 20 units
- Willowspring - 30 units
- Add 3 more properties (you choose name / units)
Write down the following sql statements that are required to solve the following tasks. Place your answers in part-3.sql
.
- Show all the data in the owners table. // select * from owner;
- Show the names of all owners. // select name from owner;
- Show the ages of all of the owners in ascending order. // select age from owner asc;
- Show the name of an owner whose name is Donald. // select name from owner where name = 'Donald';
- Show the age of all owners who are older than 30. // select age from owner where age > 30;
- Show the name of all owners whose name starts with an E. // select * from owners where name like 'E%';
- Change Jane's age to 30. // update owner set age = 30 where name = 'Jane';
- Change Jane's name to Janet. // update owner set name = 'Janet' where name 'Jane';
- Delete the owner named Janet. // delete from owner where name = 'Janet';
- Show the names of the first three owners in your owners table. // select name from owner where id < 4;
- List all properties sorted by the owners names // select * from properties join owner on owner.id = properties.owner_id order by owner.name;
- Show all of the properties in alphabetical order that are not named Archstone and do not have an id of 3 or 5. // select * from properties join owner on owner.id = properties.owner_id where properties.name != 'Archstone' order by properties;
These might require you to look up documentation online, or look at the next section in the notes.
- In the properties table change the name of the column "name" to "property_name". // alter table properties rename name to property_name;
- Count the total number of properties where the owner_id is between 1 and 3. // select sum(units) from properties where owner_id between 1 and 3;
- Show the highest age of all owners. // select max(age) from owner;
- Show the name of all owners whose name starts with an E. // select name from owner where name like 'E%';
- All content is licensed under a CC-BY-NC-SA 4.0 license.
- All software code is licensed under GNU GPLv3. For commercial use or alternative licensing, please contact [email protected].