Package org.h2.constraint

# Class Constraint

java.lang.Object
org.h2.engine.DbObject
org.h2.schema.SchemaObject
org.h2.constraint.Constraint

All Implemented Interfaces:
  `Comparable<Constraint>`, `HasSQL`

Direct Known Subclasses:
  `ConstraintCheck`, `ConstraintDomain`, `ConstraintReferential`, `ConstraintUnique`

public abstract class Constraint extends SchemaObject implements Comparable<Constraint>
The base class for constraint checking.

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`static enum`
`Constraint.Type`

## Field Summary

Fields
Modifier and Type
Field
Description
`protected Table`
`table`
The table for which this constraint is defined.

### Fields inherited from class org.h2.engine.DbObject

`AGGREGATE, comment, COMMENT, CONSTANT, CONSTRAINT, database, DOMAIN, FUNCTION_ALIAS, INDEX, RIGHT, ROLE, SCHEMA, SEQUENCE, SETTING, SYNONYM, TABLE_OR_VIEW, trace, TRIGGER, USER`

### Fields inherited from interface org.h2.util.HasSQL

`ADD_PLAN_INFORMATION, DEFAULT_SQL_FLAGS, NO_CASTS, QUOTE_ONLY_WHEN_REQUIRED, REPLACE_LOBS_FOR_TRACE, TRACE_SQL_FLAGS`

## Method Summary

Modifier and Type
Method
Description
`abstract void`
`checkExistingData(SessionLocal session)`
Check the existing data.
`abstract void`
`checkRow(SessionLocal session, Table t, Row oldRow, Row newRow)`
Check if this row fulfils the constraint.
`int`
`compareTo(Constraint other)`
`abstract Constraint.Type`
`getConstraintType()`
The constraint type name
`abstract String`
`getCreateSQLWithoutIndexes()`
Get the SQL statement to create this constraint.
`Expression`
`getExpression()`
Returns the CHECK expression or null.
`Index`
`getIndex()`
Get the index of this constraint in the source table, or null if no index is used.
`abstract HashSet<Column>`
`getReferencedColumns(Table table)`
Get all referenced columns.
`ConstraintUnique`
`getReferencedConstraint()`
Returns the referenced unique constraint, or null.
`Table`
`getRefTable()`
`Table`
`getTable()`
`int`
`getType()`
Get the object type.
`abstract boolean`
`isBefore()`
Check if this constraint needs to be checked before updating the data.
`boolean`
`isEverything(ExpressionVisitor visitor)`
Visit all elements in the constraint.
`abstract void`
`rebuild()`
This method is called after a related table has changed (the table was renamed, or columns have been renamed).
`abstract void`
`setIndexOwner(Index index)`
This index is now the owner of the specified index.
`abstract boolean`
`usesIndex(Index index)`
Check if this constraint needs the specified index.

### Methods inherited from class org.h2.schema.SchemaObject

`getSchema, getSQL, getSQL`

### Methods inherited from class org.h2.engine.DbObject

`checkRename, equals, getChildren, getComment, getCreateSQL, getCreateSQLForCopy, getCreateSQLForMeta, getDatabase, getDropSQL, getId, getModificationId, getName, hashCode, invalidate, isTemporary, isValid, removeChildrenAndResources, rename, setComment, setModified, setObjectName, setTemporary, toString`

### Methods inherited from class java.lang.Object

`clone, finalize, getClass, notify, notifyAll, wait, wait, wait`

### Methods inherited from interface org.h2.util.HasSQL

`getTraceSQL`

## Field Details

### table

protected Table table
The table for which this constraint is defined.

## Method Details

### getConstraintType

public abstract Constraint.Type getConstraintType()
The constraint type name

Returns:
  the name

### checkRow

public abstract void checkRow(SessionLocal session, Table t, Row oldRow, Row newRow)
Check if this row fulfils the constraint. This method throws an exception if not.

Parameters:
  `session` - the session
  `t` - the table
  `oldRow` - the old row
  `newRow` - the new row

### usesIndex

public abstract boolean usesIndex(Index index)
Check if this constraint needs the specified index.

Parameters:
  `index` - the index
Returns:
  true if the index is used

### setIndexOwner

public abstract void setIndexOwner(Index index)
This index is now the owner of the specified index.

Parameters:
  `index` - the index

### getReferencedColumns

public abstract HashSet<Column> getReferencedColumns(Table table)
Get all referenced columns.

Parameters:
  `table` - the table
Returns:
  the set of referenced columns

### getExpression

public Expression getExpression()
Returns the CHECK expression or null.

Returns:
  the CHECK expression or null.

### getCreateSQLWithoutIndexes

public abstract String getCreateSQLWithoutIndexes()
Get the SQL statement to create this constraint.

Returns:
  the SQL statement

### isBefore

public abstract boolean isBefore()
Check if this constraint needs to be checked before updating the data.

Returns:
  true if it must be checked before updating

### checkExistingData

public abstract void checkExistingData(SessionLocal session)
Check the existing data. This method is called if the constraint is added after data has been inserted into the table.

Parameters:
  `session` - the session

### rebuild

public abstract void rebuild()
This method is called after a related table has changed (the table was renamed, or columns have been renamed).

### getIndex

public Index getIndex()
Get the index of this constraint in the source table, or null if no index is used.

Returns:
  the index

### getReferencedConstraint

public ConstraintUnique getReferencedConstraint()
Returns the referenced unique constraint, or null.

Returns:
  the referenced unique constraint, or null

### getType

public int getType()
Description copied from class: `DbObject`
Get the object type.

Specified by:
  `getType` in class `DbObject`
Returns:
  the object type

### getTable

public Table getTable()

### getRefTable

public Table getRefTable()

### compareTo

public int compareTo(Constraint other)

Specified by:
  `compareTo` in interface `Comparable<Constraint>`

### isEverything

public boolean isEverything(ExpressionVisitor visitor)
Visit all elements in the constraint.

Parameters:
  `visitor` - the visitor
Returns:
  true if every visited expression returned true, or if there are no expressions