title: How do I enforce this constraint?
body: <p>I have this table:</p>
  
  <pre><code>CREATE TABLE Category
  (
    LogId int NULL,
    Name varchar(30) NOT NULL
  )
  </code></pre>
  
  <p>Two categories with different <code>LogId</code>s may have the same <code>Name</code>, but a category may not have the same name as a category with a null <code>LogId</code>. </p>
  
  <p>Is there a way to enforce this constraint?</p>
  
  <p>I tried creating a unique index on this view:</p>
  
  <pre><code>create view Category_LogId_Name
    with schemabinding
  as
  select
    LogId,
    Name
  from
    dbo.Category
  where
    LogId is null
  union all 
  select
    b.LogId,
    a.Name
  from
    dbo.Category a
    cross join dbo.Log b
  where
    a.LogId is null
  </code></pre>
  
  <p>But trying to create the index:</p>
  
  <pre><code>create unique clustered index un_Category_LogId_Name on Category_LogId_Name (LogId, Name)
  </code></pre>
  
  <p>produces this error:</p>
  
  <blockquote>
    <p>Cannot create index on view 'Category_LogId_Name' because it contains one or more UNION, INTERSECT, or EXCEPT operators. Consider creating a separate indexed view for each query that is an input to the UNION, INTERSECT, or EXCEPT operators of the original view.</p>
  </blockquote>
  
  <p>Is there a different approach?</p>