如何在SQL Server 2005中的insert语句中增加主键

时间:2012-12-06 21:55:27

标签: sql sql-server

我需要在一个表中写一个insert语句,这些列看起来像这个

  • demandtypeidPK, FK, int, not null
  • characvalueidPK, FK, int, not null
  • percentageint null
  • lastuservarchar(100), null
  • lastediteddatetime, null

这是INSERT声明。请注意

处没有值
value( ,  , 'Bob')

因为我认为自动增量命令应该在哪里

insert into tr_demandtypecharac(demandtypeID, characvalueid, lastuser) 
values(  , , 'Bob')

请帮助一个简单的小声明

我只想知道如何手动插入此表

这是我的表结构:

CREATE TABLE [dbo].[tr_demandtypecharac](
[demandtypeid] [int] NOT NULL,
[characvalueid] [int] NOT NULL,
[percentage] [int] NULL,
[lastuser] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[lastedited] [datetime] NULL,
 CONSTRAINT [PK_tr_dtc_pkey] PRIMARY KEY CLUSTERED 
(
[demandtypeid] ASC,
[characvalueid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
 ) ON [PRIMARY]

ALTER TABLE [dbo].[tr_demandtypecharac]  WITH CHECK 
ADD CONSTRAINT [FK_tr_dtc_cvid] 
FOREIGN KEY([characvalueid]) REFERENCES [dbo].[tr_characvalue] ([characvalueid])

ALTER TABLE [dbo].[tr_demandtypecharac]  WITH CHECK 
ADD CONSTRAINT [FK_tr_dtc_dtid] 
FOREIGN KEY([demandtypeid]) REFERENCES [dbo].[tr_demandtype] ([demandtypeid])

5 个答案:

答案 0 :(得分:5)

如果您想要一个唯一且自动增量的int列,请使用IDENTITY关键字:

CREATE TABLE new_employees
(
 id_num int IDENTITY(1,1),
 fname varchar (20),
 minit char(1),
 lname varchar(30)
)

然后当您插入表格时,不要为该列插入任何内容 - 它会自动增量。

答案 1 :(得分:2)

鉴于您发布的CREATE TABLE语句,没有自动增量(也称为标识)列,您将插入提供所有列和值,如下所示:

insert into tr_demandtypecharac(
       demandtypeid, characvalueid, 
       percentage, lastuser, lastedited) 
values(2, 3, 80, 'Bob', '01/01/2012')

但是,如果您确实通过将CREATE TABLE更改为:

来使其自动增加
CREATE TABLE [dbo].[tr_demandtypecharac](
[demandtypeid] [int] NOT NULL IDENTITY(1,1),
[characvalueid] [int] NOT NULL IDENTITY(1,1),
[percentage] [int] NULL,
[lastuser] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[lastedited] [datetime] NULL,
 CONSTRAINT [PK_tr_dtc_pkey] PRIMARY KEY CLUSTERED 
(
[demandtypeid] ASC,
[characvalueid] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
 )

然后你会插入提供所有非身份(非自动增量)列,如下所示:

insert into tr_demandtypecharac(
      percentage, lastuser, 
      lastedited) 
values(80, 'Bob', '01/01/2012')

但是,将多个列作为标识(自动增量)列并不常见,通常,此列是唯一的PRIMARY KEY列。

答案 2 :(得分:0)

如果列是auto_increment列(与主键列不同),则省略insert语句中的列,它将被填充。

INSERT INTO tr_demandtypecharac (lastuser) VALUES ('Bob')

答案 3 :(得分:0)

我有一个类似的问题,需要用一组记录更新购买的数据库。我的解决方案是找到到目前为止使用的最高密钥,然后将其用作插入的基础。它的核心是ROWNUMBER()OVER(按PART_CODE订购)。

键是inadjinf表中的“ recnum”字段。我确定当前的最高密钥是675400,并将查询更新为:

insert into inadjinf (recnum, user_id, adj_type, adj_status, trans_date, part_code, lotqty, uom, cost_ctr, lot, location, to_cost_ctr, to_location, rec_status, to_part_grade, to_rec_status, remarks1, uom_conv) 
select ROW_NUMBER() OVER(ORDER BY INVDET.PART_CODE) + 675400 as recnum, 'CHRSTR' as user_id, 'M' as adj_type, 'O' as adj_status, '2020-10-23' as trans_date, invdet.part_code, sum(lotqty) as lotqty, uom, 
cost_ctr, lot, location, 'NS' as to_cost_ctr, '500A' as to_location, rec_status, 'Q' as to_part_grade, 'H' as to_rec_status, 'NS Scrap Request from MSobers 10/21/2020' as remarks1, '1' as uom_conv
from invdet
inner join partmstr on invdet.part_code = partmstr.part_code
where 
invdet.part_code In
(
'86038',
'1271',
'VM-0021',
'CO-0107',
...
'FO-0391',
'FO-0376'
)
and lot not in (select lot from inadjinf where trans_date = '2020-10-23' and user_id = 'CHRSTR')
group by invdet.part_code, uom, cost_ctr, lot, location, rec_status

我的输出从675401开始,并从那里上升。最后,我更新了系统内部的“下一个ID字段”表记录。

答案 4 :(得分:-5)

你不应该使用int作为主键......下面是一篇关于它的文章:http://techtrainedmonkey.com/2012/07/30/why-integers-are-lousy-primary-keys/

但是如果你这样做...将字段设置为标识,Sql Server将为你完成...检查出来:http://msdn.microsoft.com/en-us/library/ms186775.aspx