如何解决约束阻止我复制表架构?

时间:2019-05-30 15:29:25

标签: sql-server

我有一个表,该表由各行组成,以区分大小写。我现在需要与托盘条形码完全相同的架构。但是,仅尝试更改SHOW CREATE表中的caselabels,更改名称和一些列名是无效的。

这是CREATE脚本-

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[caselabels](
    [idx] [int] IDENTITY(42,1) NOT NULL,
    [imageFile] [nvarchar](50) NULL,
    [jobId] [nvarchar](50) NULL CONSTRAINT [DF__caselabel__workO__47DBAE45]  DEFAULT (NULL),
    [triggerStatus] [nvarchar](5) NULL CONSTRAINT [DF__caselabel__trigg__48CFD27E]  DEFAULT (NULL),
    [workOrder] [nvarchar](30) NULL,
    [t_stamp] [datetime2](0) NULL CONSTRAINT [DF__caselabel__t_sta__49C3F6B7]  DEFAULT (getdate()),
    [lotCode] [nvarchar](45) NULL CONSTRAINT [DF__caselabel__lotCo__4AB81AF0]  DEFAULT (NULL),
    [expDate] [nvarchar](20) NULL CONSTRAINT [DF__caselabel__expDa__4BAC3F29]  DEFAULT (NULL),
    [qty] [int] NULL CONSTRAINT [DF__caselabels__qty__4CA06362]  DEFAULT (NULL),
    [gtinUL] [nvarchar](45) NULL CONSTRAINT [DF__caselabel__gtinU__4D94879B]  DEFAULT (NULL),
    [gtinUC] [nvarchar](45) NULL CONSTRAINT [DF__caselabel__gtinU__4E88ABD4]  DEFAULT (NULL),
    [prodNumber] [nvarchar](45) NULL CONSTRAINT [DF__caselabel__prodN__4F7CD00D]  DEFAULT (NULL),
    [prodDesc] [nvarchar](45) NULL CONSTRAINT [DF__caselabel__prodD__5070F446]  DEFAULT (NULL),
    [prodBrand] [nvarchar](45) NULL CONSTRAINT [DF__caselabel__prodB__5165187F]  DEFAULT (NULL),
    [prodSKU] [nvarchar](45) NULL CONSTRAINT [DF__caselabel__prodS__52593CB8]  DEFAULT (NULL),
    [prodSize] [nvarchar](45) NULL CONSTRAINT [DF__caselabel__prodS__5441852A]  DEFAULT (NULL),
    [dataMatrix] [nvarchar](50) NULL,
    [startingADCaseCount] [int] NULL,
    [address] [nvarchar](50) NULL,
 CONSTRAINT [PK_palletlabels_idx] PRIMARY KEY CLUSTERED 
(
    [idx] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

我只是想将表的名称更改为palletellabels,并将列startingADCaseCount更改为startingADPalletCount。请注意,我已经将CONSTRAINT[PL_caselabels_idx] PRIMARY KEY CLUSTERED更改为CONSTRAINT [PK_palletlabels_idx] PRIMARY KEY CLUSTERED

但是,我仍然遇到这些错误-

Msg 2714, Level 16, State 5, Line 11
There is already an object named 'DF__caselabel__workO__47DBAE45' in the database.
Msg 1750, Level 16, State 0, Line 11
Could not create constraint or index. See previous errors.
Msg 15233, Level 16, State 1, Procedure sp_addextendedproperty, Line 74
Property cannot be added. Property 'MS_SSMA_SOURCE' already exists for 'dbo.caselabels'.

我看到了[DF_caselabel_workO-47DBAE45]下的第一个约束caselabels,我看到了

USE [AntiDiversion]
GO

ALTER TABLE [dbo].[caselabels] ADD  CONSTRAINT [DF__caselabel__workO__47DBAE45]  DEFAULT (NULL) FOR [jobId]
GO

似乎已经与表创建中的DEFAULT (NULL)相同。每行约束都是这样的。摆脱CONSTRAINT [DF_caselabel_....]部分可以吗?

鉴于第二个错误是基于第一个错误,我认为解决此约束问题也将解决第二个错误消息,但是第三个错误是什么?我对此一无所知。

使用SQL SERVER 2014。

1 个答案:

答案 0 :(得分:2)

根据您的表定义,您可以删除显式名称,而只需使用FieldName NullFieldName2 Not Null

我通常不命名我的Null / Not Null约束。