user1876281
user1876281

Reputation: 21

How do you get multiple addresses to one customer?

I cant work out how you get multiple addresses to one customer, for example. Any ideas on how to implement this?

Thanks

Upvotes: 0

Views: 3049

Answers (1)

Sheena
Sheena

Reputation: 16212

Use a foreign key in the addresses table

CREATE TABLE Customers
(
C_Id int NOT NULL,
other_stuff whatever,
PRIMARY KEY (C_Id),
)

CREATE TABLE Addresses
(
 A_ID int NOT NULL,
 C_ID int NOT NULL,
 other_stuff whatever,
 FOREIGN KEY (C_Id) REFERENCES Customers(C_Id),
 PRIMARY KEY (A_ID)
)

This can be used to implement a one-to-many relationship.

For example if you have George the Customer with C_ID=55 then if you wanted to give George a new address then you would insert an entry into the addresses table with the value of 5 as C_ID.

insert into addresses (C_ID,blah,blah) values (55,blah,blah)

and then if you wanted to get all of George's addresses you would say:

select * from addresses where C_ID=55

If that didn't make sense, this here has pictures http://net.tutsplus.com/tutorials/databases/sql-for-beginners-part-3-database-relationships/

Upvotes: 2

Related Questions