I have two tables in SQL Database. The structure is defined as below-
Table 1 Structure
CREATE TABLE [mdcdev].[Table1](
[Id] [int] IDENTITY(1,1) NOT NULL,
[GNum] [varchar](16) NOT NULL,
[GNavn] [varchar](256) NOT NULL,
[GType] [varchar](256) NOT NULL,
[SektorID] [varchar](256) NULL,
[SektorType] [varchar](256) NULL,
[ENum] [varchar](256) NULL,
[Area] [varchar](256) NULL,
[Date] [datetime] NULL
CONSTRAINT [PK_Table1] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
Table 2 Structure
CREATE TABLE [mdcdev].[Table2](
[GNum] [varchar](16) NOT NULL,
[ENum] [varchar](16) NOT NULL,
CONSTRAINT [PK_Table2] PRIMARY KEY CLUSTERED
(
[Grunneiendomsnummer] ASC,
[Eiendomsnummer] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
Where through stored procedure we are saving records in both the tables. Note- GNum column contains duplicate data
I was testing some scenarios where in Table 1, when I change the primary key from Id to GNum, it throws below error "Violation of PRIMARY KEY constraint 'PK_Table1'.
Cannot insert duplicate key in object 'mdcdev.Table1'. The duplicate key value is (12948).\r\nThe statement has been terminated."
Whereas records are getting saved in Table 2 without any such error. In Table 2, both GNum and ENumn column is set as primary key.