How to create a single index based on two columns?

2

I have a table with these columns:

ip
nome
email

I want ip and email to be a single pair. The same value of ip can contain multiple records and a same email can also contain. But the same ip and even email must be unique.

So, this would be possible:

  - ip              nome              email
1 - 187.89.98.4     João              [email protected]  // ip repetido com registro 2
2 - 187.89.98.4     Maria             [email protected] // ip repetido com registro 1; email repetido com registro 3
3 - 197.19.1.47     Vinicius          [email protected] // e-mail repetido com registro 2

But this does not:

  - ip              nome              email
1 - 187.89.98.4     João              [email protected] // mesmo ip com mesmo e-mail com registro 2
2 - 187.89.98.4     Maria             [email protected] // mesmo ip com mesmo e-mail com registro 1

I do not want the ip and email to be repeated.

    
asked by anonymous 17.10.2017 / 21:48

1 answer

3
CREATE UNIQUE INDEX IpEmail ON tabela (ip ASC, email ASC)

Documentation .

I hope you have another column that is primary, this data may vary and are not good candidates for the primary key. If you want to insist:

ALTER TABLE tabela ADD CONSTRAINT IpEmail PRIMARY KEY CLUSTERED (ip, email);

Documentation .

    
17.10.2017 / 21:57