Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Social Networking Database
Old 03-24-2009, 02:37 PM Social Networking Database
Novice Talker

Posts: 5
Trades: 0
I found an interesting article (Deconstructing Myspace.com Part 1 - Social Networking Database, btw there is not a Part 2) and after discovering that the basic structure behind these social networking sites is very simple, I wanted to experiment with it. I am having some issues with the queries suggested in the article. Here is what I did, please let me know if you see any errors.
Code:
CREATE TABLE IF NOT EXISTS Accounts (
ID varchar(255) NOT NULL default '',
DisplayName varchar(25) default NULL,
FirstName varchar(25) default NULL,
LastName varchar(25) default NULL,
Username varchar(25) default NULL,
Password varchar(255) default NULL,
PRIMARY KEY  (ID)
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS Friends (
AccountID varchar(255) NOT NULL default '',
FriendID varchar(255) NOT NULL default '',
PRIMARY KEY  (AccountID,FriendID),
CONSTRAINT FOREIGN KEY (AccountID) REFERENCES Accounts (ID) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT FOREIGN KEY (FriendID) REFERENCES Accounts (ID) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB;
I was able to create two tables using similar code as above. I then wanted to play with the suggested queries. So I inserted some info like this.
Code:
INSERT INTO Accounts (ID) VALUES ('Person1');
INSERT INTO Accounts (ID) VALUES ('Person2');
INSERT INTO Accounts (ID) VALUES ('Person3');
Next I made Person1 and Person2 friends and Person3 request a friendship of Person1.
Code:
INSERT INTO Friends (AccountID,FriendID) VALUES ('Person1', 'Person2');
INSERT INTO Friends (AccountID,FriendID) VALUES ('Person2', 'Person1');
INSERT INTO Friends (AccountID,FriendID) VALUES ('Person3', 'Person1');
Following the first two inserts in the Friends table above, it is my understanding that for two people to be friends, there must be two entries in the table. The first has personA as the AccountID and personB as the FriendID, then the second is reversed with personB as the AccountID and personA as the FriendID. However, as above, there is not a friendship between Person1 and Person 3 yet. Is this correct?

So I tried to run some of the sample queries (i.e. Listing 2) and they failed. What is wrong with the following query? Apparently it should return all the people who have requested friendship with Person1 and are not friends yet. In this case Person3 should be returned I believe.
Code:
SELECT DISTINCT a.* FROM Accounts a INNER JOIN Friends f ON a.ID = f.AccountID WHERE f.FriendID = 'Person1'
Thanks for the help!

Last edited by scotty2024; 03-26-2009 at 07:32 PM.. Reason: fixed typos in create table sql
scotty2024 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-25-2009, 02:15 PM Re: Social Networking Database
Novice Talker

Posts: 5
Trades: 0
Anyone know why the select query fails?
scotty2024 is offline
Reply With Quote
View Public Profile
 
Old 03-25-2009, 02:26 PM Re: Social Networking Database
NullPointer's Avatar
Will Code for Food

Posts: 2,786
Name: Matt
Location: Irvine, CA
Trades: 0
Try
Code:
SELECT DISTINCT * FROM Accounts NATURAL JOIN Friends ON Accounts.ID = Friends.AccountID WHERE Friends.FriendID = 'Person1'
Inner join will give you the cartesian product of the two tables, and I don't think that is what you want. Also I cleaned up your syntax a bit.

By the way, don't use SELECT * unless you actually need every column. Specify the columns you need (ie SELECT Accounts.username , Friends.AccountID etc)
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-26-2009, 07:03 PM Re: Social Networking Database
Novice Talker

Posts: 5
Trades: 0
Hi NullPointer,
Thank you for the reply. I tried your query but it throws the following error:
Quote:
Error

SQL query:
SELECT DISTINCT *
FROM Accounts
NATURAL JOIN Friends ON Accounts.ID = Friends.AccountID
WHERE Friends.FriendID = 'Person1'
LIMIT 0 , 30
MySQL said:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ON Accounts.ID = Friends.AccountID WHERE Friends.FriendID = 'Person1'
LIMIT 0, ' at line 1
scotty2024 is offline
Reply With Quote
View Public Profile
 
Old 03-26-2009, 07:24 PM Re: Social Networking Database
NullPointer's Avatar
Will Code for Food

Posts: 2,786
Name: Matt
Location: Irvine, CA
Trades: 0
My mistake. Use a left join in place of the natural join and that should work.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-26-2009, 07:29 PM Re: Social Networking Database
Novice Talker

Posts: 5
Trades: 0
I used left join, but still get the wrong results. It returned Person2 and Person3 when only Person3 needs to be returned.

The query I used is
Code:
SELECT DISTINCT * FROM Accounts LEFT JOIN Friends ON Accounts.ID = Friends.AccountID WHERE Friends.FriendID = 'Person1'
I'm actually kind of thinking this is not possible. I think the author of the article may not have tested his ideas.
scotty2024 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Social Networking Database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.23339 seconds with 12 queries