• Welcome to Valhalla Legends Archive.
 

SQL Databases

Started by Archangel, May 26, 2008, 10:14 PM

Previous topic - Next topic

Archangel

I'm starting to learn how to use SQL databases and i was wondering if its ok to have 2 tables for the users infromation, for example:

Having table: USERS with the rows: USER_ID USERNAME PASSWORD
and then having: USERS_INFO with the rows: USERS_INFO_ID ADRESS PHONE

the idea is having 2 separate tables to manage the users information, is this a correct way of doing this?

If so, what command should i be using to request the user adress using the user_id?

Edit: im new to this theme, so dont go hard with me :).
I'm not an Addict.

Barabajagal

One table, multiple fields.

Archangel

Quote from: Andy on May 27, 2008, 12:24 AM
One table, multiple fields.

k, thanks :]. I will post if i got any more doubts.
I'm not an Addict.

FrOzeN

#3
It really depends on the data you'll be storing. You have to determine how flexible your database needs to be. If you just wanted to know the user's main address, then putting it all into one table would probably be sufficient. But say the information you were collecting required room to allow for client's that have two or more addresses. In that scenario you would create a second table which stored all the address' and referenced each address to a ClientID.
~ FrOzeN

MyndFyre

This is a process we call "normalizing a database."  There's a pretty great Wikipedia article about it and lots of Google pages.

Basically, you should take a look at the logical objects that you'll be storing; where there are relationships of 1:n or m:n, these are almost always prime candidates for normalization.

In your user table, for instance, consider that you might want to store a list of user's IM contact info.  So, consider that a user object might be represented:


class CUser
{
    public:
        const std::string& GetUserName() const;
        const std::string* GetProfileIMLinks() const;
        const int GetProfileIMLinkCount() const;
    private:
        // etc
}

This class represents a user that has multiple IM links in his profile.  Your software might define an IM link as a hypertext link that can include the user's name.  For instance, all AIM links might be "aim:goim?user=%%USERNAME%%

A database schema for this might looks something like this (this is T-SQL for MS-SQL products):

CREATE TABLE [Users]
{
    UserID int NOT NULL IDENTITY(1,1) PRIMARY KEY,
    UserName nvarchar(75) NOT NULL
}

CREATE TABLE [IMLinks]
{
    ProtocolID int NOT NULL IDENTITY(1,1) PRIMARY KEY,
    ProtocolName nvarchar(10) NOT NULL UNIQUE,
    ProtocolLinkFormat nvarchar(125) NOT NULL
}

CREATE TABLE [UserIMs]
{
    UserID int NOT NULL PRIMARY KEY REFERENCES [Users](UserID) ON DELETE CASCADE,
    ProtocolID int NOT NULL PRIMARY KEY REFERENCES [IMLinks](ProtocolID) ON DELETE CASCADE,
    UserName nvarchar(30) NOT NULL
}


With these three tables you have everything you need to have unlimited IM protocol fields and unlimited IM profiles per user.  Consider:
Users:
1  MyndFyre 
2  Archangel

IMLinks:
1  AIM  aim:goim?userid=%%USERNAME%%
2  MSN  http://members.msn.com/%%USERNAME%%
3  GTalk  googletalk:talk?userid=%%USERNAME%%   (Note: I have no idea if these work)

UserIMs:
1  1  MyndFyre
1  2  [email protected]
1  3  [email protected]
2  2  [email protected]

Do you see how there are links by ID across the different tables?  These relational links allow us to make sure that our database integrity is good as well as avoid repeating information.  NOTE: Although UserIMs has "[email protected]" for both 1/2 and 1/3, realize that this information is actually separate.  They're separate addresses, one for MSN and one for Google Talk.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Banana fanna fo fanna

i wouldn't use sql if i were you. the technology is outdated and is barely in use anymore. only a few vendors support the technology until its end of life later this year. no real company still uses a sql database these days. sql is slow, inflexible, and overall lacking in relevance to any career plans you may have.

instead, i would use ajax.

iago

Quote from: Banana fanna fo fanna on June 01, 2008, 12:08 PM
i wouldn't use sql if i were you. the technology is outdated and is barely in use anymore. only a few vendors support the technology until its end of life later this year. no real company still uses a sql database these days. sql is slow, inflexible, and overall lacking in relevance to any career plans you may have.

instead, i would use ajax.
Just to be clear (for newbies), that post didn't make sense. :)
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


Invert

Quote from: Banana fanna fo fanna on June 01, 2008, 12:08 PM
i wouldn't use sql if i were you. the technology is outdated and is barely in use anymore. only a few vendors support the technology until its end of life later this year. no real company still uses a sql database these days. sql is slow, inflexible, and overall lacking in relevance to any career plans you may have.

instead, i would use ajax.

^
|
This is the dumbest thing I have ever read on the Web Development forums.

Banana fanna fo fanna

im super serial guys.

Don Cullen

Quote from: Invert on June 07, 2008, 04:21 PM
Quote from: Banana fanna fo fanna on June 01, 2008, 12:08 PM
i wouldn't use sql if i were you. the technology is outdated and is barely in use anymore. only a few vendors support the technology until its end of life later this year. no real company still uses a sql database these days. sql is slow, inflexible, and overall lacking in relevance to any career plans you may have.

instead, i would use ajax.

^
|
This is the dumbest thing I have ever read on the Web Development forums.

Seconded.
Regards,
Don
-------

Don't wonder why people suddenly are hostile when you treat them the way they shouldn't be- it's called 'Mutual Respect'.

Grok

Quote from: Banana fanna fo fanna on June 01, 2008, 12:08 PM
i wouldn't use sql if i were you. the technology is outdated and is barely in use anymore. only a few vendors support the technology until its end of life later this year. no real company still uses a sql database these days. sql is slow, inflexible, and overall lacking in relevance to any career plans you may have.

instead, i would use ajax.

???   :o

Nonsense.

bulletproof tiger

What does that even mean...

macyui

Quote from: Grok on November 09, 2008, 12:38 PM
Quote from: Banana fanna fo fanna on June 01, 2008, 12:08 PM
i wouldn't use sql if i were you. the technology is outdated and is barely in use anymore. only a few vendors support the technology until its end of life later this year. no real company still uses a sql database these days. sql is slow, inflexible, and overall lacking in relevance to any career plans you may have.

instead, i would use ajax.

uh, i work at zynga and we use the following:
mysql database
memcache
mongoDB for persistence
php to wrap facebook api
java and smartfox for socket server
flash client with some javascript


what is wrong with mysql btw?

rabbit

Dunno.  AJAX doesn't replace a database anyway....
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

smithshn

You can use the normalization. Normalization can sort the data and it manage the database properly.Therefore, you can access your database as fast in minimum duration by query.