Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

How do I implement a complex table constraint?

$
0
0

I have a table similar to the following:-

CREATE TABLE [dbo].[child_table](
	child_id [int] IDENTITY(1,1) NOT NULL,
	child_status [varchar](25) NULL,
	parent_id [int] NOT NULL,
	CONSTRAINT [PK__child_table] PRIMARY KEY CLUSTERED 
	(
		child_id ASC
	)
)

Each parent_id may have one or more child_id's. But only one child (of each parent) may have a "child_status" of "Active". Is there a way to enforce this in TSQL (e.g. with a CHECK CONSTRAINT or RULE), I can't find a way to do this.

Thanks


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>