Section 2.12 Constraints Flashcards

1
Q

What is a constraint in the context of a database?

A
  1. A constraint is a rule that governs allowable values in a database and is implemented with special keywords in a CREATE TABLE statement.
  2. The database automatically rejects any insert, update, or delete statements that violate a constraint.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the syntax for creating a NOT NULL constraint in a CREATE TABLE statement?

A

A NOT NULL constraint is placed after the column name and data type.

Example:
sql Name VARCHAR(20) NOT NULL

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

How do you define a FOREIGN KEY constraint in SQL?

A

A FOREIGN KEY constraint is defined in a separate clause of a CREATE TABLE statement.

Example:
sql FOREIGN KEY (DepartmentCode) REFERENCES Department (Code)

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

Provide an example of a CREATE TABLE statement with multiple constraints including a PRIMARY KEY.

A

sql CREATE TABLE Employee ( ID INT, Name VARCHAR(20) NOT NULL, DepartmentCode INT DEFAULT 999, PRIMARY KEY (ID), FOREIGN KEY (DepartmentCode) REFERENCES Department (Code) );

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

What does the DEFAULT constraint do?

A

The DEFAULT constraint specifies a value inserted when a column is omitted from an INSERT statement but does not limit allowable values.

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

How can you enforce uniqueness in a column using a constraint?

A

Use the UNIQUE constraint, which ensures that all values in a column or a group of columns are unique.

Example:
sql UNIQUE (ColumnName)

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

What is the purpose of a CHECK constraint?

A

A CHECK constraint specifies an expression that must be satisfied for data to be allowed in the table, violating when the expression is FALSE.

Example:
sql CHECK (BirthDate < HireDate)

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

How do you add a CHECK constraint in a CREATE TABLE statement?

A

It can be added in the column declaration or as a separate clause.

Example:
sql CHECK (HireDate >= '2000-01-01' AND HireDate <= '2019-12-31')

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

How can you name a constraint when creating a table?

A

Use the CONSTRAINT keyword followed by the name of the constraint and the constraint declaration.

Example:
sql CONSTRAINT ConstraintName UNIQUE (Column1, Column2)

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

What is the syntax to add a new CHECK constraint using ALTER TABLE?

A

Use the following syntax to add a CHECK constraint:

sql ALTER TABLE TableName ADD CONSTRAINT ConstraintName CHECK (condition);

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

What is the command to drop the UNIQUE constraint called UniqueNameMgr from a table?

A

Use the following SQL command:

sql ALTER TABLE Department DROP INDEX UniqueNameMgr;

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

What is the syntax for adding a PRIMARY KEY constraint to an existing table?

A

The syntax is:

sql ADD [CONSTRAINT ConstraintName] PRIMARY KEY (Column1, Column2 ...)

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

What type of constraint is used to ensure that a column cannot have a NULL value?

A

The NOT NULL constraint is used to ensure that a column cannot contain NULL values.

Example:

sql ColumnName VARCHAR(50) NOT NULL

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

What is the syntax for defining a composite PRIMARY KEY in a CREATE TABLE statement?

A

A composite PRIMARY KEY is defined by specifying multiple columns in a table constraint.

Example:

sql PRIMARY KEY (Column1, Column2)

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

What SQL command is used to add a UNIQUE constraint to a table?

A

Use the ALTER TABLE command with ADD CONSTRAINT.

Example:

sql ALTER TABLE TableName ADD CONSTRAINT UniqueConstraintName UNIQUE (ColumnName);

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

How can you check if a constraint has been violated in a table?

A

When attempting to insert or update data that violates a constraint, the database generates an error message indicating the violation.

17
Q

What is the difference between a UNIQUE constraint and a PRIMARY KEY constraint?

A

A PRIMARY KEY constraint does not allow NULL values and uniquely identifies each record, while a UNIQUE constraint allows one NULL value and ensures all other values are unique.

18
Q

Can multiple FOREIGN KEY constraints be defined in a single table?

A

Yes, multiple FOREIGN KEY constraints can be defined in a single table, each referencing different parent tables or columns.

19
Q

How can you enforce that a HireDate must be later than a BirthDate using a constraint?

A

You can use a CHECK constraint.

Example:

sql CHECK (HireDate > BirthDate)

20
Q

What happens if a CHECK constraint is violated during an INSERT operation?

A

If a CHECK constraint is violated, the INSERT operation fails, and an error message is returned, preventing the data from being saved in the table.

21
Q

What is an example SQL command to drop a PRIMARY KEY constraint from a table?

A

Use the following SQL command to drop a primary key constraint:

sql ALTER TABLE TableName DROP PRIMARY KEY;

22
Q

How do you alter an existing column to add a DEFAULT constraint?

A

You can alter the column with the ALTER TABLE command:

sql ALTER TABLE TableName ALTER COLUMN ColumnName SET DEFAULT 'DefaultValue';

23
Q

What is the significance of naming constraints?

A

Naming constraints helps identify them in error messages and makes database management easier by allowing you to drop or modify specific constraints.

24
Q

How can you specify that a column must be greater than a certain value using a constraint?

A

You would use a CHECK constraint for this purpose.

Example:

sql CHECK (ColumnName > 1000)

25
Q

What is the syntax for adding a foreign key constraint to an existing table?

A

Use the ALTER TABLE command with ADD CONSTRAINT.

Example:

sql ALTER TABLE TableName ADD CONSTRAINT FK_ColumnName FOREIGN KEY (ColumnName) REFERENCES OtherTable (OtherColumn);