1.12 Constraints Flashcards

1
Q

constraint

A

A constraint is a rule that governs allowable values in a database. Constraints are based on relational and business rules, and implemented with special keywords in a CREATE TABLE statement. The database automatically rejects insert, update, and delete statements that violate a constraint.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

column constraint

A

A column constraint appears after the column name and data type in a CREATE TABLE statement. Column constraints govern values in a single column.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

table constraint

A

A table constraint appears in a separate clause of a CREATE TABLE statement and governs values in one or more columns.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

UNIQUE constraint

A

The UNIQUE constraint ensures that values in a column, or group of columns, are unique.

Can be used as a column constraint (single collum), which cannot be named. Or used as a table contraint (multiple columns), which can be named.

Name VARCHAR(20) UNIQUE

CONSTRAINT UniqueNameMgr UNIQUE (Name, ManagerID)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

CHECK

A

The CHECK constraint specifies an expression on one or more columns of a table. The constraint is violated when the expression is FALSE and satisfied when the expression is either TRUE or NULL.

Only column constraint that can be named.

CONSTRAINT CheckManager CHECK (ManagerID > 999)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

CONSTRAINT keyword usage

A

Table constraints may be named using the optional CONSTRAINT keyword, followed by the constraint name and declaration.

CONSTRAINT DepartmentKey PRIMARY KEY (Code)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Add or remove a constraint

A

ALTER TABLE TableName followed by an ADD, DROP, or CHANGE clause.

ALTER TABLE Employee
ADD CONSTRAINT HireCheck CHECK (HireDate < ‘2000-02-14’);

Unnamed constraints such as NOT NULL and DEFAULT are added or dropped with a CHANGE clause:

CHANGE CurrentColumnName NewColumnName NewDataType [ConstraintDeclaration]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly