gpt4 book ai didi

SQL Server : error creating table with multiple primary keys

转载 作者:行者123 更新时间:2023-12-03 02:10:44 28 4
gpt4 key购买 nike

查询如下:

create TABLE tbl_temp (
[ref] numeric(18),
[item_code] varchar(50),
[item_desc] nvarchar(150),
[Qty] smallint) PRIMARY KEY (ref, item_code))

返回错误:

Msg 156, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'PRIMARY'.

最佳答案

试试这个方法:

create TABLE tbl_temp 
(
[ ref] numeric(18),
[item_code] varchar(50),
[item_desc] nvarchar(150),
[Qty] smallint,
PRIMARY KEY (ref, item_code)
)

但更好的方法是使用约束,如下所示:

create TABLE tbl_temp 
(
[ ref] numeric(18),
[item_code] varchar(50),
[item_desc] nvarchar(150),
[Qty] smallint,
CONSTRAINT pk_tbl_temp PRIMARY KEY (ref, item_code)
)

create TABLE tbl_temp 
(
[ ref] numeric(18),
[item_code] varchar(50),
[item_desc] nvarchar(150),
[Qty] smallint
)

ALTER TABLE tbl_temp
ADD CONSTRAINT pk_tbl_temp PRIMARY KEY (ref, item_code)

这是更好的方法,因为你为你的 PK 设置了一个友好的名称。

关于SQL Server : error creating table with multiple primary keys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16580721/

28 4 0