Can I have compound constraints?

Having a table with this structure ...

Table_files

  • id_file (PK)
  • file name
  • file_path

... Can I have a restriction that allows me not to duplicate the pair "file_name" + "file path" (but allows me to duplicate the "file_name" and "file path" separately), where the only Primary Key is the field "id_file" ?

thanks

+3
source share
2 answers

Yes. Create an index for two fields and make it unique.

+6
source

To go with what Guff said in his answer , create a unique index in two fields:

CREATE UNIQUE NONCLUSTERED INDEX IX_Table_files_name_path ON Table_files 
(
    file_name,file_path
)
GO

file_name+file_path, file_name file_path, .

+2

Source: https://habr.com/ru/post/1720067/


All Articles