March 11, 2011 at 9:43 pm
Hello friends , I am newbie here.
In my project a user can have multiple friends and I need to store friends of a each user. What I am doing is that , in my table , I have created two columns ; one for username and second for friends.
When a user adds a new friend name 'newfriend', I update like this :
"update table set friend=friend+'newfriend|' where user=myuser"
Then, when I need to select different friends, I fetch friend column and split the value by '|' to get an array of friends.
This code is working fine. But I know, you geniuses have something better in mind.
March 11, 2011 at 9:57 pm
What approach are u following to split, currently ?
March 11, 2011 at 10:06 pm
I am spliting in front end (ASP.Net) by .Split('<char>') function of strings.
March 11, 2011 at 10:53 pm
jashwantsingh (3/11/2011)
Hello friends , I am newbie here.In my project a user can have multiple friends and I need to store friends of a each user. What I am doing is that , in my table , I have created two columns ; one for username and second for friends.
When a user adds a new friend name 'newfriend', I update like this :
"update table set friend=friend+'newfriend|' where user=myuser"
Then, when I need to select different friends, I fetch friend column and split the value by '|' to get an array of friends.
This code is working fine. But I know, you geniuses have something better in mind.
How do you remove a friend? Split it in the front end and then reassemble it except for the friend you want to remove?
--Jeff Moden
Change is inevitable... Change for the better is not.
March 11, 2011 at 11:14 pm
Yeah...I will do the same...I will remove friend from front end.
I think, its not a good idea. Can you suggest something better ? How do you people deal with such type of problems ?
March 12, 2011 at 12:49 am
jashwantsingh (3/11/2011)
Yeah...I will do the same...I will remove friend from front end.I think, its not a good idea. Can you suggest something better ? How do you people deal with such type of problems ?
Basically, anytime you have delimited data in one column to represent multiple occurrences, then you should be looking at a child table. For instance (grossly simplified):
CREATE TABLE Users (UserID INT IDENTITY CONSTRAINT PK_Users PRIMARY KEY,
Name varchar(50) CONSTRAINT UQ_Users_Name UNIQUE);
CREATE TABLE UserFriends (UserID INT REFERENCES Users(UserID),
FriendName varchar(50),
CONSTRAINT PK_UserFriends PRIMARY KEY (UserID, FriendName));
INSERT INTO Users VALUES ('WayneS');
INSERT INTO UserFriends VALUES (1, 'jashwantsingh');
INSERT INTO UserFriends VALUES (1, 'Jeff Moden');
INSERT INTO UserFriends VALUES (1, 'ColdCoffee');
To add a new friend, just insert it into the UserFriends table. To remove a friend, just delete it.
To make your delimited list for the front end:
SELECT u.Name, Friends = STUFF((SELECT '|'+uf.FriendName
FROM UserFriends uf
WHERE uf.UserID = u.UserID
ORDER BY uf.FriendName
FOR XML PATH(''),TYPE).value('.','varchar(max)'),1,1,'')
FROM Users u;
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
March 12, 2011 at 7:38 am
Everything is good there except I wouldn't make the delimited list for the front end. Just return a result set and consume it at the front end. I guess that's a matter of choice.
--Jeff Moden
Change is inevitable... Change for the better is not.
March 12, 2011 at 7:48 am
yeah...that would be better if I do processing at backend itself and just return to front end what is really needed. But my sql server skills really sucks...I can do simple select, insert, update commands ( the ones needed to learn sql).
If you can help me out in building such strong queries which can split string or can check if string possesses particular characters...or anything like that , which can help me.
Viewing 8 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply