Database SQL Q&A 1
First COMP 353 database Q&A
Database SQL question:
The database schema for a community operated barter, announcement and giveaway system,
contains among others, the following relations:
Member (ID, Name, Address, City, Prov, PCode, Country, PhoneNo, Email)CreditCard (MemberID, CardNo, CardID, ExpDate)CardHolder (CardNo, HolderName, Address, City, Prov, PCode, Country, PhoneNo, Email)Listing (ListingID, MemberID, ListingType, PostingDate, Duration)
Here the relation Member contains the details of a member of the system. Each member register information about a credit card (credit card number and the card identification number and the expiry date) for charging the annual membership fees and other charges. The credit card may belong to the member or some other person; details of the holder are given in the relation CardHolder.
A member may make one of more listing to offer a service or article for barter, sell or giveaway (ListingType). Each such Listing has a posting date and Duration(days being 7, 15, 30, or 60). (Note: Associated with each listing is description. These details should be stored in a separate tables which are not included here. )
Q: Creating tables corresponding to these relations with appropriate domains and populate them with representative data.
Use these tables to write SQL query to answer the following queries.
1. Create a database called barter, then use it:
1 | --create a database barter |
2. Create table Member
1 | CREATE TABLE Member( |
insert 5 to 10 tuples into table Member
1 | insert into member values(11111, 'Jack', 'address 1', 'Montreal', 'Quebec', 'QC', 'Canada', '514-111-1111', '11111@gmail.com'); |
3. Create table CreditCard
1 | CREATE TABLE CreditCard( |
insert 5 to 10 tuples into table CreditCard
1 | insert into creditcard values(11111, 112345, 1, '01/20'); |
4. Create table CardHolder
1 | CREATE TABLE CardHolder( |
insert 5 to 10 tuples into table CardHolder
1 | --insert 5 to 10 tuples per table |
5. Create table Listing
1 | CREATE TABLE Listing( |
insert 5 to 10 tuples into table Listing
1 | insert into Listing values(00001, 11111, 'sell', '2020/10/01', 7); |
SQL query:
- Find the
namesofMembersfromMontreal.1
2
3
4
5
6select name from member where city like ('Montreal');
/* Another solution:
select distinct(name) from member where city like ('Montreal');
select name from member where city = 'Montreal';
*/ - List the
NamesandCitiesofmembersalong with thelistingsthey have posted.1
2select name, city, listing.* from member, listing where memberid = id;
- For all
giveawaytypelistingfrommembersinMontreal, list the membersnameanddatesof the listings.1
2select m.name, L.postingdate from member m, listing L where m.city='Montreal' and m.id=L.memberid and L.listingtype='giveaway';
- List the pairs of names where the first name is the
holderof thecredit cardand the second name is that of amemberwho uses thiscredit cardand these names are not the same.1
select h.HolderName, m.name from CardHolder h, Member m, CreditCard c where m.id=c.memberid and c.cardno=h.cardno and h.HolderName != m.name;
- Repeat above where the
namesare the same, limiting the list to thememberswho live inLaval.1
select h.HolderName, m.name from CardHolder h, Member m, CreditCard c where m.id=c.memberid and c.cardno=h.cardno and h.HolderName=m.name and m.city='Laval';
- Find the
memberswith the largest number oflistings.1
2
3select * from member where id in ( select MemberID from Listing group by MemberID having COUNT(*) >= all(select COUNT(*) as count from Listing group by MemberID));
--we can test the other special case by inserting more tuples to listing table - Find the members with no listings.
1
2select * from member where ID NOT IN (select DISTINCT(MemberID) from Listing);
We can add more tuples into these tables to test them in MySQL.
Database SQL Q&A 1







