unique-constraint

Allow null in unique column

孤街浪徒 提交于 2019-11-30 12:04:24
问题 I've created the following table: CREATE TABLE MMCompany ( CompanyUniqueID BIGSERIAL PRIMARY KEY NOT NULL, Name VARCHAR (150) NOT NULL, PhoneNumber VARCHAR(20) NOT NULL UNIQUE, Email VARCHAR(75) UNIQUE, CompanyLogo BYTEA ); The email column is unique and it causes a "bug" in my scenario since there could only be one record with null. I'm trying to achieve records of companies without the same email but at the same time allow a companies to have no email. How can I achieve that? 回答1: This is a

HABTM - uniqueness constraint

荒凉一梦 提交于 2019-11-30 11:14:28
I have two models with a HABTM relationship - User and Role. user - has_and_belongs_to_many :roles role - belongs_to :user I want to add a uniqueness constraint in the join (users_roles table) that says the user_id and role_id must be unique. In Rails, would look like: validates_uniqueness_of :user, :scope => [:role] Of course, in Rails, we don't usually have a model to represent the join relationship in a HABTM association. So my question is where is the best place to add the constraint? Art Shayderov You can add uniqueness to join table add_index :users_roles, [ :user_id, :role_id ], :unique

Sqlite NULL and unique?

落花浮王杯 提交于 2019-11-30 10:56:00
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? 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 SQL Server) chose that multiple NULL values do not count towards uniqueness in an index. The SQL standard

mysql combined unique keys

早过忘川 提交于 2019-11-30 08:51:48
Is there way in MySQL to have two unique keys and connect them somehow? for example if i have the following table and 'title'and 'store' are a unique keys id | category | title | price | store 1 | outdoors | fishing rod | 59.99 | wal-mart 2 | auto | Penzoil Oil | 9.99 | target and i try to insert the following record. This new record would be ignored because the title is "fishing rod" AND the store is 'wal-mart' and there is an existing record with that title and store | outdoors | fishing rod | 30.99 | wal-mart but if i attempted to insert the following record it would be accepted because

Postgres UNIQUE CONSTRAINT for array

被刻印的时光 ゝ 提交于 2019-11-30 08:34:12
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 CONSTRAINT I don't think you can use a function with a unique constraint but you can with a unique index

SQL can I have a “conditionally unique” constraint on a table?

折月煮酒 提交于 2019-11-30 08:17:25
问题 I've had this come up a couple times in my career, and none of my local peers seems to be able to answer it. Say I have a table that has a "Description" field which is a candidate key, except that sometimes a user will stop halfway through the process. So for maybe 25% of the records this value is null, but for all that are not NULL, it must be unique. Another example might be a table which must maintain multiple "versions" of a record, and a bit value indicates which one is the "active" one.

How to do multiple column UniqueConstraint in hbm?

对着背影说爱祢 提交于 2019-11-30 07:19:49
问题 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; } 回答1: Use the properties tag:

How can I create a unique constraint on my column (SQL Server 2008 R2)?

好久不见. 提交于 2019-11-30 06:20:51
问题 I have SQL Server 2008 R2 and I want to set a unique column. There seems to be two ways to do this: "unique index" and "unique constraint". They are not much different from what I understand, although unique constraint is recommended by most, because you also get an index automatically. How do I create a unique constraint? ALTER TABLE Customer ADD CONSTRAINT U_Name UNIQUE(Name) Is there a way to create a unique constraint through the SQL Server Management Studio? 回答1: To create these

Allow null in unique column

喜夏-厌秋 提交于 2019-11-30 01:24:42
I've created the following table: CREATE TABLE MMCompany ( CompanyUniqueID BIGSERIAL PRIMARY KEY NOT NULL, Name VARCHAR (150) NOT NULL, PhoneNumber VARCHAR(20) NOT NULL UNIQUE, Email VARCHAR(75) UNIQUE, CompanyLogo BYTEA ); The email column is unique and it causes a "bug" in my scenario since there could only be one record with null. I'm trying to achieve records of companies without the same email but at the same time allow a companies to have no email. How can I achieve that? This is a misunderstanding. The UNIQUE constraint does exactly what you want. Multiple NULL values can coexist in a

Unique Constraint vs Unique Index

依然范特西╮ 提交于 2019-11-30 01:13:56
I’m interested in learning which technique developers prefer to use to enforce uniqueness in SQL Server: UNIQUE CONSTRAINT or UNIQUE INDEX. Given that there is little difference in the physical implementation of each, how do you decide which is best? Are there reasons other than performance to evaluate the best solution? Are there database management advantages to one or the other? This MSDN article comparing the two is for SQL Server 2000: http://msdn.microsoft.com/en-us/library/aa224827(SQL.80).aspx For most purposes, there's no difference - the constraint is implemented as an index under