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
3
votes
1 answer

Deleting from a MySQL table with foreign key constraints

I have two tables, with some data loaded in it and the two reference each other. Table B references the primary key of Table A. I manually attempt to delete some table rows from Table A that are present in Table B, and I get this: #1451 - Cannot…
Parijat Kalia
  • 4,929
  • 10
  • 50
  • 77
3
votes
2 answers

Capture values that trigger DUP_VAL_ON_INDEX

Given this example (DUP_VAL_ON_INDEX Exception), is it possible to capture the values that violated the constraint so they may be logged? Would the approach be the same if there are multiple violations generated by a bulk insert? BEGIN -- want to…
craig
  • 25,664
  • 27
  • 119
  • 205
3
votes
1 answer

Need to add constraint: date plus 10 days

I'm trying to add a constraint to a table so that it displays one of the columns as the current date plus 10 days. Here's what I've tried so far (I'm very new to SQL): ALTER TABLE orders ADD CONSTRAINT default_date DEFAULT DATEADD…
isolatedhowl
  • 171
  • 2
  • 6
  • 17
3
votes
3 answers

Implementing table level check constraint

We have a table that contains prices that are depending on a base amount. As an example let say that if the base amount is less or equal to 100 then the price is 10 but if the base amount is greater that 100 but less or equal to 1000 then the price…
Peter Å
  • 1,269
  • 11
  • 20
3
votes
1 answer

With symfony2 Is it possible to use constraintValidator service to validate properties

I followed the following Symfony2 cookbook tutorial , but I keep getting the error constraint myContraintClass cannot be put on properties or getters in validation.yml I have Core\Entity\Activity: properties: maxTotalParticipant: -…
svassr
  • 5,538
  • 5
  • 44
  • 63
3
votes
0 answers

Using dynamic scaffolding with a inlist constraint and dynamic finders

Grails 2.0.4 - Using dynamic scaffolding with a inlist constraint and dynamic finders. I am trying to use a dynamic finder in the inList constraint of a domain object. If I change the constraint while the application is active via run-app to sample…
3
votes
1 answer

How can I represent rows and columns in an application in MSSQL?

I have an application that displays tabular information and allows the user to enter and edit it. I have been working on the database design for this and have come across the following problem. I want to avoid creating an MSSQL table for each table…
Michael J. Gray
  • 9,784
  • 6
  • 38
  • 67
3
votes
1 answer

ON DUPLICATE KEY UPDATE causing foreign key constraint error

I searched the StackOverflow, but I couldn't find an answer to my specific question. Recently I have become a big fan of updating records with a list, using "INSERT INTO...ON DUPLICATE KEY UPDATE." However, I had problems with the following…
Expedito
  • 7,771
  • 5
  • 30
  • 43
3
votes
1 answer

Oracle check constraint for varray type

I have a custom type defined as this : CREATE TYPE myType_t AS VARRAY(2) of char(10); Is it possible to add a check constraint on the char(10) type, so myType_t items respect a certain regex? I tried things like CREATE TYPE myType_t AS VARRAY(2)…
Pacane
  • 20,273
  • 18
  • 60
  • 97
3
votes
4 answers

Read-only fields in SQLite3 database table?

I'm a beginner when it comes to databases and have been reading through the SQLite3 documentation. However, I can't find the answer to the following: Is it possible to create a SQLite3 database table that has some read-only fields (i.e. not the…
Umbungu
  • 945
  • 3
  • 10
  • 30
3
votes
3 answers

Xcode constraints for iPhone5 messing me up

I'm trying to do a thing that seems easy but since the new auto layout in xcode I having a hard time to understand what's going on with the constraints. The thing I want to do is 2 subviews in a main view basically one scrollview at the top and one…
Mathieu
  • 1,175
  • 4
  • 19
  • 34
3
votes
2 answers

Database Child Table with Two Possible Parents

First off, I'm not sure how exactly to search for this, so if it's a duplicate please excuse me. And I'm not even sure if it'd be better suited to one of the other StackExchange sites; if so, please let me know and I'll ask over there instead. …
MCory
  • 437
  • 2
  • 13
3
votes
2 answers

Fix for Innodb Integrity constraint violation: 1452

What Innodb query can I use to correct the following problem? SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (mrvallar_magdb.catalog_product_entity_varchar, CONSTRAINT…
Road Ready
  • 31
  • 2
3
votes
2 answers

raphaeljs How to restrict element drag with constrained movement (within parent bounding box)

I am Trying to tweak this example http://raphaeljs.com/graffle.html to restrict the drag to within the container svg. Somewhat like http://bl.ocks.org/1557377 or http://jqueryui.com/demos/draggable/#constrain-movement Basically I want to restrict…
Prashant Bhate
  • 10,907
  • 7
  • 47
  • 82
3
votes
2 answers

Switch values between rows in unique column

Let's say you had a table like this: id | name ---+--------- 1 | foo 2 | bar There is a uniqueness constraint on the name column. How would you set the row with id=2 to foo, and the row with id=1 to bar? I know you could probably just assign…
yvkk