Nov 21, 2009
Pages: 1, 2

Linking Two Tables

free web hosting

Read Latest Entries..: (Post #12) by iGuest on May 2 2008, 10:54 AM.
How to link two tables in sql using primary and foriegn key-reply by shofiya
Read the FIRST post of this Topic. - Express your Opinion! Contribute Knowledge :-).

Open Discussion & Free Web Hosting > Computers & Tech > Databases

Linking Two Tables

ginginca
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?

Comment/Reply (w/o sign-up)

ruben
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

Comment/Reply (w/o sign-up)

doudou
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.

Comment/Reply (w/o sign-up)

ginginca
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.

Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

Hercco
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.

Comment/Reply (w/o sign-up)

msabas
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

Comment/Reply (w/o sign-up)

SP Rao
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.

 

 

 


Comment/Reply (w/o sign-up)

minnieadkins
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

Comment/Reply (w/o sign-up)

SP Rao
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

Comment/Reply (w/o sign-up)

Latest Entries

iGuest
How to link two tables in sql using primary and foriegn key

-reply by shofiya

Comment/Reply (w/o sign-up)

masterio
Hi guys, I have some question for you. From many articles that I've read. I only found about joining two tables. But I didn't find about joing more than two tables, three tables, and even more!.

Is joining more than two tables same as joining more that 2 tables?. I little confuse about it. If joining 2 tables we simply using query like this:

CODE

SELECT table1.column1, table1.column2, table1.column3 FROM table1, table2 WHERE table1.column2='VALUE'


Help is very appreciate! biggrin.gif

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Pages: 1, 2
Similar Topics

Keywords : linking, tables

  1. Mysql Multiple Tables
    (3)
  2. Mysql Problem
    Linking tables (1)
    Hello guys, I'm making a PHP/MySQL site for a friend of mine who plays an online game where you
    can create towns, add buildings to them and upgrade them to a newer level. I've offered to make
    him a small site where he can input his data into a database and keep track of his towns, buildings
    and levels. I'll show you my database design and then explain what i want to do. Table: towns
    field 1: TownID (primary key, auto-increment) field 2: TownName field 3: TownType field 4: Town
    Description Table: buildings field 1: BuildingID (primary key, auto-incremen....
  3. Script Tables On Sql Server Compact
    (0)
    So I have an application that's using SQL Server Compact Edition 2005. I need to export the
    table schema to a file of SQL statements, containing only the schema...no data, no rows, just the
    tables, indexes, and constraints. Now, when using SQL 2000/2005 I just use the script table as...
    feature to do exactly this, but because SQL server Compact Edition doesn't have the support for
    it, I can't do this. The tables in SQL Server Compact *do* have the information_schema views
    though, so I was wondering if anyone knows of any tools that can read these views and....
  4. (help With Sql And Php)retrive Datas From Realted Tables And Display Them!
    Please help me to diplay datas by combining 3 tables! (4)
    Hi all.. Please help me to write the SQL Query for the following requirement! Im having 3 MySQL
    tables Sales_users Sales_products Sales_details 1st Table (Sales_users) contains unique userID
    and username for the users! 2nd Table (Sales_products) Contains unique Product ID, product Name and
    Product Description! 3rd Table (Sales_details) contacins unique Sales ID, Product ID (Foreign Key),
    FromUser (Foreign Key) and ToUser (Foreign Key) Sales_users
    User_id name 1 Test1 ....
  5. MySQL, Multiple Tables
    (27)
    Ok, I'm coding a project which is a leap than what I'd normally do. Before, I've always
    learned ONE table... put EVERYTHING in one database table. I'm making a profile system so there
    needs to be at least two tables: 1 for users, 1 for content. My problem is, how do I link the two
    together? I could probably figure this out faster if someone explained and posted sample SQL code
    that shows how the two are linked together. Thanks!! F....
  6. Updating A Database's Tables
    (11)
    Is there an "easy" way to update a database's tables? Like for instance, I have my own
    custom-coded member login system. Whenever I add a new feature that needs a database, I manually
    have to download, update and upload the database. Is there an easy way I can do this? THANKS!!! F....
  7. User Priveileges Vs. Tables Vs Rows
    which method is best for accounts/logins (1)
    I want to know which is the most efficient way to create accounts and logins. I have trouble
    deciding which is the better of the three: - User Priveileges - TABLES - Rows....
  8. Creating Tables In MySQL On Home Comp
    (8)
    Hi, I've tried seraching for a bit, but I couldn't find an answer to my questions.
    Recently I've succesfully installed Apache on my Windows XP Machine, along with PHP and mySQL. I
    need a database on my computer for testing purposes. I've enables mySQL in PHP, and both seems
    to be working fine. I can connect to the database, but how do I edit the database? E.g., edit rows
    or modify columns. I would much rather stay away from using the command-line stuff. I'm looking
    for a GUI Query program that I can use, but I have no clue which one to choose. It ....
  9. Recover Tables From A MySQL .frm File
    (9)
    I have a couple of .frm files with no corresponding data or index files. Is it possible to recover
    the table structure (field names, types, sizes, rows,col, etc) from these files? The table type is
    innodb....
  10. Generating Many Tables
    (2)
    I have a database that will contain 43 tables (among others). For these particular tables, they will
    each contain the same type of information, each representing a particular week in the NASCAR season.
    I used the following script to create one table: DROP TABLE IF EXISTS gulfOwnerDriversWeek1;
    CREATE TABLE gulfOwnerDriversWeek1 ( ownerIndex int primary key, driverIndex1 int, driverIndex2 int,
    driverIndex3 int, driverIndex4 int ); This table represents a draft sheet for one particular week
    (Week 1) for a particular team. My first question is, should try to put all week....
  11. Selection From Multiple Tables
    (0)
    I am in the middle of creating a little forums system, and all so far is going quite well. However I
    have come across a small problem; PHP:
    --------------------------------------------------------------------------------
    $gather_forums_qry = "SELECT a.frm_id, a.frm_name, a.frm_description,
    count(distinct(b.fth_id)) ThreadCount, count(distinct(c.fpt_id))
    PostCount, a.frm_enabled, IF( IFNULL( MAX(
    b.fth_dateposted), 0) > IFNULL( MAX( c.fpt_datepo....

    1. Looking for linking, tables

See Also,

*SIMILAR VIDEOS*
Searching Video's for linking, tables
advertisement



Linking Two Tables

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com