unique-constraint

Is there in SQL a way to enforce unicity of undirected edge?

不问归期 提交于 2021-02-17 03:21:35
问题 create table Location ( id integer primary key(1, 1), latitude decimal(8,6), longitude decimal(9,6), address varchar(100), name varchar(60) unique ); create table Journey ( id integer primary key(1,1), id_from integer foreign key references Location(id), id_to integer foreign key references Location(id), name varchar(100) unique, unique(id_from, id_to) ); With this schema, you could create 2 different journeys for a pair of locations, one for the way in and one for the way back. What I want

JPA (Hibernate) + Spring: Dealing with unique constraint violations

大憨熊 提交于 2021-02-10 20:26:21
问题 I have a entity A with a unique field and that field basically defines the entity, meaning it it is equal then then entity is also exactly the same. A second point is that it is lets say in no way exceptional if that constraint is violated. Meaning it is fully expected that users will try to input duplicates. In case of duplicate, the application should silently chose the already existing entity. My question is now what I should do especially when saving entities containing a list of As. just

Changing a primary key to a composite primary key

橙三吉。 提交于 2021-02-08 09:41:34
问题 I've been using a "normal" (non-composite) primary key for one of my tables. Now I want to change it to a composite primary key. My tables look something like this: -- Table 1 CREATE TABLE foo ( id SERIAL PRIMARY KEY, id2 INT, ... ) -- Table 2 CREATE TABLE bar ( id SERIAL PRIMARY KEY, id_foo INT REFERENCES foo (id) ) The problem here is that psql does not want to drop the old primary key, since other tables reference it. Is there any way of getting around this without dropping the whole

Postgres unique combination constraint across tables

陌路散爱 提交于 2021-02-05 07:45:26
问题 I have three tables - file ( file_id int primary key filename text not null etc... ) product ( product_id int primary key etc.... ) product_attachment ( product_id references product file_id references file ) I want to ensure that when these are natural-joined, product_id + filename is unique. The best solution I have so far involves adding filename to the product_attachment table, but I'm wondering if there's a way to avoid that. 回答1: If the filename column is not unique you can add a custom

Indexing Postgresql JSONB arrays for element existence and unicity

与世无争的帅哥 提交于 2021-01-29 18:37:12
问题 I have a Postgresql 11.8 table named posts where I would like to define a column slugs of type JSONB, which would contain arrays of strings such as ["my-first-post", "another-slug-for-my-first-post"] . I can find a post having a specific slug using the ? existence operator: SELECT * FROM posts WHERE slugs ? 'some-slug' . Each post is expected to only have a handful of slugs but the amount of posts is expected to grow. Considering the above query where some-slug could be any string: How can I

Applying unique constraint of date on TIMESTAMP column in postgresql

前提是你 提交于 2021-01-29 08:31:02
问题 I have a postgresql table as CREATE TABLE IF NOT EXISTS table_name ( expiry_date DATE NOT NULL, created_at TIMESTAMP with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP(0), CONSTRAINT user_review_uniq_key UNIQUE (expiry_date, created_at::date) -- my wrong attempt of using :: ) I want to put uniue constraint on this table in such a way that expiry_date and date of created_at should be unique. Problem is created_at column is timestamp not date. so is there any way to put unique constraint such

Unique value constraint across multiple columns

纵然是瞬间 提交于 2021-01-05 11:50:02
问题 Suppose, I have the following table: CREATE TABLE "user" ( id BIGINT PRIMARY KEY NOT NULL, phone1 VARCHAR, phone2 VARCHAR ); And I need to implement the following limitation: all phone numbers (if any) in table must be unique. i.e database should not allow any of the following situations: id | phone1 | phone2 1 | 111 | 111 id | phone1 | phone2 1 | 111 | NULL 2 | 111 | NULL id | phone1 | phone2 1 | 111 | NULL 2 | NULL | 111 I know how to implement constraints for first two examples, but I'm

Unique value constraint across multiple columns

落爺英雄遲暮 提交于 2021-01-05 11:49:05
问题 Suppose, I have the following table: CREATE TABLE "user" ( id BIGINT PRIMARY KEY NOT NULL, phone1 VARCHAR, phone2 VARCHAR ); And I need to implement the following limitation: all phone numbers (if any) in table must be unique. i.e database should not allow any of the following situations: id | phone1 | phone2 1 | 111 | 111 id | phone1 | phone2 1 | 111 | NULL 2 | 111 | NULL id | phone1 | phone2 1 | 111 | NULL 2 | NULL | 111 I know how to implement constraints for first two examples, but I'm