Mysql 创建表时快捷方式指定主键

Mysql 创建表时快捷方式指定主键,mysql,Mysql,创建表时,使用快捷方式查询分配主键的正确方法是什么 下面是我的快捷方式的示例: 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` decima

创建表时,使用快捷方式查询分配主键的正确方法是什么

下面是我的快捷方式的示例:

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主键自动递增,
    我会得到错误:
    重复的键名“booking\u product”

如果使用
constraint booking\u product\u id
,则不会出现关于
重复键名“booking\u product”
的错误,因为SQL解析器会在第一个错误时停止

放下
constraint booking\u product\u 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

如果使用
constraint-booking\u-product\u-id
,则不会出现关于
重复键名“booking\u-product”
的错误,因为SQL解析器会在第一个错误时停止

放下
constraint booking\u product\u 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

我知道你选择了一个答案,但是当我将你的create语句简化为下面的代码时,我让它开始工作了。现在,通过Oswald添加的“drop constraint”请求,您可能已经拥有了所需的一切:

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)
create table booking\u产品(
id int(10)非空自动增量,

主键(id),我知道您选择了一个答案,但当我将您的create语句简化为下面的代码时,我让它开始工作。现在,通过Oswald添加的“drop constraint”请求,您可能已经拥有了所需的一切:

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)
create table booking\u产品(
id int(10)非空自动增量,

主键(id),您知道
int(10)
不会将值限制为10位吗?@a_horse_和\u no_name是
int(10)的意思吗
最大值不是
9.999.999.999
?不,它只意味着整数的显示宽度是10,它不限制值的范围。任何有符号整数的数字范围,包括INT(10)、INT(5)或任何其他INT(n)是:-2147483648…2147483647,最多10位。您知道
INT(10)
不会将值限制在10位吗?@a_horse_与_no_name是指
int(10)
最大值不是
9.999.999
?不,它只意味着整数的显示宽度是10,它不限制值的范围。任何有符号int的数值范围包括int(10)、int(5)或任何其他int(n)is:-2147483648…2147483647,最多10位。是的,我得到错误
重复键相同
。这似乎是因为在此之前我已经创建了表名
booking\u product
,然后删除了该表。但我不明白。如果我删除了该表,是否也应该从该删除的表中删除主键?我尝试了你的代码
DROP CONSTRAINT
booking_product_id
它给了我这个错误:
你的SQL语法有错误;检查与你的MySQL服务器版本对应的手册,在第1行的“CONSTRAINT
booking_product_id
”附近使用正确的语法>主键
。但是从外键开始。例如,
currencyId\u fk(currencyId)
()
之后的字符串我认为是一个表名。所以它是键名。我的坏@u@。对!语法是
外键[KeyName]([Columns])引用[OtherTable]([OtherColumns])
。是的,我得到了错误
相同的重复键
。这似乎是因为在此之前我已经创建了表名
booking\u product
,然后删除了该表。但我不明白。如果我删除了该表,是否也应该从该删除的表中删除主键?我尝试您的代码
drop约束
booking\u product\u id
它给了我这个错误:
您的SQL语法有错误;请查看与您的MySQL服务器版本对应的手册,以了解第1行的“CONSTRAINT
booking\u product\u id
”附近使用的正确语法。哦,我理解。错误不是来自
主键,而是外键。例如
currencyId_fk(currencyId)
我认为
()
后面的字符串是一个表名。因此它是一个键名。我的错误@对!语法是
外键[KeyName]([Columns])引用[OtherTable]([OtherColumns])