D
D
Danil Vaskevich2021-12-09 18:55:45
SQL Server
Danil Vaskevich, 2021-12-09 18:55:45

How to fix the error of creating a unique index on NVARCHAR(MAX)?

I need to create table Faculties SQL query but row

Dean nvarchar(max) NOT NULL check(Dean != '') unique,

returns an error:
Msg 1919, Level 16, State 1, Line 7
Тип столбца "Dean" в таблице "Faculties" не позволяет выбрать его в качестве ключевого столбца индекса.

Here is the code for the rest of the table:
CREATE TABLE Faculties(
  Id int primary key NOT NULL IDENTITY(1,1),
  Dean nvarchar(max) NOT NULL check(Dean != '') unique,
  [Name] nvarchar(100) NOT NULL check([Name] != '') unique
)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2021-12-09
@DaniVffgf

NVARCHAR(MAX)not indexed.

CREATE TABLE Faculties(
  ID INT PRIMARY KEY NOT NULL IDENTITY(1,1),
  Dean NVARCHAR(MAX) NOT NULL CHECK(Dean<>''),
  DeanIndex AS HASHBYTES('SHA',Dean) UNIQUE,
  Name NVARCHAR(100) NOT NULL CHECK(Name<>'') UNIQUE
)

R
Rsa97, 2021-12-09
@Rsa97

The length of nvarchar(max) is up to 2 31 -1 bytes.
The length of a non-clustered key is 1700 bytes.
It is logical that the field does not fit into the key.
Are you sure you have deans with such long names?
And for good, this field should contain the id of the dean in the table of employees.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question