unique-constraint

SQL: How to find duplicates based on two fields?

五迷三道 提交于 2019-11-27 01:50:21
问题 I have rows in an Oracle database table which should be unique for a combination of two fields but the unique constrain is not set up on the table so I need to find all rows which violate the constraint myself using SQL. Unfortunately my meager SQL skills aren't up to the task. My table has three columns which are relevant: entity_id, station_id, and obs_year. For each row the combination of station_id and obs_year should be unique, and I want to find out if there are rows which violate this

How can I create a SQL unique constraint based on 2 columns?

拥有回忆 提交于 2019-11-27 00:25:56
问题 I have a Table like this one: |UserId | ContactID | ContactName --------------------------------------- | 12456 | Ax759 | Joe Smith | 12456 | Ax760 | Mary Smith | 12458 | Ax739 | Carl Lewis | 12460 | Ax759 | Chuck Norris | 12460 | Bx759 | Bruce Lee I need to add a constraint to this table so that no user can have duplicate contact id's. The users are importing data from various external systems so ContactId will not be unique across the board but will be unique on a per user basis. I know how

IntegrityError duplicate key value violates unique constraint - django/postgres

人盡茶涼 提交于 2019-11-26 23:47:28
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_id = cr.id then kv1.option_id else null end ) as kv_values1, array_agg( case when kv2.code_round_id = cr_m.id

Is there a way to enforce unique constraint on a property (field) other than the primary key in dynamodb

寵の児 提交于 2019-11-26 20:19:25
问题 In dynamodb, if you want to enforce uniqueness in a field other than the primary key (like were you have a users table and want unique email addresses for users while primary key is a userid which is a number) is there a way other thans scanning the table to see if the email is already in use? 回答1: Short answer: No. DynamoDB is a key:value store. It is very good at quickly retrieving/saving Items because it does a couple of compromise. This is a constraint you have to handle yourself.

MySQL: ALTER IGNORE TABLE gives “Integrity constraint violation”

北城余情 提交于 2019-11-26 19:43:35
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. The other conflicting rows are deleted. Incorrect values are truncated to the closest matching acceptable

How do you validate uniqueness of a pair of ids in Ruby on Rails?

偶尔善良 提交于 2019-11-26 19:22:26
问题 Suppose the following DB migration in Ruby: create_table :question_votes do |t| t.integer :user_id t.integer :question_id t.integer :vote t.timestamps end Suppose further that I wish the rows in the DB contain unique (user_id, question_id) pairs. What is the right dust to put in the model to accomplish that? validates_uniqueness_of :user_id, :question_id seems to simply make rows unique by user id, and unique by question id, instead of unique by the pair. 回答1: validates_uniqueness_of :user_id

Postgresql: Conditionally unique constraint

懵懂的女人 提交于 2019-11-26 19:00:45
问题 I'd like to add a constraint which enforces uniqueness on a column only in a portion of a table. ALTER TABLE stop ADD CONSTRAINT myc UNIQUE (col_a) WHERE (col_b is null); The WHERE part above is wishful thinking. Any way of doing this? Or should I go back to the relational drawing board? 回答1: PostgreSQL doesn't define a partial (i.e. conditional) UNIQUE constraint - however, you can create a partial unique index . PostgreSQL uses unique indexes to implement unique constraints, so the effect

SQL Unique constraint across multiple tables

亡梦爱人 提交于 2019-11-26 18:26:01
问题 I am trying to create a unique constraint across multiple tables. I have found similar questions answered here but they don't quite capture the spirit of what I am trying to do. As an example, I have three tables, t_Analog, t_Discrete, t_Message CREATE TABLE t_Analog( [AppName] [nvarchar](20) NOT NULL, [ItemName] [nvarchar](32) NOT NULL, [Value] [float] NOT NULL, CONSTRAINT [uc_t_Analog] UNIQUE(AppName, ItemName) ) CREATE TABLE t_Discrete( [AppName] [nvarchar](20) NOT NULL, [ItemName]

SQLite table constraint - unique on multiple columns

元气小坏坏 提交于 2019-11-26 15:47:26
I can find syntax "charts" on this on the SQLite website, but no examples and my code is crashing. I have other tables with unique constraints on a single column, but I want to add a constraint to the table on two columns. This is what I have that is causing an SQLiteException with the message "syntax error". CREATE TABLE name (column defs) UNIQUE (col_name1, col_name2) ON CONFLICT REPLACE I'm doing this based on the following: table-constraint To be clear, the documentation on the link I provided says that CONTSTRAINT name should come before my constraint definition. Something that may lead

Can PostgreSQL have a uniqueness constraint on array elements?

你说的曾经没有我的故事 提交于 2019-11-26 15:24:21
I'm trying to come up with a PostgreSQL schema for host data that's currently in an LDAP store. Part of that data is the list of hostnames a machine can have, and that attribute is generally the key that most people use to find the host records. One thing I'd like to get out of moving this data to an RDBMS is the ability to set a uniqueness constraint on the hostname column so that duplicate hostnames can't be assigned. This would be easy if hosts could only have one name, but since they can have more than one it's more complicated. I realize that the fully-normalized way to do this would be