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
4
votes
1 answer

Unique key column order in fluent nhibernate

I try to create unique index with fluent nhibernate. But when i use the following classes tables are created like : Person Table: Id Name PersonStatistic Table: Id Date Count Person_Id Because of this structure when i create the unique key,…
Murat Aras
  • 403
  • 2
  • 10
4
votes
2 answers

Mysql : Drop a unique key if exists -- but without knowing name of the key or whether it exists

Don't ask why (as the answer is 'our totally weird and bepsoke setup...'), but I need to drop a unique key on a table (if it exists) without knowing the name of the key -- only the columns that make up the key. e.g. I have this table CREATE…
Dave P
  • 178
  • 2
  • 9
4
votes
2 answers

MySQL INSERT only if row doesn't exist, otherwise select the duplicate row

I am looking for a statement that will try to insert a row into a table, but return the primary key of the duplicate row if one is encountered. One field in the table is the auto incrementing primary key, the other is unique.
VWD
  • 135
  • 6
4
votes
4 answers

Fastest way to compute a "Visual" checksum of an image

I'm looking to create an ID system for cataloging images. I can't use md5() since that will change if I alter the EXIF tags of the image. I am currently using the SHA1 checksum computed by imagemagick. It works perfectly, but it's really, really…
ensnare
  • 40,069
  • 64
  • 158
  • 224
4
votes
1 answer

mysql insert on duplicate key update, check which one occurred

In PHP, how to detect which one happened (INSERT or UPDATE) in the following query: INSERT INTO ... ON DUPLICATE KEY UPDATE ...
Aliweb
  • 1,891
  • 3
  • 21
  • 44
4
votes
6 answers

Varchar with trailing spaces as a Primary Key in SQL Server 2008

Is it possible to have a varchar column as a primary key with values like 'a ' and 'a', is gives always this error "Violation of PRIMARY KEY constraint" in MS SQL Server 2008. In Oracle dons't give any error. BTW I'm not implementing this way I'm…
4
votes
1 answer

SQL Unique Key Syntax

Very basic question; I'm very new to SQL and trying to decipher an example data base. In the below create table code, why does the define primary key syntax reference only the 'id' column once in parentheses but the unique key definition references…
DVCITIS
  • 1,067
  • 3
  • 16
  • 36
4
votes
1 answer

MySQL: UNIQUE text field using additional HASH field

In my MySQL DB I have a table defined like: CREATE TABLE `mytablex_cs` ( `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT, `tag` varchar(6) COLLATE utf8_bin NOT NULL DEFAULT '', `value` text COLLATE utf8_bin NOT NULL, PRIMARY KEY (`id`), …
Giovanni Di Milia
  • 13,480
  • 13
  • 55
  • 67
3
votes
2 answers

MySQL UUID() when not unique?

What happens when a UUID() generated by MySQL is not unique? If this is for a column that is a primary key, does MySQL error out, or does it try generating another UUID until a truly unique one is found?
ina
  • 19,167
  • 39
  • 122
  • 201
3
votes
1 answer

MySQL CONSTRAINT on two UNIQUE KEYs, that use the same FOREIGN KEY IDs

I want to create a MySQL table to hold relationship data between users. The relationship between user A to B and user B to A can be different. Example: Relationship of (from) Bob (to) Alice: 0.9 - Bob loves Alice's stuff. Relationship of (from)…
3
votes
2 answers

Making varchar(500) datacolumn unique

I have a requirement to make a column of varchar(500) unique. Putting unique constraint would not work as it crosses the size limit of unique. Then, what would be the best strategy to make column unique?
user841123
3
votes
3 answers

Objects to strings, unique keys in PHP

I was reading around about the Observer pattern, and found a dated article. Having read through, I noticed an interesting mention in this paragraph: The key methods to look at here are attach(), detach(), and notify(). attach() and detach() handle…
Dan Lugg
  • 20,192
  • 19
  • 110
  • 174
3
votes
3 answers

Generate unique random number in ASP.NET Core

I'm working on a Hotspot service, using ASP.NET Core building a MVC application, and i need do generate a 6-digit (100000 to 999999) number to serve as the password for the user to connect and have internet access. That number needs to be unique in…
3
votes
3 answers

How to know if an uncommitted transaction tries to insert a specific unique key into SQL

I'm writing a programm which inserts data to a MariaDB-Server and can be used by different people on the same time. The transactions take some time, so the following problem might occur: Person A starts a transaction with primary key "c" and while…
Lukas
  • 381
  • 3
  • 13
3
votes
1 answer

ON DUPLICATE KEY UPDATE only for primary keys?

Is there a way to restrict "ON DUPLICAYE KEY UPDATE" to only trigger if the duplicate key is the PRIMARY KEY of the table? (and not if the conflict is generated by a UNIQUE KEY) For example in the following table: CREATE TABLE users ( id INT(16)…
Sneppy
  • 379
  • 5
  • 20