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 TableName SET Column1 = Value1, Column2 = Value2, ... WHERE condition;
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, Number) );
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.