Section 2.12 Constraints Flashcards

(38 cards)

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.

CREATE TABLE table_name (
    column1_name data_type NOT NULL,
    column2_name data_type 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.

CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    Name VARCHAR(255),
    DepartmentCode INT,
    FOREIGN KEY (DepartmentCode) REFERENCES Departments(Code)
);
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

This statement provides an example with which types of constraints?

A
  1. PRIMARY KEY: Uniquely identifies each row in the Employee table (ID column).
  2. NOT NULL: Ensures the Name column always has a value.
  3. FOREIGN KEY: Links Employee records to valid Department records (DepartmentCode referencing Code).
  4. DEFAULT: Sets a default value of β€˜999’ for DepartmentCode if none is given.

These constraints ensure data integrity and consistency in the database.

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
  1. The UNIQUE constraint, which ensures that all values in a column; or a group of columns are unique.
  2. Demonstrate the UNIQUE constraint (create, alter)?
A
CREATE TABLE your_table_name (
   column_name DATA_TYPE UNIQUE,
   -- other columns and constraints
);

ALTER TABLE your_table_name
ADD CONSTRAINT unique_constraint_name UNIQUE (column_name);

There is no need to ALTER column since it’s included last

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

Add a constraint CK_BirthDate to the existing or non-existent table Employees specifying that BirthDate must be greater than HireDate which satisfies data to be allowed in the table.

A
CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    LastName VARCHAR(255) NOT NULL,
    FirstName VARCHAR(255),
    BirthDate DATE,
    HireDate DATE,
    CHECK (BirthDate < HireDate) 
);

ALTER TABLE Employees
  ADD CONSTRAINT CK_BirthDate 
  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.

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
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:

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 called DEPARTMENT?

A

Use the following SQL command:

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
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
  1. Inline constraint definition you specify the PRIMARY KEY constraint directly within the column definitions.
  2. OOL constraint definition you specify the PRIMARY KEY constraint separately from the column definitions, usually after all columns have been declared.
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 an existing table?

A

Use the ALTER TABLE command with ADD CONSTRAINT.

Example:

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

After 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 PRIMARY KEY constraint and a UNIQUE constraint?

A
  1. A PRIMARY KEY constraint does not allow NULL values and uniquely identifies each record.
  2. 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 $ command & SET DEFAULT:

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 > $)

25
What is the syntax for adding a foreign key constraint to an existing table?
Use the `ALTER TABLE` command with `ADD CONSTRAINT`. Example: ``` ALTER TABLE TableName ADD CONSTRAINT FK_Name FOREIGN KEY (ColumnName) REFERENCES OtherTable (OtherColumn); ```
26
What category is a`FOREIGN KEY` constraint?
A FOREIGN KEY is exclusively a table constraint. ## Footnote A FOREIGN KEY is used to establish a relationship between two tables.
27
What SQL statement **retrieves** the creation *details of a specified* table?
`SHOW CREATE TABLE TableName` *retrieves the SQL statement* that creates the specified table, including the definitions for all constraints that are associated with it. ## Footnote This command is useful for understanding the structure and constraints of an existing table.
28
What SQL command can be used to find constraints associated *with a specific table*?
``` SELECT Column_Name, Constraint_Name FROM Information_Schema.Key_Column_Usage WHERE Table_Name = 'TableName'; ``` ## Footnote This query provides information on the key columns and their constraints in the specified table.
29
How can unnamed constraints like `NOT NULL` and `DEFAULT` be modified?
Unnamed constraints such as `NOT NULL` and `DEFAULT` are added or dropped with a `CHANGE` clause. ## Footnote The CHANGE clause allows for altering the structure of the table without needing to drop and recreate the entire column.
30
What does Referential Integrity _Constraints_ ensure?
A FK in one table matches a PK or unique key in another table, *maintaining consistent relationships* between tables ## Footnote This is crucial for database integrity and prevents orphaned records.
31
What is an Insert Constraint in Referential Integrity?
Ensures that any new foreign key value inserted into a table corresponds to an existing primary key value in the related table ## Footnote This constraint prevents invalid foreign key entries.
32
What does a Delete Constraint do?
Prevents the deletion of a record in the PK table *if it has related records* in a FK table ## Footnote Options like CASCADE, SET NULL, and RESTRICT control the outcome of such deletions.
33
What are the options available for controlling Delete Constraints?
* CASCADE * SET NULL * RESTRICT ## Footnote Each option dictates a different action when a deletion is attempted in the primary key table.
34
What are Cascading Updates?
Automatically updates foreign key values in one table if the corresponding primary key values in another table are updated ## Footnote This feature helps maintain data consistency across related tables.
35
Why wouldn't '*The offending value would be set to the database default.*' be something that could avoid violating a `RESTRICT` referential integrity constraint?
This is incorrect because setting a violating value to the database default would not comply with the RESTRICT constraint, which prevents any update leading to a violation.
36
Why is this update process 'The update would be rejected by the database.' also a violation of `RESTRICT` referential integrity constraint?
Because a RESTRICT constraint ensures that any update violating referential integrity is rejected, thus preserving data integrity.
37
Why wouldn't '*The offending value would be changed to NULL*.' be something that could avoid violating a `RESTRICT` referential integrity constraint?
Because changing the offending value to NULL doesn't align with a RESTRICT constraint, as it requires the update to be completely blocked if it violates integrity.
38
Why wouldn't '*The updated value would flow to the primary key*.' be something that could avoid violating a `RESTRICT` referential integrity constraint?
This is incorrect because the RESTRICT constraint does not allow the update to affect the primary key; it blocks the update entirely if constraints are violated.