MSSQL create table CHECK for sub-query

130 Views Asked by At

I guess no sub-queries are allowed in "CREATE TABLE CHECK" (only current values allowed). But I would like to check the table for condition outer the current row like below:

Pseudocode:

CREATE TABLE Persons
(
LoginName varchar(128) NOT NULL CHECK (OLNY ONE of following can be duplicate to an existing record: LoginName, LastName or FirstName. Other two may not),
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255)
)

I would like to check this condition: CHECK (OLNY ONE of following can be duplicate to an existing record: LoginName, LastName or FirstName. Other two may not)

How to do that? (Please write the code, not only a hint. Thanx)

SAMPLE DATA

Existing:

  • Jannie, Smith, Jane, Elm Str.
  • Johnie, Smith, John, Elm Str.
  • Alvaro, Jimenez, Alvaro, 5th Av.

Allowed insert/update:

  • Greenhorn, Prochazka, Martin, MyStreet#1
  • Johnie, Robot, No5, Army Str.
  • GodLike, Smith, No5, Army Str.

Not allowed:

  • Johnie, Smith, John, Route 66
  • Alvaro, Roman, Alvaro, Down Str.
  • Alvaro, Roman, John, Down Str.

To easiest understanding what is not allowed: If one of "name" values is duplicate, rest must be unique

0

There are 0 best solutions below