Questions tagged [constraints]

A constraint is a condition that has to be fulfilled in a given context. Constraints are typically used in databases and programming languages to detect errors, ensure data consistency, accuracy, and to enforce business requirements.

A constraint is a condition that has to be fulfilled in a given context. Constraints are typically used in databases and programming languages to detect errors, ensure data consistency, accuracy, and to enforce business requirements. Constraints are also important in optimization problems. Constraints can be either hard constraints which set conditions for the variables that are required to be satisfied, or soft constraints which have some variable values that are penalized in the objective function if, and based on the extent that, the conditions on the variables are not satisfied.

For example, database constraints can be:

  • NOT NULL Constraint - a column cannot have NULL value.

  • DEFAULT Constraint - a default value for a column when none is specified.

  • UNIQUE Constraint - all values in a column are different.

  • CHECK Constraint - all values in a column satisfy certain criteria.

Further reading

What are database constraints?

8809 questions
205
votes
11 answers

How can I use interface as a C# generic type constraint?

Is there a way to get the following function declaration? public bool Foo() where T : interface; ie. where T is an interface type (similar to where T : class, and struct). Currently I've settled for: public bool Foo() where T : IBase; Where…
Matthew Scharley
  • 127,823
  • 52
  • 194
  • 222
203
votes
3 answers

How to add not null constraint to existing column in MySQL

I have table name called "Person" with following column names P_Id(int), LastName(varchar), FirstName (varchar). I forgot to give NOT NULL Constraint to P_Id. Now I tried with following query to add NOT NULL Constraint to existing column called…
mymotherland
  • 7,968
  • 14
  • 65
  • 122
197
votes
4 answers

Oracle find a constraint

I have a constraint called users.SYS_C00381400. How do I find what that constraint is? Is there a way to query all constraints?
David Oneill
  • 12,502
  • 16
  • 58
  • 70
193
votes
17 answers

SQL DROP TABLE foreign key constraint

If I want to delete all the tables in my database like this, will it take care of the foreign key constraint? If not, how do I take care of that first? GO IF OBJECT_ID('dbo.[Course]','U') IS NOT NULL DROP TABLE dbo.[Course] GO IF…
user188229
184
votes
2 answers

Postgresql: Conditionally unique constraint

I'd like to add a constraint which enforces uniqueness on a column only in a portion of a table. ALTER TABLE stop ADD CONSTRAINT myc UNIQUE (col_a) WHERE (col_b is null); The WHERE part above is wishful thinking. Any way of doing this? Or should I…
EoghanM
  • 25,161
  • 23
  • 90
  • 123
181
votes
10 answers

SQL Server 2005 How Create a Unique Constraint?

How do I create a unique constraint on an existing table in SQL Server 2005? I am looking for both the TSQL and how to do it in the Database Diagram.
David Basarab
  • 72,212
  • 42
  • 129
  • 156
180
votes
9 answers

How to drop all user tables?

How can I drop all user tables in oracle? I have problem with constraints. When I disable all it is still no possible.
szaman
  • 6,666
  • 13
  • 53
  • 81
163
votes
5 answers

Inherit from a generic base class, apply a constraint, and implement an interface in C#

This is a syntax question. I have a generic class which is inheriting from a generic base class and is applying a constraint to one of the type parameters. I also want the derived class to implement an interface. For the life of me, I cannot seem to…
Dan Rigby
  • 17,133
  • 6
  • 43
  • 60
148
votes
5 answers

Ruby on Rails: How do I add a not null constraint to an existing column using a migration?

In my Rails (3.2) app, I have a bunch of tables in my database but I forgot to add a few not null constraints. How can I write a migration which adds not null to an existing column?
David Robertson
  • 1,809
  • 3
  • 12
  • 12
137
votes
11 answers

Postgres: Add constraint if it doesn't already exist

Does Postgres have any way to say ALTER TABLE foo ADD CONSTRAINT bar ... which will just ignore the command if the constraint already exists, so that it doesn't raise an error?
Paul A Jungwirth
  • 23,504
  • 14
  • 74
  • 93
137
votes
8 answers

Display names of all constraints for a table in Oracle SQL

I have defined a name for each of the constraint for the multiple tables that I have created in Oracle SQL. The problem is that to drop a constraint for the column of a particular table I need to know the name that I have supplied for each…
Jeris
  • 2,355
  • 4
  • 26
  • 38
136
votes
4 answers

Constraint name update in PostgreSQL

Is it possible to change the constraint name in Postgres? I have a PK added with: ALTER TABLE contractor_contractor ADD CONSTRAINT commerce_contractor_pkey PRIMARY KEY(id); And I want to to have different name for it, to be consistent with the rest…
Sebastian
129
votes
3 answers

Unique constraint that allows empty values in MySQL

I have a field that stores product codes. The codes are unique, but some products simply doesn't have a code. I can't invent codes because those are providers codes. Is this kind of constraint possible in MySQL? I'm a noob with stored procedures and…
The Disintegrator
  • 4,147
  • 9
  • 35
  • 43
123
votes
2 answers

PostgreSQL: default constraint names

When creating a table in PostgreSQL, default constraint names will assigned if not provided: CREATE TABLE example ( a integer, b integer, UNIQUE (a, b) ); But using ALTER TABLE to add a constraint it seems a name is mandatory: ALTER…
Ian Mackinnon
  • 13,381
  • 13
  • 51
  • 67
112
votes
7 answers

Rename a constraint in SQL Server?

Is it possible to rename a constraint in SQL Server? I don't want to have to delete and create a new one because this constraint affects other already existing constraints and I will have to recreate/alter those.
intrigued_66
  • 16,082
  • 51
  • 118
  • 189