0

テーブルの作成中にショートカットクエリで主キーを割り当てる正しい方法は何ですか?

これが私のショートカットの例です:

create table booking_product (
`id` int(10) not null constraint `booking_product_id` primary key auto_increment ,
`bookingId` int(10) not null,
`serviceId` int(10) not null,
`date` date not null,
`price` decimal(30,15) not null,
`qty` int(1) not null,
`currencyId` int(10) not null,
`total` decimal(30,15) not null,
`roomInclusion` text null default null,

foreign key booking_product(bookingId) references booking(id) on update cascade on delete cascade,
foreign key booking_product(serviceId) references service(id) on update cascade on delete set null,
foreign key booking_product(currencyId) references currency(id) on update cascade on delete set null
) engine = InnoDB;
  • 行 2 で、主キーを割り当てようとしましたが、このクエリは正しくなく、エラーが発生します。のみを使用しようとするid int(10) not null primary key auto_increment ,と、エラーが発生します:Duplicate key name 'booking_product'
4

2 に答える 2

3

を使用すると、SQL パーサーが最初のエラーで停止するため、constraint booking_product_idエラーは発生しません。Duplicate key name 'booking_product'

落としconstraint booking_product_idて使う

foreign key bookingId_fk(bookingId) references booking(id)
    on update cascade
    on delete cascade,
foreign key serviceId_fk(serviceId) references service(id)
    on update cascade
    on delete set null,
foreign key currencyId_fk(currencyId) references currency(id)
    on update cascade
    on delete set null
于 2013-03-29T19:09:37.230 に答える
1

あなたが答えを選んだことは知っていますが、作成ステートメントだけを以下のコードに単純化すると、それが機能しました。Oswald による「ドロップ制約」リクエストが追加されたので、必要なものがすべて揃っている可能性があります。

create table booking_product (
id int(10) not null auto_increment,
PRIMARY KEY(id),  <<<<< this worked for me...
bookingId int(10) not null,
serviceId int(10) not null,
date date not null,
price decimal(30,15) not null,
qty int(1) not null,
currencyId int(10) not null,
total decimal(30,15) not null,
roomInclusion text null default null)

繰り返しますが、私はあなたが質問したことから質問に近づいているだけです。

お役に立てれば。

于 2013-03-29T19:27:07.643 に答える