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
38
votes
2 answers

MySQL: ALTER IGNORE TABLE ADD UNIQUE, what will be truncated?

I have a table with 4 columns: ID, type, owner, description. ID is AUTO_INCREMENT PRIMARY KEY and now I want to: ALTER IGNORE TABLE `my_table` ADD UNIQUE (`type`, `owner`); Of course I have few records with type = 'Apple' and owner = 'Apple…
kuba
  • 3,670
  • 3
  • 31
  • 41
35
votes
3 answers

make text column as unique key

i want to make a table in MySQL server with mediumtext column as UNIQUE KEY CREATE TABLE `parts` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` mediumtext NOT NULL, `display_status` int(11) NOT NULL, UNIQUE KEY `name`…
d-doctor
  • 403
  • 1
  • 4
  • 9
30
votes
2 answers

Is it bad to use user name as primary key in database design?

I was told by a friend: What unique key do you use? I hope you are not saving the entire user name --- this will use up too much table space! Assign an unique userID to each (unique) userNAME and save this userID (should be INTEGER…
Steven
  • 24,410
  • 42
  • 108
  • 130
27
votes
11 answers

What is the difference b/w Primary Key and Unique Key

I tried to find it out in google but not satisfactory answer is given out there. Can anybody explain the solid difference. actually if Primary key is used to select data uniquely then what is the need of Unique key? When should I use a Primary…
nectar
  • 9,525
  • 36
  • 78
  • 100
23
votes
9 answers

Find or insert based on unique key with Hibernate

I'm trying to write a method that will return a Hibernate object based on a unique but non-primary key. If the entity already exists in the database I want to return it, but if it doesn't I want to create a new instance and save it before…
Mike Deck
  • 18,045
  • 16
  • 68
  • 92
22
votes
1 answer

Primary key and unique key in django

I had a custom primary key that need to be set up on a particular data in a model. This was not enough, as an attempt to insert a duplicate number succeeded. So now when i replace primary_key=True to unique=True it works properly and rejects…
Nagaraj Tantri
  • 5,172
  • 12
  • 54
  • 78
20
votes
5 answers

Unique keys in Entity Framework 4

An existing DB schema has unique, non-primary, keys, and some foreign keys that rely on them. Is it possible to define unique keys, which are not primary keys, in Entity Framework v4? How?
Asaf R
  • 6,880
  • 9
  • 47
  • 69
15
votes
1 answer

How to get EF6 to honor Unique Constraint (on FK) in Association/Relationship multiplicity?

2019 Update / TLDR; switch to Entity Framework Core (or whatever else) While missing some "Features", EF Core properly honors Alternate Keys (aka Unique Constraints) in addition to Primary Keys and thus does a much better job of honoring Relational…
14
votes
5 answers

MySQL Question - Unique Key Not functioning correctly, or am I misunderstanding?

I'm trying to create a relation where any of four different parts may be included, but any collection of the same parts should be handled as unique. Example: An assignment must have an assigned company, may optionally have an assigned location,…
Drew Arrigoni
13
votes
1 answer

PostgreSQL: is it possible to provide custom name for PRIMARY KEY or UNIQUE?

When I write: CREATE TABLE accounts ( username varchar(64) PRIMARY KEY, I get primary key named: accounts_pkey Is it possible to assign my own custom name, for instance "accounts_primary_key"? Same story about UNIQUE. I couldn't find it in…
Maciej Ziarko
  • 11,494
  • 13
  • 48
  • 69
13
votes
1 answer

How to add an EF6 Association to a Candidate Key / Unique Key which is not the Primary Key?

Using Schema First, I have a database structure, as so ExternalDataItems --- edataitem_id PK -- surrogate auto-increment - NOT for FK relation here datahash UX -- Candidate Key / Unique Index (binary(20)) ExternalMaps --- emap_id …
12
votes
3 answers

Is Unique key Clustered or Non-Clustered Index in SQL Server?

I am new to SQL Server and while learning about clustered index, I got confused! Is unique key clustered or a non-clustered index? Unique key holds only unique values in the column including null, so according to this concept, unique key should be a…
Lijin Durairaj
  • 4,910
  • 15
  • 52
  • 85
12
votes
3 answers

SQL - Unique Key, Primary Key & Foreign Key

What are the differences between Unique Key, Primary Key and Foreign Key with respect to concept of SQL? How they are different from each other?
Jaipal Reddy K
  • 497
  • 1
  • 4
  • 7
12
votes
3 answers

Solr Composite Unique key from existing fields in schema

I have an index named LocationIndex in solr with fields as follows:
N D Thokare
  • 1,703
  • 6
  • 35
  • 57
10
votes
4 answers

How to prevent rows with duplicated indices / keys to be appended to a data.frame?

I have data in which the combination of two variables ("ManufactererId" and "ProductId") constitute unique keys / identifiers. The data looks like this: my.data <- data.frame(ManufactererId = c(1, 1, 2, 2), ProductId = c(1, 2,…
kmccoy
  • 2,761
  • 3
  • 25
  • 29
1
2
3
34 35