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
920
votes
17 answers

How can foreign key constraints be temporarily disabled using T-SQL?

Are disabling and enabling foreign key constraints supported in SQL Server? Or is my only option to drop and then re-create the constraints?
Ray
  • 187,153
  • 97
  • 222
  • 204
833
votes
15 answers

How to truncate a foreign key constrained table?

Why doesn't a TRUNCATE on mygroup work? Even though I have ON DELETE CASCADE SET I get: ERROR 1701 (42000): Cannot truncate a table referenced in a foreign key constraint (mytest.instance, CONSTRAINT instance_ibfk_1 FOREIGN KEY (GroupID) REFERENCES…
user391986
  • 29,536
  • 39
  • 126
  • 205
456
votes
24 answers

Is there a constraint that restricts my generic method to numeric types?

Can anyone tell me if there is a way with generics to limit a generic type argument T to only: Int16 Int32 Int64 UInt16 UInt32 UInt64 I'm aware of the where keyword, but can't find an interface for only these types, Something like: static bool…
Corin Blaikie
  • 17,718
  • 10
  • 37
  • 39
323
votes
2 answers

Techniques for Tracing Constraints

Here's the scenario: I've written some code with a type signature and GHC complains could not deduce x ~ y for some x and y. You can usually throw GHC a bone and simply add the isomorphism to the function constraints, but this is a bad idea for…
crockeea
  • 21,651
  • 10
  • 48
  • 101
312
votes
12 answers

Unique Key constraints for multiple columns in Entity Framework

I'm using Entity Framework 5.0 Code First; public class Entity { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public string EntityId { get; set;} public int FirstColumn { get; set;} public int SecondColumn { get; set;} …
304
votes
7 answers

Animating a constraint in Swift

I have a UITextField that I want to enlarge its width when tapped on. I set up the constraints and made sure the constraint on the left has the lower priority then the one that I am trying to animate on the right side. Here is the code that I am…
icekomo
  • 9,328
  • 7
  • 31
  • 59
300
votes
12 answers

How to remove constraints from my MySQL table?

I want to remove constraints from my table. My query is: ALTER TABLE `tbl_magazine_issue` DROP CONSTRAINT `FK_tbl_magazine_issue_mst_users` But I got an error: #1064 - You have an error in your SQL syntax; check the manual that corresponds to…
deepu sankar
  • 4,335
  • 3
  • 26
  • 37
295
votes
6 answers

How to trap on UIViewAlertForUnsatisfiableConstraints?

I'm seeing an error appear in my debugger log: Will attempt to recover by breaking constraint Make a symbolic breakpoint at…
Maury Markowitz
  • 9,082
  • 11
  • 46
  • 98
258
votes
12 answers

SQL-script: How to write ALTER statements to set Primary key on an existing table?

I have an existing table called Person which already has existing 1000 rows of data. In this table I have 5 fields: personId Pname PMid Pdescription Pamt The table already has an existing compound-key [PersonId, Pname] as the primary key. Now I…
jay
  • 3,699
  • 7
  • 28
  • 28
246
votes
5 answers

Turn off constraints temporarily (MS SQL)

I'm looking for a way to temporarily turn off all DB's constraints (eg table relationships). I need to copy (using INSERTs) one DB's tables to another DB. I know I can achieve that by executing commands in proper order (to not break…
Maciej
  • 10,423
  • 17
  • 64
  • 97
241
votes
5 answers

How to add "on delete cascade" constraints?

In PostgreSQL 8 is it possible to add ON DELETE CASCADES to the both foreign keys in the following table without dropping the latter? # \d scores Table "public.scores" Column | Type |…
237
votes
4 answers

"Insert if not exists" statement in SQLite

I have an SQLite database. I am trying to insert values (users_id, lessoninfo_id) in table bookmarks, only if both do not exist before in a row. INSERT INTO bookmarks(users_id,lessoninfo_id) VALUES( (SELECT _id FROM Users WHERE…
user2780638
  • 2,503
  • 2
  • 13
  • 8
227
votes
11 answers

MySQL Removing Some Foreign keys

I have a table whose primary key is used in several other tables and has several foreign keys to other tables. CREATE TABLE location ( locationID INT NOT NULL AUTO_INCREMENT PRIMARY KEY ... ) ENGINE = InnoDB; CREATE TABLE assignment ( …
Drew
  • 15,158
  • 17
  • 68
  • 77
210
votes
5 answers

Can I create a named default constraint in an add column statement in SQL Server?

In SQL Server, I have a new column on a table: ALTER TABLE t_tableName ADD newColumn NOT NULL This fails because I specify NOT NULL without specifying a default constraint. The table should not have a default constraint. To get around this, I…
GlennS
  • 5,251
  • 6
  • 28
  • 31
207
votes
10 answers

Foreign key constraint may cause cycles or multiple cascade paths?

I have a problem when I try to add constraints to my tables. I get the error: Introducing FOREIGN KEY constraint 'FK74988DB24B3C886' on table 'Employee' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION,…
Christian Nielsen
1
2 3
99 100