unique-constraint

How do I ALTER a PostgreSQL table and make a column unique?

橙三吉。 提交于 2019-11-26 15:20:21
问题 I have a table in PostgreSQL where the schema looks like this: CREATE TABLE "foo_table" ( "id" serial NOT NULL PRIMARY KEY, "permalink" varchar(200) NOT NULL, "text" varchar(512) NOT NULL, "timestamp" timestamp with time zone NOT NULL ) Now I want to make the permalink unique across the table by ALTER-ing the table. Can anybody help me with this? TIA 回答1: I figured it out from the PostgreSQL docs, the exact syntax is: ALTER TABLE the_table ADD CONSTRAINT constraint_name UNIQUE (thecolumn);

How does PostgreSQL enforce the UNIQUE constraint / what type of index does it use?

蹲街弑〆低调 提交于 2019-11-26 14:45:56
I've been trying to sort out the relationship between unique and index in Postgres after reading the docs on index uniqueness being an implementation detail : The preferred way to add a unique constraint to a table is ALTER TABLE ... ADD CONSTRAINT. The use of indexes to enforce unique constraints could be considered an implementation detail that should not be accessed directly. One should, however, be aware that there's no need to manually create indexes on unique columns; doing so would just duplicate the automatically-created index. So taking the docs at their word I'm going to just declare

How to persist @ManyToMany relation - duplicate entry or detached entity

若如初见. 提交于 2019-11-26 13:45:42
问题 I want to persist my entity with ManyToMany relation. But i have some problem during persisting process. My entities : @Entity @Table(name = "USER") public class User implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "ID") @GeneratedValue(strategy = GenerationType.IDENTITY) Long userId; @Column(name = "NAME", unique = true, nullable = false) String userName; @Column(name = "FORNAME") String userForname; @Column(name = "EMAIL") String userEmail;

Unique Key Violation in SQL Server - Is it safe to assume Error 2627?

亡梦爱人 提交于 2019-11-26 12:42:53
问题 I need to catch violation of UNIQUE constraints in a special way by a C# application I am developing. Is it safe to assume that Error 2627 will always correspond to a violation of this kind, so that I can use if (ThisSqlException.Number == 2627) { // Handle unique constraint violation. } else { // Handle the remaing errors. } ? 回答1: 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

IntegrityError duplicate key value violates unique constraint - django/postgres

谁说我不能喝 提交于 2019-11-26 12:20:27
问题 I\'m following up in regards to a question that I asked earlier in which I sought to seek a conversion from a goofy/poorly written mysql query to postgresql. I believe I succeeded with that. Anyways, I\'m using data that was manually moved from a mysql database to a postgres database. I\'m using a query that looks like so: \"\"\" UPDATE krypdos_coderound cru set is_correct = case when t.kv_values1 = t.kv_values2 then True else False end from (select cr.id, array_agg( case when kv1.code_round

Unique Constraint in Entity Framework Code First

喜欢而已 提交于 2019-11-26 11:10:50
Question Is it possible to define a unique constraint on a property using either the fluent syntax or an attribute? If not, what are the workarounds? I have a user class with a primary key, but I would like to make sure the email address is also unique. Is this possible without editing the database directly? Solution (based on Matt's answer) public class MyContext : DbContext { public DbSet<User> Users { get; set; } public override int SaveChanges() { foreach (var item in ChangeTracker.Entries<IModel>()) item.Entity.Modified = DateTime.Now; return base.SaveChanges(); } public class Initializer

SQL unique varchar case sensitivity question

送分小仙女□ 提交于 2019-11-26 10:57:01
问题 I\'m trying to populate a SQL table with a list of words. The table itself it pretty simple: CREATE TABLE WORDS( ID BIGINT AUTO_INCREMENT, WORD VARCHAR(128) NOT NULL UNIQUE, PRIMARY KEY(ID) ); The problem I\'m running into is this: when I do the following inserts back to back INSERT INTO WORDS(WORD) VALUES(\'Seth\'); INSERT INTO WORDS(WORD) VALUES(\'seth\'); The second insert fails with a constraint violation (\"Duplicate entry \'seth\' for key \'WORD\'\"). How can I get the UNIQUE constraint

MySQL - Make an existing Field Unique

拟墨画扇 提交于 2019-11-26 10:16:07
I have an already existing table with a field that should be unique but is not. I only know this because an entry was made into the table that had the same value as another, already existing, entry and this caused problems. How do I make this field only accept unique values? WuHoUnited ALTER IGNORE TABLE mytbl ADD UNIQUE (columnName); For MySQL 5.7.4 or later: ALTER TABLE mytbl ADD UNIQUE (columnName); As of MySQL 5.7.4, the IGNORE clause for ALTER TABLE is removed and its use produces an error. So, make sure to remove duplicate entries first as IGNORE keyword is no longer supported. Reference

How to delete duplicate entries?

时间秒杀一切 提交于 2019-11-26 09:51:05
I have to add a unique constraint to an existing table. This is fine except that the table has millions of rows already, and many of the rows violate the unique constraint I need to add. What is the fastest approach to removing the offending rows? I have an SQL statement which finds the duplicates and deletes them, but it is taking forever to run. Is there another way to solve this problem? Maybe backing up the table, then restoring after the constraint is added? just somebody For example you could: CREATE TABLE tmp ... INSERT INTO tmp SELECT DISTINCT * FROM t; DROP TABLE t; ALTER TABLE tmp

MySQL: ALTER IGNORE TABLE gives “Integrity constraint violation”

对着背影说爱祢 提交于 2019-11-26 07:23:30
问题 I\'m trying to remove duplicates from a MySQL table using ALTER IGNORE TABLE + an UNIQUE KEY. The MySQL documentation says: IGNORE is a MySQL extension to standard SQL. It controls how ALTER TABLE works if there are duplicates on unique keys in the new table or if warnings occur when strict mode is enabled. If IGNORE is not specified, the copy is aborted and rolled back if duplicate-key errors occur. If IGNORE is specified, only the first row is used of rows with duplicates on a unique key.