unique-constraint
Unique Key Violation in SQL Server – Is it safe to assume Error 2627?
2627 is unique constraint (includes primary key), 2601 is unique index SELECT * FROM sys.messages WHERE text like ‘%duplicate%’ and text like ‘%key%’ and language_id = 1033
Does a Postgres UNIQUE constraint imply an index?
Yes. UNIQUE constraints are implemented using a unique B-tree index in Postgres. See: How does PostgreSQL enforce the UNIQUE constraint / what type of index does it use?
Django Unique Together (with foreign keys)
You can’t. The unique_together clause is directly translated to the SQL unique index. And you can only set those on columns of a single table, not a combination of several tables. You can add validation for it yourself though, simply overwrite the validate_unique method and add this validation to it. Docs: http://docs.djangoproject.com/en/dev/ref/models/instances/#django.db.models.Model.validate_unique
How do I ALTER a PostgreSQL table and make a column unique?
I figured it out from the PostgreSQL docs, the exact syntax is: ALTER TABLE the_table ADD CONSTRAINT constraint_name UNIQUE (thecolumn); Thanks Fred.
MySQL: ALTER IGNORE TABLE gives “Integrity constraint violation”
The IGNORE keyword extension to MySQL seems to have a bug in the InnoDB version on some version of MySQL. You could always, convert to MyISAM, IGNORE-ADD the index and then convert back to InnoDB ALTER TABLE table ENGINE MyISAM; ALTER IGNORE TABLE table ADD UNIQUE INDEX dupidx (field); ALTER TABLE table ENGINE InnoDB; Note, … Read more
IntegrityError duplicate key value violates unique constraint – django/postgres
This happend to me – it turns out you need to resync your primary key fields in Postgres. The key is the SQL statement: SELECT setval(‘tablename_id_seq’, (SELECT MAX(id) FROM tablename)+1);
How does PostgreSQL enforce the UNIQUE constraint / what type of index does it use?
create an index and not assume that the values are unique It is safe to assume that values are unique, if you have a unique index defined. That’s how unique constraints are implemented (at the time being, and probably in all future versions as well). Defining a UNIQUE constraint does effectively the same (almost, see … Read more
SQLite table constraint – unique on multiple columns
Put the UNIQUE declaration within the column definition section; working example: CREATE TABLE a ( i INT, j INT, UNIQUE(i, j) ON CONFLICT REPLACE );
Can PostgreSQL have a uniqueness constraint on array elements?
The righteous path You might want to reconsider normalizing your schema. It is not necessary for everyone to “join for even the simplest query”. Create a VIEW for that. Table could look like this: CREATE TABLE hostname ( hostname_id serial PRIMARY KEY , host_id int REFERENCES host(host_id) ON UPDATE CASCADE ON DELETE CASCADE , hostname … Read more