你不能(直接)。關係數據庫沒有數組;他們有表格(關係)和他們之間的關係。因此,SQL不具有數組的概念。
你可以這樣做:
create table foo
(
id int not null primary key ,
col_01 varchar(200) null ,
col_02 varchar(200) null ,
...
col_nn varchar(200) null ,
)
但那是non-normal,違反1st Normal Form:它已經重複的組。
你想要的模式是一樣的東西
create table book
(
id int not null primary key , -- primary key
isbn varchar(32) null ,
title varchar(80) not null , -- can't use title as a key as titles are not unique
)
-- you might have multiple copies of the same book
create table book_copy
(
book_id int not null ,
copy_number int not null ,
primary key (book_id , copy_number) ,
foreign key (book_id) references book(id) ,
)
create table customer
(
id int not null primary key ,
surname varchar(32) not null ,
name varchar(32) not null ,
)
create table customer_has_book
(
customer_id int not null ,
book_id int not null ,
copy_number int not null ,
primary key (customer_id , book_id , copy_number) , -- customer+book+copy number is unique
unique (book_id , copy_number) , -- a given copy may only be borrowed one at a time
foreign key (customer_id) references customer(id) ,
foreign key (book_id , copy_number) references book_copy(book_id,copy_number) ,
)
好吧,那麼我不得不checkedout.name具有參考借款人(名稱)和checkedout.bookTitle引用標題(BOOKTITLE) – mring 2012-02-22 19:46:10
我與BD同意。不過,我認爲你應該添加自動遞增主鍵,交叉表應該引用這些鍵,而不是名稱。我認爲這將解決您的擔憂。 – northpole 2012-02-22 19:49:09
@northpole你正在談論自然與替代鍵。這兩種方法都有其優點,但沒有一個是絕對「更好」的 - 這取決於具體情況。 – 2012-02-22 20:01:53