Welcome Guest ( Log In | Register )



2 Pages V   1 2 >  
Reply to this topicStart new topic
> Linking Two Tables
ginginca
post Aug 22 2006, 01:10 PM
Post #1


Premium Member
Group Icon

Group: Members
Posts: 250
Joined: 6-July 06
From: The net (or at least that's what my family says)
Member No.: 14,330



I have two tables in a database. userid and usercredits

In userid I have all the fields that identify the user (name, address etc.)

In my usercredits table I have the following fields:

IDNUM
ProgramName
Camp#
CompletionDate
Amount
DatePmtSent
PmtMethod
Comments
creditID


What I would like to understand about my table structure, is how do I associate my credits tothe correct user. If I have done this correctly, there is also a field in the first table that is called IDNUM where each user has a unique number to identify who they are.

When someone is logged into the site I need to display the values from the usercredits table that are only THEIR credits.

Am I starting this off correctly?
Go to the top of the page
 
+Quote Post
ruben
post Aug 22 2006, 03:39 PM
Post #2


Wheeeeeeee!
Group Icon

Group: Members
Posts: 245
Joined: 19-October 05
From: DG, Belgium
Member No.: 9,200



Yes, you're perfectly right.
In your user-table you would probably have one entry per user only, so that the ID is a unique value.
Then, when the user logs in you could get all the entries from the usercredits table that contain his userid ("SELECT Amount FROM usercredits WHERE IDNUM = 'user'") and sum them up or whatever. It might be a good idea to keep count of the current total credit number in a separate table and rename the current "usercredit" table to "usertransactions" or so. This would save you the extra effort of summing up the credits total.

Seems fine to me otherwise,
Ruben
Go to the top of the page
 
+Quote Post
doudou
post Aug 22 2006, 05:12 PM
Post #3


Advanced Member
Group Icon

Group: Members
Posts: 114
Joined: 1-July 06
Member No.: 14,234



You need one field unique field (where the value is unique) in both table so that they can be linked together. For example you can use IDNUM as your primary key in both tables and link the tables using that field.
Go to the top of the page
 
+Quote Post
ginginca
post Aug 24 2006, 06:12 PM
Post #4


Premium Member
Group Icon

Group: Members
Posts: 250
Joined: 6-July 06
From: The net (or at least that's what my family says)
Member No.: 14,330



QUOTE(doudou @ Aug 22 2006, 01:12 PM) *

You need one field unique field (where the value is unique) in both table so that they can be linked together. For example you can use IDNUM as your primary key in both tables and link the tables using that field.


Yes I set it up as the primary key in both. So it sounds like I'm on the right track here.
Go to the top of the page
 
+Quote Post
msabas
post Aug 27 2006, 06:15 PM
Post #5


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 50
Joined: 19-July 06
Member No.: 14,610



do you guys have any tutorials on this as id like to learn some of this
Go to the top of the page
 
+Quote Post
Hercco
post Aug 27 2006, 06:26 PM
Post #6


Super Member
Group Icon

Group: Members
Posts: 595
Joined: 4-September 04
Member No.: 228



QUOTE(msabas @ Aug 27 2006, 09:15 PM) *

do you guys have any tutorials on this as id like to learn some of this


There's tons of them in the web. Type in your favourite search engine: "SQL tutorial" and you'll get bunch of really good tutorials with lots of examples. Once you know the basics the DBMS's developer's online reference manual is probably the most handy tool to use. I've been doing database applications for years now but still need to check things from dev.mysql.com/doc regularly.

This post has been edited by Hercco: Aug 27 2006, 06:26 PM
Go to the top of the page
 
+Quote Post
msabas
post Aug 27 2006, 06:28 PM
Post #7


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 50
Joined: 19-July 06
Member No.: 14,610



I was hoping you guys had posted some here.. i tried looking up a few thru the search.. but no luck.. and yeah i guess i will have to use th eweb to look themup.. thnx
Go to the top of the page
 
+Quote Post
SP Rao
post Aug 29 2006, 02:33 PM
Post #8


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 70
Joined: 29-August 06
Member No.: 15,594



QUOTE(ginginca @ Aug 22 2006, 01:10 PM) *

I have two tables in a database. userid and usercredits

In userid I have all the fields that identify the user (name, address etc.)

In my usercredits table I have the following fields:

IDNUM
ProgramName
Camp#
CompletionDate
Amount
DatePmtSent
PmtMethod
Comments
creditID
What I would like to understand about my table structure, is how do I associate my credits tothe correct user. If I have done this correctly, there is also a field in the first table that is called IDNUM where each user has a unique number to identify who they are.

When someone is logged into the site I need to display the values from the usercredits table that are only THEIR credits.

Am I starting this off correctly?


Ok. Your table structure looks fine. You say that you IDNUM as the common field between two tables. As long as it is unique in bothe the tables your table structure will alllow you to join the tables perfectly. Are you planning to have a row in "usercredits" for every user in userid? May be it doesn't matter, but if you don't have any row in usercredits for a particular userid, you'll have to handle the exception of "Now Rows Found".

Yes, you are starting off correctly. You can use IDNUM for joining the tables.

This post has been edited by SP Rao: Aug 30 2006, 04:12 AM
Go to the top of the page
 
+Quote Post
minnieadkins
post Aug 29 2006, 03:36 PM
Post #9


Premium Member
Group Icon

Group: Members
Posts: 292
Joined: 15-December 04
Member No.: 1,768



So this is a one to many relationship
One user can multiple credits records in the table.

Here should be somewhat of an answer to your design:

User Table
-----------
userid (primary key)
Name
address
phone
(etc)

Credit Table
-------------
creditID (primary key)
userid (foreign key)
Camp#
CompletionDate
Amount
DatePmtSent
PmtMethod
Comments


So in order to call an entry from the credit table you must have both keys (foreign and partial primary key).

SQL would look something like this
CODE

SELECT U.name, C.completiondate, C.amount, C.datepmtsent, C.pmtmethod, C.comments
FROM users U
JOIN credit C ON U.userid=C.userid
ORDER BY C.datepmtsent ASC


I guess that would give u all the payments made by a user. Obviously if you wanted a single payment you would have to identify the appropriate where clauses and give the correct arguements.

Lots of reading on this topic but I guess this would be as good of a place as any. I suggest that you pay particular attention to the ERD's as they will probably explain most of the text if you understand them.

http://dev.mysql.com/tech-resources/articl...malization.html
Go to the top of the page
 
+Quote Post
SP Rao
post Aug 31 2006, 05:58 AM
Post #10


Member [ Level 2 ]
Group Icon

Group: Members
Posts: 70
Joined: 29-August 06
Member No.: 15,594



Well, as ginginca was asking for some guidance, there are a lot of stuff in the web. Somehow I feel that you don't need to search particularly for MySQL tutorials alone. It's better you learn the basica of SQL first. (May be little bit about RDBMS concepts).

Now, there are million sites on net which will provide you information about SQL. The one which is particularly good for beginners is http://sqlcourse.com/. This site offers online interactive tutorial where you can test what you are learning too.

Moreover, you will get info and updates on almost all well known databases MySQL, Oracle, MS SQL etc.
I think this site will definitely help you in learning the SQL basics.

You may also try W3Schools tutorials for SQL SQL Tutorial

For RDBMS concepts and data modelling, any google search will help you.
Happy programming biggrin.gif

This post has been edited by SP Rao: Aug 31 2006, 06:02 AM
Go to the top of the page
 
+Quote Post

2 Pages V   1 2 >
Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Recover Tables From A MySQL .frm File(6)
  2. Creating Tables In MySQL On Home Comp(8)
  3. User Priveileges Vs. Tables Vs Rows(1)
  4. Updating A Database's Tables(10)
  5. MySQL, Multiple Tables(22)
  6. (help With Sql And Php)retrive Datas From Realted Tables And Display Them!(4)
  7. Script Tables On Sql Server Compact(0)
  8. Mysql Problem(1)


 



- Lo-Fi Version Time is now: 7th July 2008 - 02:59 AM