Questions tagged [unique-key]

A key is a set of attributes that is irreducibly unique and non-nullable within a table.

A key is a set of attributes that is irreducibly unique and non-nullable within a table. Irreducible means that all the attributes of the key are necessary to guarantee uniqueness - remove any one attribute and the uniqueness property would be lost. A key may consist of zero, one or more attributes and a relational table (relation variable) must have at least one key and may have more than one.

Keys are unique by definition so the commonly used term "unique key" is actually a tautology.

515 questions
312
votes
12 answers

Unique Key constraints for multiple columns in Entity Framework

I'm using Entity Framework 5.0 Code First; public class Entity { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public string EntityId { get; set;} public int FirstColumn { get; set;} public int SecondColumn { get; set;} …
299
votes
15 answers

Difference between primary key and unique key

I'm using a MySQL database. In which situations should I create a unique key or a primary key?
Anuj
  • 3,049
  • 3
  • 15
  • 11
293
votes
9 answers

Difference between Key, Primary Key, Unique Key and Index in MySQL

When should I use KEY, PRIMARY KEY, UNIQUE KEY and INDEX?
HELP
  • 14,237
  • 22
  • 66
  • 100
248
votes
3 answers

INSERT ... ON DUPLICATE KEY (do nothing)

I have a table with a unique key for two columns: CREATE TABLE `xpo`.`user_permanent_gift` ( `id` INT UNSIGNED NOT NULL AUTO_INCREMENT , `fb_user_id` INT UNSIGNED NOT NULL , `gift_id` INT UNSIGNED NOT NULL , `purchase_timestamp` TIMESTAMP NULL…
ufk
  • 30,912
  • 70
  • 235
  • 386
187
votes
40 answers

Laravel migration: unique key is too long, even if specified

I am trying to migrate a users table in Laravel. When I run my migration I get this error: [Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes…
harryg
  • 23,311
  • 45
  • 125
  • 198
110
votes
8 answers

How to remove unique key from mysql table

I need to remove a unique key from my mysql table. How can remove that using mysql query. I tried this but it is not working alter table tbl_quiz_attempt_master drop unique key; Please help me Thanks
DEVOPS
  • 18,190
  • 34
  • 95
  • 118
87
votes
8 answers

How add unique key to existing table (with non uniques rows)

I want to add complex unique key to existing table. Key contains from 4 fields (user_id, game_id, date, time). But table have non unique rows. I understand that I can remove all duplicate dates and after that add complex key. Maybe exist another…
yAnTar
  • 4,269
  • 9
  • 47
  • 73
74
votes
7 answers

How can I catch UniqueKey Violation exceptions with EF6 and SQL Server?

One of my tables have a unique key and when I try to insert a duplicate record it throws an exception as expected. But I need to distinguish unique key exceptions from others, so that I can customize the error message for unique key constraint…
Sinan ILYAS
  • 1,212
  • 1
  • 12
  • 10
66
votes
3 answers

MySQL - Meaning of "PRIMARY KEY", "UNIQUE KEY" and "KEY" when used together while creating a table

Can anyone explain about the purpose of PRIMARY KEY, UNIQUE KEY and KEY, if it is put together in a single CREATE TABLE statement in MySQL? CREATE TABLE IF NOT EXISTS `tmp` ( `id` int(11) NOT NULL AUTO_INCREMENT, `uid` varchar(255) NOT NULL, …
sura2k
  • 7,365
  • 13
  • 61
  • 80
63
votes
5 answers

Unique key with EF code first

I have a following model in my project public class Category { public Guid ID { get; set; } [Required(ErrorMessage = "Title cannot be empty")] public string Title { get; set; } } and I'm trying to make Title as unique key, I googled…
Prashant Cholachagudda
  • 13,012
  • 23
  • 97
  • 162
53
votes
3 answers

MySQL - Make a pair of values unique

I have a table with two int values that are IDs. On their own these IDs can show up any number of times in the table, but together they should only ever appear once. Is there a way to make a pair of values unique and still allow the individual…
Josh Brittain
  • 2,162
  • 7
  • 31
  • 54
52
votes
3 answers

Is the Sql Server Unique Key also an Index?

I've got a column in a table (eg. UserName) which I want to make sure is unique. So I create a unique key for that column and call it IX_Users_UserName. Now, if I do lots of searching for users based on their username I want to make sure there is an…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
50
votes
3 answers

MySQL: is primary key unique by default?

If I define a column as a primary key in MySQL, is it also unique key by default or do I need to also define it as unique key (in case I want it to be unique)? I saw this question What is the difference b/w Primary Key and Unique Key that explain…
Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
48
votes
6 answers

#1062 - Duplicate entry '' for key 'unique_id' When Trying to add UNIQUE KEY (MySQL)

I've got an error on MySQL while trying to add a UNIQUE KEY. Here's what I'm trying to do. I've got a column called 'unique_id' which is VARCHAR(100). There are no indexes defined on the table. I'm getting this error: #1062 - Duplicate entry '' for…
MillerMedia
  • 3,651
  • 17
  • 71
  • 150
44
votes
4 answers

Oracle unique constraint and unique index

Could someone clarify what is the purpose of having unique index without unique constraint (Oracle)? For example, create table test22(id int, id1 int, tmp varchar(20)); create unique index idx_test22 on test22(id); insert into test22(id, id1, tmp)…
a1ex07
  • 36,826
  • 12
  • 90
  • 103
1
2 3
34 35