constraints

C# generic serialization utility class

I have an existing class for serializing and deserializing objects to/from XML. It's a generic class with a single type parameter T whose only constraint is where T : IXmlSerializable. However, I want to still be able to use this class on classes that do not implement IXmlSerializable but have the [Serializable] attribute. How could I...

Special case of updating a column with not null and unique constraints in PostgreSQL

This is a toy example that illustrates a real problem in PostgreSQL. The below examples are using a PostgreSQL 8.4.3 server, but I suspect other versions have the same problem. Given the following table: => create table tmp_foo (foo boolean not null unique, bar boolean not null unique); => insert into tmp_foo (foo, bar) values (true,...

How to constrain Xml content to a specific length using xsd restrictions?

Using XSD, is it possible to constrain the total text within a Node. In the example below, I want Address Node content to be restricted to 255 characters. <Address> <Line1>Text</Line1> <Line2>Text</Line2> <City></City> <Street></Street> <State></State> <Country></Country> </Address> So, if I only had Line1 and...

How can you tell what method a constraint was created with in SQL Server 2008?

There are two ways to create a constraint, Method A: ALTER TABLE dbo.<tablename> ADD CONSTRAINT <namingconventionconstraint> UNIQUE NONCLUSTERED ( <columnname> Method B: CREATE UNIQUE NONCLUSTERED INDEX <namingconventionconstraint> ON dbo.<tablename> ( <columnname> ) ON [PRIMARY] However, it appears that these constraints need to ...

Why most SQL databases allow defining the same index twice?

Why most SQL databases allow defining the same index (or constraint) twice? For example in MySQL I can do: CREATE TABLE testkey(id VARCHAR(10) NOT NULL, PRIMARY KEY(id)); ALTER TABLE testkey ADD KEY (id); ALTER TABLE testkey ADD KEY (id); SHOW CREATE TABLE testkey; CREATE TABLE `testkey` ( `id` varchar(10) NOT NULL, PRIMARY KEY (`i...

What is the safest way to merge 2 table rows in SQL Server?

I have a table called [Sectors], which stores industry sectors. [SectorId] is defined as an INT and is the primary key of this table. These sectors are referenced throughout the database using the primary key, but there are no foreign key constraints for this primary key in the other tables. Now there are 2 sectors in this table that no...

Unique constraint within a group of records where some value is the same

DBMS: MS Sql Server 2005, Standard I'd like to make a table constraint to have only one record have a particular value within a subset of the table (where the rows share a value in a particular column). Is this possible? Example: I have records in myTable which have a non-unique foreign key (fk1), and a bit column called isPrimary to ...

Oracle INSERT only if values are in another table

Hi, I have two tables: players (has a team name column) and teams (also has a team name column) I want to only allow inserts of new players if the team name exists in the team table. Any help would be appreciated. Please keep it simple because I'm still learning. ...

What is the best practice to work in unmanaged programming pattern

I have a query UPDATE dbo.M_Room SET //do something WHERE PK_RoomId= @RoomId AND IsActive=1 AND FK_DepartmentId =@DepartmentId Now suppose PK_RoomId is my Pk of M_Room and is autoincremented field. So according to this I could have used WHERE PK_RoomId= @RoomId rather than WHERE PK_RoomId= @RoomId AND IsActive=1 AND FK_Departme...

C# Form inheritance and generic constraints

I'm working with .NET Compact Framework and just started to refactor some UI code. I'm defining a base user control to encapsulate common functionality, the project compiles OK but when I try to open a child user control in design mode I'm getting an error. I made my class hierarchy taking into account this question. My classes are like...

ASP.NET MVC - Use Reflection to find if a Controller Exists

I'm having a heck of a time figuring out how to properly implement my 404 redirecting. If I use the following <HandleError()> _ Public Class BaseController : Inherits System.Web.Mvc.Controller ''# do stuff End Class Then any unhandled error on the page will load up the "Error" view which works great. http://example.com/user/999 (whe...

Enumerating m-tuples of Integers Subject to Implication Constraints

How do I enumerate all m-tuples of nonnegative integers (a[1],...,a[m]) subject to the following constraints? For each i in {1,...,m}, there is a number n[i] >= 0 such that a[i] <= n[i]. For each ordered pair (i,j) with i,j in {1,...,m}, there are numbers c[i][j], d[i][j] >= 0 such that: if a[i] > c[i][j], then a[j] <= d[i][j]. c[i][...

Setting column constraint on a table (SQL Server)

I have a column that should contain one of values of 2 power n: 2,4,8,16,32 etc. I want to enforce that on table schema level - is there a way to specify such a column constraint? Thanks! ...

How do I 'DROP' and unamed relationship/constraint in MS Access with SQL?

I have a Microsoft Access database and I have two tables. Table1 has a primary key and Table2 has a foreign key that references Table1's primary key. This relationship is set up and viewable in the Relationship viewer in MS Access, the 'Enforce Referential Integrity' check box is checked, and the Join type is an inner join. The relations...

Django unique constraint + form errors

I'm having some issues with double-posting on my site. I figure a simple unique constraint across all the relevant fields will solve the issue on a database level, but then it just produces a nasty error page for the user. Is there a way I can turn this into a pretty form error instead? Like a non_field_error? Or what approach should I t...

Question on AdventureWorks DB...What is the point of rowguid in a lot of the tables?

So I think I already know the answer to this question... Running AdventureWorks2008 (latest version) on mssql server 2008. I am thinking that the rowguid column in a lot of the tables is to ensure that there is a primary key. I am probably dead wrong as the column appears to be a constraint. Correct me if I am wrong and also please cor...

MySQL with Soft-Deletion, Unique Key and Foreign Key Constraints

Say I have two tables, user and comment. They have table definitions that look like this: CREATE TABLE `user` ( `id` INTEGER NOT NULL AUTO_INCREMENT, `username` VARCHAR(255) NOT NULL, `deleted` TINYINT(1) NOT NULL DEFAULT 0, PRIMARY KEY (`id`), UNIQUE KEY (`username`) ) ENGINE=InnoDB; CREATE TABLE `comment` ( `id` ...

Constraints comparator

Hello, Do you know a tool I could use to compare constraints (not only mathematical)? It's easier to explain with examples: A) Simple example C1: x < 0 x C2: x < 0 ...

Should generic constraints be preferred to using interfaces as parameter types?

Consider this trivial function: public static bool IsPositive(IComparable<int> value) { return value.CompareTo(0) > 0; } Now, if I pass an int to this method, it gets boxed. Wouldn't it therefore be better to define the above method as follows? public static bool IsPositive<T>(T value) where T : IComparable<int> { return valu...

UNIQUE Constraint in SQLite

Hi, Can some one please help me with this problem.To save the values in my iphone app, I am using SQLite3. I have many duplicate values in my database. So, to eliminate that I am planning to write some unique constraint on the table. My constraint should be such a way that a contact whose firstname,lastname and email are same, I want to ...