MySQL: Create a relation between two tables.


 
Thread Tools Search this Thread
Top Forums Programming MySQL: Create a relation between two tables.
# 1  
Old 01-29-2011
MySQL: Create a relation between two tables.

Hello everybody,
I'm having troubles creating a relation between two tables in a MySQL database.
Having two tables, being one which contains users information (username, password, user ID, etc) and the other the one which contains transactions information (operation type, user ID of the user who did the transaction, etc), How can i create a relation between any transaction done by each user?

I tried making user ID a primary key for both tables, but when i try to create a second entry with the same user ID on the 'Transactions' table, MySQL tells me that it must be unique, since it's a primary key.

What i want is to run a query for matching all the "transactions" made by an specific "user ID" , How can i do that?

Thanks in advance, and please excuse my poor english
# 2  
Old 01-30-2011
ok so a primary key in table A is a foreign key in table B (not a primary)
so table A table B
uid - primary key ser_id - Primary key
name uid - foreign key
password trans_id

then
select A.*, B.trans_id
from A,B
where A.uid = B.uid

so a 3rd normal form of relational DB.
# 3  
Old 01-30-2011
Also, you have to use InnoDB tables to use features like foreign keys. Normal MyISAM tables won't do.
Login or Register to Ask a Question

Previous Thread | Next Thread

6 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script automation using cron which query's MySQL Tables

What I have: I have a input.sh (script which basically connect to mysql-db and query's multiple tables to write back the output to output1.out file in a directory) note: I need to pass an integer (unique_id = anything b/w 1- 1000) next to the script everytime I run the script which generates... (3 Replies)
Discussion started by: kkpand
3 Replies

2. Shell Programming and Scripting

UPDATE COmmand post comparing 2 columns in 2 mysql tables

my queryis : select distinct m.name, item_count, item from master m join client p on m.name=p.name where item_count = 1 and item > 1; But how should I update them? i used update statetment : Update from client Set item =1 where m.name=p.name and item_count=1 AND item>1 Is this wrong? (1 Reply)
Discussion started by: siya@
1 Replies

3. Shell Programming and Scripting

Show only new and removed records by comparing to MySQL tables

Hello all; I have been really frustrated with finding the correct perl code (and MySql statements) to accomplish what I thought was straight forward...I have tested I don't know how many different codes\suggestions I found on the net without any success...but anyhow let me explain my plight and... (0 Replies)
Discussion started by: gvolpini
0 Replies

4. Programming

MySQL join four tables!

Hello; I want merge four MySQL tables to get the intersection that have a common field for all of them. Join two tables is fine to me, but my this case is different from common situations and there are not very many discussions about it. Can anybody give me some idea? Thanks a lot! Here is part... (8 Replies)
Discussion started by: yifangt
8 Replies

5. Web Development

How do you sync tables in mysql between two different servers?

Hi all, I have 2 mysql databases running on two different servers (both can be accessed via ssh to each other). If I have say table ABC on server 1, how can I sync it with table ABC on server 2 if the number of records is different? Both databases are the same structure though server 2 has... (2 Replies)
Discussion started by: muay_tb
2 Replies

6. UNIX and Linux Applications

create 'day' tables based on timestamp in mysql

How would one go about creating 'day' tables based on the timestamp field. I have some 'import' tables which contains data from various days and would like to spilt that data up into 'days' based on the timestamp field in new tables. TABLE_IMPORT1 TABLE_IMPORT2 TABLE_IMPORT3 ... (2 Replies)
Discussion started by: hazno
2 Replies
Login or Register to Ask a Question