unique-index

Conditional Unique index on h2 database

人走茶凉 提交于 2019-12-05 17:10:31
I have a SAMPLE_TABLE with a column BIZ_ID which should be unique when the column active is not equal to 0. On an oracle database the index looks like this: CREATE UNIQUE INDEX ACTIVE_ONLY_IDX ON SAMPLE_TABLE (CASE "ACTIVE" WHEN 0 THEN NULL ELSE "BIZ_ID" END ); How would this unique index look like on a h2 database? In H2, you could use a computed column that has a unique index: create table test( biz_id int, active int, biz_id_active int as (case active when 0 then null else biz_id end) unique ); --works insert into test(biz_id, active) values(1, 0); insert into test(biz_id, active) values(1,

Uniqueness validation in database when validation has a condition on another table

只愿长相守 提交于 2019-12-05 14:52:35
I asked a similar question here at Uniqueness validation in database when validation has a condition but my requirements have changed, hence this question. Using uniqueness validations in Rails is not safe when there are multiple processes unless the constraint is also enforced on the database (in my case a PostgreSQL database so see http://robots.thoughtbot.com/the-perils-of-uniqueness-validations ). In my case, the uniqueness validation is conditional: it should only be enforced if another attribute on another model becomes true. So I have class Parent < ActiveRecord::Base # attribute is

Should I specify both INDEX and UNIQUE INDEX?

时光怂恿深爱的人放手 提交于 2019-12-05 11:02:58
问题 On one of my PostgreSQL tables, I have a set of two fields that will be defined as being unique in the table, but will also both be used together when selecting data. Given this, do I only need to define a UNIQUE INDEX, or should I specify an INDEX in addition to the UNIQUE INDEX? This? CREATE UNIQUE INDEX mytable_col1_col2_idx ON mytable (col1, col2); Or this? CREATE UNIQUE INDEX mytable_col1_col2_uidx ON mytable (col1, col2); CREATE INDEX mytable_col1_col2_idx ON mytable (col1, col2); 回答1:

MySQL Views in Navicat - How to define 'primary key'?

烂漫一生 提交于 2019-12-05 10:49:24
Often when I define a View in Navicat I receive the following message: xxx does not have a primary key. Updates to this table will be done using the following pseudo statement: UPDATE xxx SET ModifiedFieldsAndValues WHERE AllFieldsAndOldValues LIMIT 1 Obviously I only use my Views for viewing data, not updating . But this did make me curious: Is there a way to define a "primary key" or "unique index" on a View? its implied that the view uses the indices and primary keys of its base table. You can change the semantics of how insert and updates occur when using them via views by playing with the

MyISAM unique keys being cut off at 64 bytes, causing collisions

故事扮演 提交于 2019-12-05 08:25:30
I've got a MySQL table that stores urls as unique keys. I'm starting to get collisions on my keys because it seems the keys themselves are only the first 64 bytes (or characters if you prefer, its a latin-1 collated) of any url. So if a url is over 64 characters and I've already got a similar url it throws an error. For example: SELECT l.link_id FROM mydb.links l WHERE url = 'http://etonline.com/tv/108475_Charlie_Sheen_The_People_Have_Elected_Me_as_Their_Leader/index.html' Throws this error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'http://etonline.com/tv/108475

Uniqueness validation in database when validation has a condition

牧云@^-^@ 提交于 2019-12-04 20:17:43
Using uniqueness validations in Rails is not safe when there are multiple processes unless the constraint is also enforced on the database (in my case a PostgreSQL database, so see this blog post ). In my case, the uniqueness validation is conditional: it should only be enforced if another attribute in the model becomes true. So I have class Model < ActiveRecord::Base validates_uniqueness_of :text, if: :is_published? def is_published? self.is_published end end So the model has two attributes: is_published (a boolean) and text (a text attribute). text should be unique across all models of type

PostgreSQL constraint, that gets checked on commit and not earlier [duplicate]

倾然丶 夕夏残阳落幕 提交于 2019-12-04 05:26:15
问题 This question already has answers here : Constraint defined DEFERRABLE INITIALLY IMMEDIATE is still DEFERRED? (2 answers) Closed last year . Is it possible to create to create a unique index or other kind of constraint in PostgreSQL, that would be checked on transaction COMMIT and not a millisecond earlier? I need an index for a pair of (record_id, ordering), so I make sure that inside a given record_id only one and no more than one records has the same ordering. Where's the problem? Well,

Should I specify both INDEX and UNIQUE INDEX?

旧巷老猫 提交于 2019-12-03 23:19:20
On one of my PostgreSQL tables, I have a set of two fields that will be defined as being unique in the table, but will also both be used together when selecting data. Given this, do I only need to define a UNIQUE INDEX, or should I specify an INDEX in addition to the UNIQUE INDEX? This? CREATE UNIQUE INDEX mytable_col1_col2_idx ON mytable (col1, col2); Or this? CREATE UNIQUE INDEX mytable_col1_col2_uidx ON mytable (col1, col2); CREATE INDEX mytable_col1_col2_idx ON mytable (col1, col2); Mark Byers If you have a UNIQUE INDEX then you don't also need the INDEX - it would be redundant. A UNIQUE

Rails uniqueness constraint and matching db unique index for null column

那年仲夏 提交于 2019-12-03 23:03:14
I have the following in my migration file def self.up create_table :payment_agreements do |t| t.boolean :automatic, :default => true, :null => false t.string :payment_trigger_on_order t.references :supplier t.references :seller t.references :product t.timestamps end end I want to ensure that if a product_id is specified it is unique but I also want to allow null so I have the following in my model: validates :product_id, :uniqueness => true, :allow_nil => true Works great but I should then add an index to the migration file add_index :payment_agreements, :product_id, :unique => true Obviously

Unable to create index because of duplicate that doesn't exist?

流过昼夜 提交于 2019-12-03 08:06:11
问题 I'm getting an error running the following Transact-SQL command: CREATE UNIQUE NONCLUSTERED INDEX IX_TopicShortName ON DimMeasureTopic(TopicShortName) The error is: Msg 1505, Level 16, State 1, Line 1 The CREATE UNIQUE INDEX statement terminated because a duplicate key was found for the object name 'dbo.DimMeasureTopic' and the index name 'IX_TopicShortName'. The duplicate key value is (). When I run SELECT * FROM sys.indexes WHERE name = 'IX_TopicShortName' or SELECT * FROM sys.indexes WHERE