constraints

SQL Server - Enforcing uniqueness in one column depending on another column

Apologies if I get the terminology wrong. How do I define a constraint in MSSQL Server 2005 to enforce uniqueness in one column depending on another column? E.g. considering the last two columns: 1 A 1 2 A 2 3 A 2 <- Disallow because '2' has already appeared alongside 'A' 4 B 1 5 B 2 ...

Can DB constraits ignore existing records and apply for only new data?

I want to learn the answer for different DB engines but in our case; we have some records that are not unique for a column and now we want to make that column unique which forces us to remove duplicate values. We use Oracle 10g. Is this reasonable? Or is this something like goto statement :) ? Should we really delete? What if we had mil...

Swapping the 2 rows in MS SQL server retaining the original primary key and without manual update

I have the following problem : I have rows like ID CODE NAME ......... 1 h1100h1 Cool example1 ......... 2 h654441 Another cool1 ......... I would like to swap them retaining all old primary keys and constraints. Of course, I can easily solve this manually by updating the rows. I am kind of wondering whet...

T-SQL: How do I create a unique key that is case sensitive?

How do I create a unique constraint on a varchar field that is case sensitive (SQL Server 2005)? Currently my constraint looks like this: alter table MyTable add constraint UK_MyTable_MyUniqueKey unique nonclustered (MyCol) When I try to insert the following two values, I get a "Violation of UNIQUE KEY constraint..." error. insert i...

What is the difference between c++0x concepts and c# constraints?

C++0x introduces concepts, that let you define, basically, a type of a type. It specifies the properties required of a type. C# let you specify constraints of a generic with the "where" clause. Is there any semantic difference between them? Thank you. ...

SQL Server 2005: Nullable Foreign Key Constraint

I have a foreign key constraint between tables Sessions and Users. Specifically, Sessions.UID = Users.ID. Sometimes, I want Sessions.UID to be null. Can this be allowed? Any time I try to do this, I get an FK Constraint Violation. Specifically, I'm inserting a row into Sessions via LINQ. I set the Session.User = null; and I get this err...

Getting table metadata in MySQL

I'm trying to find out how to get the following constraint information from a table in MySQL 5.0: primary key foreign keys and table references unique columns What is the syntax of the query or queries to do so? I have a feeling I'm close with this, but there is no example. ...

C#: Enum.IsDefined on combined flags

I have this enum: [Flags] public enum ExportFormat { None = 0, Csv = 1, Tsv = 2, Excel = 4, All = Excel | Csv | Tsv } I am trying to make a wrapper on this (or any, really) enum which notifies on change. Currently it looks like this: public class NotifyingEnum<T> : INotifyPropertyChanged where T : struct { ...

Complex queries in SQL Server 2005 database

I have a table in my database in which records conceptually can be children of other rcords. The table has a non-null name field. I need to ensure that each name in a set of children is unique, but not across the entire database. I'd like to enforce this using a constraint inside the Database. What's the best way to accomplish this? I kn...

Would "Constrained Types" be useful in VB/C#?

Intro This question prompted by Marc Gravell's suggestion that I post new language feature suggestions to this site to gather general opinion on them. The idea is to gather if they might be helpful or maybe there is already another way to achieve what I am after. Suggestion (Constrained Types) A normal variable declaration in VB.Ne...

How do I know if CONSTRAINT_NAME is a Primary or Foreign Key?

Using this SQL on SQL Server 2005 SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_NAME = @TableName AND COLUMN_NAME=@ColumnName I get the Primary Keys AND the Foreign Keys. How can I get only Foreign Keys? How can I see if a Constraint is a Primary or a Foreign Key? Thanks ...

How do I solve a set of constraints in Perl?

I have the following set of constraints in Perl (just a sample set of constraints, not the ones I really need): $a < $b $b > $c $a is odd => $a in [10..18] $a > 0 $c < 30 And I need to find a list ($a, $b, $c) that meet the constraints. My naive solution is sub check_constraint { my ($a, $b, $c) = @_; if !($a < $b) {return 0...

How can I enforce a constraint only if a column is not null in Postgresql?

I would like a solution to enforce a constraint only if a column is not null. I can't seem to find a way to do this in the documentation. create table mytable( table_identifier_a INTEGER, table_identifier_b INTEGER, table_value1,...) Do to the nature of the data, I will have identifier b and a value when the table is created. Af...

Scheduling software that solves time constraints

Is there good software or a good toolkit for coordinating schedules and organizing meetings? The concrete problem I have to solve: I have a people from group A, and b people from group B. Now schedule individual meetings between people in A and people in B. This is a real-life problem. There is an open day in a company, which has s...

Understanding unit test constraints and NUnit syntax helpers

Me and a colleague are starting a new project and attempting to take full advantage of TDD. We're still figuring out all the concepts around unit testing and are thus far basing them primarily on other examples. My colleague recently brought into question the point of the NUnit syntax helpers and I'm struggling to explain their benefit...

"Punish" developers or fix it up automatically? Trigger vs constraint

Let's say I have a database column that should always be in uppercase. Here are a couple of ideas: 1) Create a column constraint of: col = UPPER(col) 2) Create an before insert/update row trigger that sets: col = UPPER(col) Usually the more constraints on your database data the better, and triggers can be mysterious and bad. Assume ...

What does "where T : somevalue" mean?

What does where T : somevalue mean? I just saw some code that said where T : Attribute. I think this has something to do with generics but I am not sure what this means or what it is doing. Does anyone know? ...

How do I create a table constraint to prevent duplicate values across two columns?

I have the following table: CREATE TABLE [dbo].[EntityAttributeRelship]( [IdNmb] [int] IDENTITY(1,1) NOT NULL, [EntityIdNmb] [int] NOT NULL, [AttributeIdNmb] [int] NOT NULL, [IsActive] [bit] NOT NULL CONSTRAINT [DF_EntityAttributeRelship_IsActive] DEFAULT ((0)), CONSTRAINT [PK_EntityAttributeRelship] PRIMARY KEY CLUSTER...

Dealing with constraint violations from the DB in NHibernate

I have what I think is a rather common use case for NHibernate. I create an entity and try to save it using ISession.Save() followed by a Transaction.Commit(). At this point, I expect violations of things like unique/primary key constraints to come up to me as exceptions. What I'm seeing, however, is just a GenericADOException. This does...

How do I specify unique constraint for multiple columns in MySQL?

I have a table: table votes ( id, user, email, address, primary key(id), ); Now I want to make the columns user, email, address unique (together). How do I do this in MySql? Thanks in advance. Of course the example is just... an example. So please don't worry about the semantics. ...