Lesson 2.8 To 3 Flashcards
What is the UPDATE
syntax
UPDATE TableName SET Column1 = Value1, Column2 = Value2, ... WHERE condition;
What is the UPDATE
syntax
UPDATE Employee SET Name = 'Tom Snead', BirthDate = '2000-03-15' WHERE ID = 5384
What would the result be?
UPDATE Employee SET Salary = 42000;
The UPDATE
statement has no WHERE
clause, so the Salary in all rows is changed.
Refer to the Department table.
What is missing to change “Sales” to “Custodial”?
UPDATE Department SET \_\_\_ WHERE Code = 82;
The SET
clause names the column to change and the new value.
UPDATE Department SET Name = 'Custodial' WHERE Code = 82;
Refer to the Department table.
What departments are changed?
UPDATE Department SET Name = 'Administration' WHERE ManagerID IS NOT NULL;
- The WHERE clause changes only rows that do not have a NULL manager.
- Only Technical Support has a NULL manager, so all other departments are renamed Administration.
What is the DELETE
syntax
DELETE FROM TableName WHERE condition;
Refer to the table
What syntax would be used for this?
DELETE FROM Employee WHERE ID = 6381;
What are the results?
DELETE FROM Employee WHERE Salary > 40000 AND Salary < 80000;
Refer to the table
What is missing to delete only Sales
?
DELETE FROM Department WHERE $ ;
Code = 82
Only the Sales row has Code 82, so only the Sales row is deleted.
Refer to the table
What departments are deleted
DELETE FROM Department WHERE ManagerID = 6381;
Sales
and Marketing
Two rows have ManagerID 6381
, so both rows are deleted.
What is the TRUNCATE
syntax
TRUNCATE TABLE TableName;
Refer to the Table | List
- Unique Keys
- Not Unique Keys
- Composite Primary
- Not Minimal
-
ID
andNumber
together is unique - each column on its own wouldn’t work as a primary key in this table because neither is unique
-
ID
&Number
are unique, so (ID, Number) is a composite primary key -
ID, Number, Relationship
is minimal beacuse relationship is Unnecessary
Why can’t (ID, Relationship)
be the primary key of Family
?
(ID, Relationship)
cannot be the primary key because (6381, Daughter)
is repeated. Composite primary keys must be unique.
- The primary key of the PhoneNumber table is not indicated with a solid circle.
- Explain how
(AreaCode, Exchange, Number)
is the primary Key?
-
(AreaCode, Exchange, Number)
is unique, non-null, and minimal; which makes it a composite primary key. - Occasionally, a primary key includes all table columns.
Composite keys can also be referred to as composite primary keys, why is this?
- While all composite primary keys are primary keys, not all primary keys are composite.
- Some primary keys can be simple, consisting of just one column.
The CREATE TABLE
statement uses the keywords PRIMARY KEY
; to indicate the ID
column is the table’s primary key this would be done how?
CREATE TABLE TableName ( Column1 DataType [Constraints], Column2 DataType [Constraints], ... ColumnN DataType [Constraints], \$\$$ (ColumnName) -- Specify primary key if necessary );
CREATE TABLE Employee ( ID SMALLINT UNSIGNED, Name VARCHAR(60), Salary DECIMAL(7,2), PRIMARY KEY (ID) );
The CREATE TABLE
statement uses the keywords PRIMARY KEY
; to indicate the ID
column is the table’s primary key this would be done how?
CREATE TABLE TableName ( Column1 DataType [Constraints], Column2 DataType [Constraints], ... ColumnN DataType [Constraints], \$\$$ (ColumnName) -- Specify primary key if necessary );
CREATE TABLE Family ( ID SMALLINT UNSIGNED, Number SMALLINT UNSIGNED, Relationship VARCHAR(20), Name VARCHAR(60), PRIMARY KEY(ID) );
When would the PRIMARY KEY
constraint include multiple columns?
- The
PRIMARY KEY
constraint includes multiple columns when the primary key is composite. - Making this not null, minimal, & unique
If ID
is an auto-increment column, why would this be incorrect?
INSERT INTO Employee (ID, Name, Salary) VALUES (3, 'Maria Rodriguez', 92300);
- If
ID
is an auto-increment column, theID
should not be listed in theINSERT
statement. - The database assigns the
ID
automatically.
INSERT INTO Employee (Name, Salary) VALUES ('Maria Rodriguez', 92300);
Refer to the table
SELECT CountryName
FROM Country
WHERE ISOCode3 = 'GEO';
Georgia
is the CountryName for which the value of the primary key ISOCode3 is GEO
.
Refer to the table
- Which properties is satisfied by the composite column
(ContinentPopRank, Area)
? - Can
(ContinentPopRank, Area)
be a primary key of theCountry
table?
-
(ContinentPopRank, Area
) is unique. Although neitherContinentPopRank
norArea
is unique, the combination (ContinentPopRank, Area) uniquely identifies each row. -
(ContinentPopRank, Area)
is minimal, since bothContinentPopRank
andArea
are necessary for uniqueness. - Primary keys must be unique and not NULL. Composite primary keys must be minimal.
(ContinentPopRank, Area)
hasNULL
values and therefore cannot be a primary key.
What is needed to complete the code?
Primary Key (TLD)
What is a foreign key?
A foreign key is a column (or set of columns) in one table that references the primary key in another table.
It establishes a relationship between the two tables.
What is the purpose of a foreign key?
To maintain referential integrity within the relational database structure.
What must match between foreign keys and primary keys?
The data types must be the same.
Can foreign key values be different from primary key values in terms of naming
Yes, foreign key values can have different names from primary key values, as they are independent identifiers used to establish relationships.
What are the possible values for foreign keys?
- Repeated
- NULL
What does referential integrity ensure?
Foreign key values must either be NULL
or match a value of the primary key from the referenced table.
In the Department and Employee example, which column in the Department table is a foreign key?
Manager.
What does a self-referencing foreign key do?
It points to its own primary key.
Fill in the blank: Foreign keys can be ______, meaning they consist of multiple columns.
composite.
How are foreign key constraints specified?
Within CREATE TABLE
statements.
What happens if an operation leaves a foreign key without a corresponding primary key?
The database system rejects the operation.
- Different foreign keys in a table refer to the same primary key in another table.
- What is this an example of?
- This is an example is simply a table with multiple foreign keys, pointing to a single table.
- The scenario could be confused with being called “bidirectional”.
Bidirectional relationships involve two tables with foreign keys referencing each other, creating a two-way link.
What is a key characteristic of composite foreign keys?
They must match a composite primary key in another table.
How can Foreign key values be NULL.
- If the foreign key is
NULL
, it simply means that no relationship has been established for that particular record. - A foreign key establishes a link between two tables based on a relationship between the data in the tables.
What are the referential integrity violations?
-
4407
does not match any value inID
and violates referential integrity. - Since
NULL
doesn’t correspond to any actual ID in the Employee table, this constitutes a referential integrity violation.
What is the referential integrity violation?
-
(6381, 4)
does not match any value in(ID, Number)
and violates referential integrity. - Referential integrity can be violated if a primary key is changed, a foreign key is updated, a row containing a primary key is deleted, or if a row containing a foreign key is inserted.
What is the referential integrity violation?
-
(NULL, 1)
is partiallyNULL
and violates referential integrity. - Referential integrity can be violated if a primary key is changed, a foreign key is updated, a row containing a primary key is deleted, or if a row containing a foreign key is inserted.
What is the referential integrity violation?
Updating the Employee
primary key to 8888
violates referential integrity because the foreign key 2538
no longer exists in Department
.
What is the referential integrity violation?
Updating the foreign key to 3333
violates referential integrity because 3333
does not match a primary key value
What is the referential integrity violation?
Deleting Employee
primary key 6381
violates referential integrity because the foreign key 6381
no longer exists in Employee
.
What is the referential integrity violation?
Inserting foreign key 0202
violates referential integrity because 0202
does not match a primary key value.
What is the the syntax for a CREATE TABLE
statement with a FOREIGN KEY
constraint
CREATE TABLE TableName ( column1 datatype, column2 datatype, ... PRIMARY KEY (column), FOREIGN KEY (foreign_key_column) REFERENCES parent_table(parent_table_column) );
From this image and code we can aknowledge that what data is shared?
CREATE TABLE Department (
Code TINYINT UNSIGNED,
Name VARCHAR(20),
ManagerID SMALLINT UNSIGNED,
PRIMARY KEY (Code),
FOREIGN KEY (ManagerID) REFERENCES Employee(ID)
);
The Department
table is created with a FOREIGN KEY
constraint that REFERENCES
the Employee ID
column.
What is the order of FOREIGN KEY constraint and column declarations
The order is not significant however, column declarations normally appear first, followed by the PRIMARY KEY constraint and the FOREIGN KEY constraint.
In a FOREIGN KEY constraint, where are parentheses required?
CREATE TABLE TableName ( column1 datatype, column2 datatype, ... PRIMARY KEY (column), FOREIGN KEY (foreign_key_column) REFERENCES parent_table(parent_table_column) );
Parentheses are required around both the foreign key and primary key column names.
How does adding a FOREIGN KEY
constraint to a table effect rows and columns?
- Ensuring Data Integrity: Validates foreign key values against primary key values, which may limit valid rows.
-
Enforcing Cascade Effects: Automatic changes from actions like
ON DELETE CASCADE
can indirectly affect row counts. - Restricting Insertions: Limits rows if corresponding referenced rows do not exist.
- Establishing Relationships: Links tables for better data organization.
In the Department table
, first three columns; which foreign key value violates referential integrity?
- Since no employee has
ID 3829
; it violates referential integrity. - Referential integrity can be violated if a primary key is changed, a foreign key is updated, a row containing a primary key is deleted, or if a row containing a foreign key is inserted.
Why do both (6381, 4)
& (6381, NULL)
foreign key values violate referential integrity?
- (6381, 4) does not match any (ID, Number) in a Family row and thus violates referential integrity.
- (6381, NULL) is partially null and thus violates referential integrity.
Is it true that if a column is partially NULL
that this should violate referential integrity?
Manager
is a single-column, or simple, foreign key. A NULL in a simple foreign key does not violate referential integrity.
The row containing primary key 2538
is deleted.
How would RESTRICT
access this situation?
RESTRICT
rejects the delete, since employee 2538
manages Engineering
.
Prevents the deletion or update of a parent record
The row containing primary key 2538
is deleted.
How would SET NULL
access this situation?
SET NULL
sets matching foreign keys to NULL
.
allows child records to exist but removes their link to the deleted parent
The row containing primary key 2538
is deleted.
How would SET DEFAULT
access this situation?
SET DEFAULT
sets matching foreign keys to the foreign key default value, 6381
.
The row containing primary key 2538
is deleted.
How would CASCADE
access this situation?
CASCADE
deletes all rows with matching foreign key values.
ensures all dependent records are cleaned up
What is the syntax for referntial integrity actions such as
RESTRICT, SET NULL, SET DEFAULT, CASCADE
Referential integrity actions on primary key DELETE.
CREATE TABLE child_table ( id INT, parent_id INT, FOREIGN KEY (parent_id) REFERENCES parent_table(id) ON DELETE CASCADE -- or SET NULL, SET DEFAULT, RESTRICT );
The syntax for referential integrity actions in SQL starts with ON DELETE or ON UPDATE.
Refer to the Employee and Department tables.
- What are the results of the actions below?
-
RESTRICT
, when the row containingMaria Rodriguez
is deleted.
-
RESTRICT
rejects referential integrity violations. - If
Maria Rodriguez
is deleted, the managers ofSales
andMarketing
would not match any ID, so the delete is rejected.
Refer to the Employee and Department tables.
- What are the results of the actions below?
-
SET NULL
, whenLisa Ellison's
ID is changed to1001
.
-
SET NULL
nullifies matching foreign keys. - Since the
Lisa Ellison
managesEngineering
, changing Lisa’s ID sets theEngineering
manager toNULL
.
Refer to the Employee and Department tables.
- What are the results of the actions below?
- SET DEFAULT, when Lisa Ellison’s ID is changed to 1001.
-
SET DEFAULT
sets matching foreign keys to a default value. - Since
Lisa Ellison
managesEngineering
, theEngineering
manager is set to a default value, which must be a valid primary key in theEmployee
table. - Any defaults value relies on whatever default value was specifically set during the table’s definition.
Refer to the Employee and Department tables.
- What are the results of the actions below?
-
CASCADE
, whenMaria Rodriguez
’ ID is changed to2022
.
-
CASCADE
propagates primary key changes & deletions accordingly to matching foreign keys. - Maria Rodriguez manages Sales and Marketing, so
Sales
andMarketing
managers are set to2022
.
Refer to the Employee and Department tables.
- What are the results of the actions below?
-
CASCADE
, whenMaria Rodriguez
is deleted.
-
CASCADE
propagates primary key changes & deletions accordingly to matching foreign keys. - Maria Rodriguez manages Sales and Marketing, so the
Sales
andMarketing
rows are deleted.
What is the create & alter syntax for creating foreign key constraints with the ON UPDATE
and ON DELETE
clauses in SQL.
CREATE TABLE child_table ( column1 datatype, column2 datatype, -- other columns CONSTRAINT fk_name FOREIGN KEY (child_column) REFERENCES parent_table (parent_column) ON DELETE action ON UPDATE action );
Foreign key constraints with ON UPDATE and ON DELETE clauses.
Explain how DELETE CASCADE
creates this result
ON DELETE CASCADE
causes the database to delete the row with ManagerID 7343
when the employee with ID 7343
is deleted.
Foreign key constraints with ON UPDATE and ON DELETE clauses.
Explain how UPDATE SET NULL
creates this result
ON UPDATE SET NULL
causes the database to set ManagerID 2538
to NULL
when the Employee ID 2538
is changed to 8754
.
What is referential integrity?
A relational database requires that foreign key values must either be fully NULL or match a primary key value.
What does the CASCADE
action do in referential integrity?
Deletes or updates the corresponding child records automatically when a parent record is deleted or updated.
What happens when SET NULL is applied in referential integrity?
Sets the foreign key value in the child records to NULL
when the parent record is deleted or updated.
What is the effect of the SET DEFAULT action in referential integrity?
Sets the foreign key value in the child records to its default value when the parent record is deleted or updated.
What does the RESTRICT action do in referential integrity?
Prevents the deletion or update of a parent record if there are any corresponding child records.
What are common causes of referential integrity violations?
Violations can occur through:
* Updating a primary key.
* Updating a foreign key.
* Deleting a row with a primary key.
* Inserting a row with a foreign key that does not match any primary key.
How can referential integrity violations be identified?
Check foreign key values in tables to find violations and NULL
values.
What does a fully NULL foreign key consist of?
All columns being NULL.
True or False: A NULL in a simple foreign key violates referential integrity.
False.
What happens when a primary key is updated to a value that doesn’t exist?
It violates referential integrity.
What is the default action in MySQL if no specific action is defined in the ON UPDATE or ON DELETE clause?
RESTRICT.
What limitation does SET NULL
have in MySQL?
It is not allowed if the foreign key column is defined as NOT NULL
.
What is the role of foreign keys in maintaining referential integrity?
Foreign keys maintain referential integrity between tables by ensuring a match with primary keys or being NULL.
The _______ action sets the foreign key to a predefined default value if the referenced primary key is deleted or updated.
SET DEFAULT
What does the ON DELETE CASCADE action do?
Deletes the row with foreign key when the referenced primary key is deleted.
What does the ON UPDATE SET NULL action do?
Sets the foreign key to NULL when the referenced primary key is updated.
What is a composite foreign key?
A foreign key that consists of more than one column.
What happens if an insert or update would result in a foreign key that does not match an existing primary key in MySQL?
The operation is rejected automatically.
What SQL statement is used to insert a new row into an existing table?
INSERT INTO TableName (Column1, Column2, ...) VALUES (Value1, Value2, ...);
What SQL statement is used to update existing rows in a table?
UPDATE TableName SET Column1 = NewValue1, Column2 = NewValue2, ... WHERE Condition;
What SQL statement is used to delete rows from an existing table?
DELETE FROM TableName WHERE Condition;
What happens if a NULL
value is inserted into a column with a NOT NULL
constraint?
The database system will raise an error, and the insertion will fail.
What occurs if a duplicate value is inserted into a column with a UNIQUE
constraint?
The insertion will be rejected, and an error will be raised.
What is the effect of violating a CHECK
constraint during an insertion?
The database will raise an error, and the insertion will fail.
List the column constraints in SQL.
- NOT NULL
- DEFAULT
- P̶r̶i̶m̶a̶r̶y̶ K̶e̶y̶
- U̶n̶i̶q̶u̶e̶
List the table constraints in SQL.
- PRIMARY KEY
- FOREIGN KEY
- 𝗨𝗡𝗜𝗤𝗨𝗘
- 𝗖𝗛𝗘𝗔𝗞
CHANGE CurrentColumnName NewColumnName NewDataType [ConstraintDeclaration]
If this is adding an unnamed constraint like NOT NULL
to an existing/non existing table?
The provided syntax is for modifying an existing table (using ALTER TABLE
).
What is the syntax to add a named PRIMARY KEY constraint?
ADD [CONSTRAINT ConstraintName] PRIMARY KEY (Column1, Column2 …)
What is the syntax to drop a FOREIGN KEY constraint?
DROP FOREIGN KEY ConstraintName
What happens when trying to add a constraint to a table with existing data violating that constraint?
Adding a constraint fails when the table contains data that violates the constraint.
What must be done before dropping a table that has a foreign key constraint referring to its primary key?
Either the foreign key constraint or the foreign key table must be dropped.
What is the syntax to drop any named constraint?
DROP CONSTRAINT
ConstraintName
Constraints are added and dropped with the ALTER TABLE TableName
followed by an _______ clause.
[ADD, DROP, or CHANGE]