Combined primary key / external key headaches

I work with SQL Server 2008 R2, C # and ASP.Net. I have a table in which there is a composite primary key consisting of the ticket number and the number of times this ticket appears in the table. Ticket frequency calculated by C # code:

VTTTickets.InsertParameters[0].DefaultValue = VTTTTicketNoBox.Text;
string CommString = "SELECT COUNT(*) FROM [Tickets] WHERE [Ticket_No] = " + 
VTTTTicketNoBox.Text;
string ConnString = ConfigurationManager.ConnectionStrings[1].ConnectionString;
OdbcConnection Conn = new OdbcConnection(ConnString);
Conn.Open();
OdbcCommand FooCommand = newOdbcCommand(CommString,Conn);
int FooVal = Convert.ToInt32(FooCommand.ExecuteScalar()) + 1;
VTTTickets.InsertParameters[1].DefaultValue = Convert.ToString(FooVal);
VTTTTicketNoBox.Text = "";
Conn.Close();

My table limitations / etc

CONSTRAINT [PK_Tickets] PRIMARY KEY CLUSTERED 
([Ticket_No] ASC, [Ticket_Sub_No] ASC)
WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = ON,
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
CONSTRAINT [Unique_Ticket_No] UNIQUE NONCLUSTERED 
([Ticket_No] ASC)
WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = ON,     
ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
) ON [PRIMARY]
GO

The reason for the unique restriction is that this part of the primary composite key is also a foreign key for the other two tables. The error I get is when she yells at me because there are duplicate values ​​in the unique constrained column. This can happen as soon as everything goes to production from development, as the ticket can be re-presented.

? , ?

+3
1

, . Technology_A Operation_B, ! , , ( Stackoverflow). , , ( , !)

.

. ( ) , .

, . -, . , .

enter image description here

: Ticket43 , . ( ), . , , ​​ Ticket_Instances.

. , , , , Ticket_Instances.

, , , SQL :

SELECT
  count(*) as TicketCount,
  TicketID
FROM
  Ticket_Instances as TI
GROUP BY
  TicketID

, :

  • ASP.NET( SQL, TTL 10 )
  • Ticket_Counts,

, 2 ( 1).

:

, Ticket_Instances , Insert. Ticket_Instances , SQL :

  • TicketID Ticket_Counts, TicketID Ticket_Counts TicketCount 0
  • TicketCount 1 TicketID Ticket_Counts

Ticket_Counts TicketID, .

, , .

+4

All Articles