Chapter 2, Level 2: Constraints Flashcards
What are SQL constraints?
SQL constraints are used specify rules for data in a table. They are used to prevent bad or unwanted data from getting into the tables.
If there is any violation between the constraint (‘rule’) and the data action (e.g. inserting a null value when the constraint forbids a null value), then the action is aborted by the constraint.
What are the six constraints?
- NOT NULL
- UNIQUE
- PRIMARY KEY
- FOREIGN KEY
- CHECK
- DEFAULT
What statement would add a constraint preventing null values from being entered into a table column?
ALTER TABLE table_name
(
CONSTRAINT not_null_name NOT NULL (column_name)
);
What is the Primary Key Constraint?
This constraint (rule) means that a column cannot be NULL but must be UNIQUE.
What is the difference between a PRIMARY KEY vs. NOT NULL + UNIQUE constraints?
A PRIMARY KEY can only be defined once per table.
When a column in one table references the primary key of another table, it’s called a ______?
Foreign Key
A row in a table containing a FOREIGN KEY that references a missing row in another table is called a ______ record?
An orphan record
The ______ constraint is used to validate the value that can be placed in a column.
CHECK