例如,我无法弄清楚如何为一位客户提供多个地址。有关如何实现这一点的任何想法?
由于
答案 0 :(得分:1)
在地址表中使用外键
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)
)
这可用于实现一对多关系。
例如,如果你有Customer
乔治C_ID=55
,那么如果你想给乔治一个新地址,你就可以在地址表中插入一个条目,其值为5作为C_ID。
insert into addresses (C_ID,blah,blah) values (55,blah,blah)
如果你想得到乔治的所有地址,你会说:
select * from addresses where C_ID=55
如果这没有意义,这里有图片http://net.tutsplus.com/tutorials/databases/sql-for-beginners-part-3-database-relationships/