unique-constraint

How to create a unique constraint just on the date part of a datetime?

℡╲_俬逩灬. 提交于 2019-11-29 18:24:33
问题 I'm writing a very simple blog engine for own use (since every blog engine I encountered is too complex). I want to be able to uniquely identify each post by its URL which is something like /2009/03/05/my-blog-post-slug . To accomplish it in the data tier, I want to create a compound unique constraint on (Date, Slug) where Date is only the date part (ignoring the time of day) of the composition date. I have a few ideas myself (like another column, probably calculated, to hold only the date

Sqlite NULL and unique?

不羁的心 提交于 2019-11-29 16:11:35
问题 I noticed that I can have NULL values in columns that have the UNIQUE constraint: UNIQUE(col) Would that generate any issues in certain situations? 回答1: While the following addresses multiple null values, it does not address any "issues" associated with such a design, other than possible database/SQL portability - as such, it should probably not be considered an answer, and is left here merely for reference. This is actually covered in the SQLite FAQ. It is a design choice - SQLite (unlike

Add a unique constraint of a sql table as foreign key reference to an another sql table

我只是一个虾纸丫 提交于 2019-11-29 15:08:50
how to add a unique constraint of a sql table as foreign key reference to an another sql table in sql server 2005 Gennady Vanin Геннадий Ванин In order to add FK constraint (in child table to parent table) you have to add unique constraint to parent table columns of relationship. All the rest is optional or has nothing to do with FK: no obligatory need of any primary key no need of uniqueness in child table colums(s) The parent table (in such FK relation) is frequently called (including by SSMS) as Primary Key table but PK is not must, unique key/constraint in parent table is enough (as PK is

Hibernate and NonUniqueObjectException

拟墨画扇 提交于 2019-11-29 14:17:44
i have an entity that contains two other entities with @ManyToOne relationship. @Entity public class A extends Serializable{ @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Long id; @ManyToOne @Cascade(CascadeType.SAVE_UPDATE) private B b; @ManyToOne @Cascade(CascadeType.SAVE_UPDATE) private C c; } If i try to save an A instance that have "B_ID" and "C_ID" of another A record i get the exception: org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session For example: A table | ID | B_ID | C_ID | | 1 | 1 | null

How to annotate unique constraint with WHERE clause in JPA

风格不统一 提交于 2019-11-29 14:13:46
I need to use these unique constraints in PostgreSQL CREATE UNIQUE INDEX favorites_3col_uni_idx ON favorites (user_id, menu_id, recipe_id) WHERE menu_id IS NOT NULL; CREATE UNIQUE INDEX favorites_2col_uni_idx ON favorites (user_id, recipe_id) WHERE menu_id IS NULL; The first one I annotate in JPA: @Table(uniqueConstraints= { @UniqueConstraint(name="favorites_3col_uni_idx", columnNames = {"user_id", "menu_id", "recipe_id"}) }) But, ¿it is possible to annotate in JPA the second unique index? Thx. Craig Ringer You appear to want to create partial indexes ( CREATE INDEX ... ON ... WHERE ) using

Postgres UNIQUE CONSTRAINT for array

时光毁灭记忆、已成空白 提交于 2019-11-29 11:59:07
问题 How to create a constraint on the uniqueness of all the values ​​in the array like: CREATE TABLE mytable ( interface integer[2], CONSTRAINT link_check UNIQUE (sort(interface)) ) my sort function create or replace function sort(anyarray) returns anyarray as $$ select array(select $1[i] from generate_series(array_lower($1,1), array_upper($1,1)) g(i) order by 1) $$ language sql strict immutable; I need that would be the value {10, 22} and {22, 10} considered the same and check under the UNIQUE

How can I add constraints to an ADO.NET Entity?

半城伤御伤魂 提交于 2019-11-29 10:25:32
I know how to mark a group of fields as primary key in ADO.NET entities but i haven't found a way to declare unique constraints or check constraints. Is this feature missing on the designer or on the framework? Support for unique keys/constraints does not exist in ADO.NET Entities in v4.0, see the answer to " one-to-one association on a foreign key with unique constraint ", where Diego B Vega says: I know for sure we haven't added support for unique keys other than primary keys in 4.0. He does, however, provide a possible workaround/hack (which comes with all the normal caveats): As you are

Quickest way to check for pre-existing record before insert [mysql_errno()]

依然范特西╮ 提交于 2019-11-29 05:22:47
My question will use emails as an example, but this could apply to anything. Normally before registering a new user (including inserting his/her email) I check if his/her email already exists in the DB something like this: $result = mysql_query("SELECT * FROM Users WHERE email = '".mysql_real_escape_string($email)"';"); if(!$result) { die(mysql_error()); } if(mysql_num_rows($result) > 0) { die('<p>Email already in DB. <a....>Recover Email</a></p>'); } else { //insert new user data into Users table } Since I'd have constrained the email field in my Users table to be UNIQUE anyway, wouldn't it

Create Unqiue case-insensitive constraint on two varchar fields

泄露秘密 提交于 2019-11-29 04:09:05
In Oracle 10g, how do I add a unique case-insensitive constraint on two varchar fields? For example, given the following records already in the table: "Stephen", "Swensen" "John", "Smith" The following inserts would be invalid: "stephen", "Swensen" "John", "smith" "stephen", "swensen" But the following inserts would be valid: "Stephen", "Smith" "John", "Swensen" Assuming your table is called person , and the first and last name columns are called first_name and last_name , add this unique constraint: ALTER TABLE person ADD CONSTRAINT person_name_unique UNIQUE(LOWER(first_name),LOWER(last_name)

How to do multiple column UniqueConstraint in hbm?

孤人 提交于 2019-11-29 02:57:14
Working on some legacy hibernate code. How do I do the following with hbm.xml(hibernate mapping file) instead of with annotations? @Table(name="users", uniqueConstraints = { @UniqueConstraint(columnNames={"username", "client"}), @UniqueConstraint(columnNames={"email", "client"}) }) public class User implements Serializable { private static final long serialVersionUID = 1L; @Id private int id; private String username; private String email; private Client client; } Use the properties tag: ... <properties name="uk1" unique="true"> <property name="username" .../> <many-to-one name="client" .../> <