Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am working with something and I came up with a question..

I created a database with one table in it, in this table I have the primary key(autogenerated ID), name, surname,email etc...
The above are describing a customer. But say if I want more fields to describe one customer(not so important like weight, height, shape etc..) but I didn't want to put them in the above table, what can I do?

Can I create a second table? but when I modify one customer from the first table with the ID how can I access the correct data for this customer from the second table without ID?

hope you understand..
thanks.
Posted
Comments
Tom Marvolo Riddle 30-Oct-13 3:54am    
use customerid(autogenerated) as common in both tables.Then use inner join for retrieving data

Yes, you can - it probably isn't necessary to create a separate table (since you can specify which fields you want to retrieve when you SELECT them):
SQL
SELECT name, surname FROM MyTable WHERE ID = 22
but if you do, then you set up a new table (with it's own ID column) and what is called a Foreign Key column which contains the ID value from the original table:
C#
ID      int, identity
CustID  int, foreign key
Weight  float
Height  float
You can then retrieve them both together like this:
SQL
SELECT a.name, a.surname, b.Weight, b.Height FROM MyTable a
JOIN MyOtherTable b ON a.ID=b.CustId
WHERE a.ID = 22
 
Share this answer
 
Comments
zapata287 30-Oct-13 4:33am    
thank you a lot
OriginalGriff 30-Oct-13 4:45am    
You're welcome!
Hello,

What you need is to create the another table and set the customerid as the foreign key in that table. Below MySQL snippet should help you.
SQL
CREATE TABLE customer_mst (
    customerid MEDIUMINT NOT NULL AUTO_INCREMENT,
    firstname  VARCHAR(64),
    lastname   VARCHAR(64),
    emailed    VARCHAR(255),
    ...,
    PRIMARY KEY (customerid)
);

CREATE TABLE customer_info (
    infoid     MEDIUMINT NOT NULL AUTO_INCREMENT,
    customerid MEDIUMINT NOT NULL,
    weight     MEDIUMINT DEFAULT 0,  
    height     MEDIUMINT DEFAULT 0,
    ...,
    PRIMARY KEY (infoid),
    FOREIGN KEY (customerid) REFERENCES customer_mst(customerid)
)

Regards,
 
Share this answer
 
Comments
zapata287 30-Oct-13 4:33am    
thank you, very helpful!
Prasad Khandekar 31-Oct-13 8:35am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900