Podcast
Questions and Answers
What does the ON UPDATE CASCADE option accomplish in a foreign key constraint?
What does the ON UPDATE CASCADE option accomplish in a foreign key constraint?
What should be the typical approach regarding NULL values in attributes according to good practices?
What should be the typical approach regarding NULL values in attributes according to good practices?
Which of the following is NOT an option for the action taken on delete in a foreign key constraint?
Which of the following is NOT an option for the action taken on delete in a foreign key constraint?
Defer is usually recommended for which type of constraints?
Defer is usually recommended for which type of constraints?
Signup and view all the answers
What could be a consequence of using ON DELETE CASCADE?
What could be a consequence of using ON DELETE CASCADE?
Signup and view all the answers
Which of these practices is a part of good database design?
Which of these practices is a part of good database design?
Signup and view all the answers
What is the main caution regarding foreign keys and the CASCADE option?
What is the main caution regarding foreign keys and the CASCADE option?
Signup and view all the answers
What action does the SET NULL option perform when applied to a foreign key constraint?
What action does the SET NULL option perform when applied to a foreign key constraint?
Signup and view all the answers
What defines a consistent state of the database?
What defines a consistent state of the database?
Signup and view all the answers
Which transaction control keywords are used to manage transactions in SQL?
Which transaction control keywords are used to manage transactions in SQL?
Signup and view all the answers
What is a primary advantage of deferred integrity constraints?
What is a primary advantage of deferred integrity constraints?
Signup and view all the answers
What property ensures that all parts of a transaction are completed or none at all?
What property ensures that all parts of a transaction are completed or none at all?
Signup and view all the answers
Which of the following describes integrity constraints?
Which of the following describes integrity constraints?
Signup and view all the answers
In practice, what is a limitation of many systems regarding integrity constraints?
In practice, what is a limitation of many systems regarding integrity constraints?
Signup and view all the answers
What scenario does the term 'atomicity' apply to in database transactions?
What scenario does the term 'atomicity' apply to in database transactions?
Signup and view all the answers
Which aspect of transactions is NOT considered a characteristic of the ACID properties?
Which aspect of transactions is NOT considered a characteristic of the ACID properties?
Signup and view all the answers
Which of the following is NOT a kind of integrity constraint supported by SQL?
Which of the following is NOT a kind of integrity constraint supported by SQL?
Signup and view all the answers
What happens when you execute the command 'DELETE FROM customers;'?
What happens when you execute the command 'DELETE FROM customers;'?
Signup and view all the answers
Which statement about a primary key is true?
Which statement about a primary key is true?
Signup and view all the answers
Which command would you use to permanently remove both the data and structure of the customers table?
Which command would you use to permanently remove both the data and structure of the customers table?
Signup and view all the answers
What is the maximum number of primary keys a single table can have?
What is the maximum number of primary keys a single table can have?
Signup and view all the answers
What behavior does the CHECK constraint enforce?
What behavior does the CHECK constraint enforce?
Signup and view all the answers
Which of the following statements is false regarding foreign keys?
Which of the following statements is false regarding foreign keys?
Signup and view all the answers
Which of the following accurately defines an integrity constraint?
Which of the following accurately defines an integrity constraint?
Signup and view all the answers
What defines a primary key in SQL?
What defines a primary key in SQL?
Signup and view all the answers
What does a UNIQUE constraint ensure in a database table?
What does a UNIQUE constraint ensure in a database table?
Signup and view all the answers
Which of the following statements about the UNIQUE constraint in SQL is correct?
Which of the following statements about the UNIQUE constraint in SQL is correct?
Signup and view all the answers
What happens if you attempt to create a record in a table with a duplicate value in a UNIQUE column?
What happens if you attempt to create a record in a table with a duplicate value in a UNIQUE column?
Signup and view all the answers
In the given table creation example, which column has a UNIQUE constraint applied?
In the given table creation example, which column has a UNIQUE constraint applied?
Signup and view all the answers
How is a primary key related to the UNIQUE constraint in SQL?
How is a primary key related to the UNIQUE constraint in SQL?
Signup and view all the answers
Which keyword is used to specify that a column must not allow NULL values in SQL?
Which keyword is used to specify that a column must not allow NULL values in SQL?
Signup and view all the answers
What is the purpose of the 'Check' constraint in a SQL table creation statement?
What is the purpose of the 'Check' constraint in a SQL table creation statement?
Signup and view all the answers
What defines a relation in the context of a relational database?
What defines a relation in the context of a relational database?
Signup and view all the answers
Which component of a table corresponds to the columns?
Which component of a table corresponds to the columns?
Signup and view all the answers
What is implied by a field's position in a table?
What is implied by a field's position in a table?
Signup and view all the answers
What does logical design mainly involve?
What does logical design mainly involve?
Signup and view all the answers
In a relational database, what value can a column field potentially hold aside from data types?
In a relational database, what value can a column field potentially hold aside from data types?
Signup and view all the answers
What does the term 'multi-set' imply about the rows in a relational table?
What does the term 'multi-set' imply about the rows in a relational table?
Signup and view all the answers
Which aspect of tables is not typically considered during logical design?
Which aspect of tables is not typically considered during logical design?
Signup and view all the answers
What constitutes the domains in a relational database context?
What constitutes the domains in a relational database context?
Signup and view all the answers
Which condition correctly filters customers based on their country and date of birth in a SQL SELECT statement?
Which condition correctly filters customers based on their country and date of birth in a SQL SELECT statement?
Signup and view all the answers
Which SQL clause correctly retrieves names from the games table with specific version conditions?
Which SQL clause correctly retrieves names from the games table with specific version conditions?
Signup and view all the answers
What is the main goal of the last_name LIKE 'B%' condition in the SQL SELECT statement?
What is the main goal of the last_name LIKE 'B%' condition in the SQL SELECT statement?
Signup and view all the answers
Which SQL method is most effective at handling duplicates when retrieving data from a table?
Which SQL method is most effective at handling duplicates when retrieving data from a table?
Signup and view all the answers
In data retrieval practices, which SQL feature helps to ensure optimized performance for large datasets when applying filters?
In data retrieval practices, which SQL feature helps to ensure optimized performance for large datasets when applying filters?
Signup and view all the answers
What is the outcome of selecting a subset of columns from a table without applying an ORDER BY clause?
What is the outcome of selecting a subset of columns from a table without applying an ORDER BY clause?
Signup and view all the answers
How does the ORDER BY clause behave when only some columns are specified for ordering?
How does the ORDER BY clause behave when only some columns are specified for ordering?
Signup and view all the answers
What is a recommended best practice when using the ORDER BY clause?
What is a recommended best practice when using the ORDER BY clause?
Signup and view all the answers
What may occur when a result is ordered by a column not included in the output?
What may occur when a result is ordered by a column not included in the output?
Signup and view all the answers
What is the default ordering behavior of the ORDER BY clause in SQL?
What is the default ordering behavior of the ORDER BY clause in SQL?
Signup and view all the answers
When projecting certain columns from a table, what should one be cautious of regarding duplicates?
When projecting certain columns from a table, what should one be cautious of regarding duplicates?
Signup and view all the answers
What might a lack of an ORDER BY clause result in when extracting data from a SQL table?
What might a lack of an ORDER BY clause result in when extracting data from a SQL table?
Signup and view all the answers
In SQL, which of the following is true about the use of the ORDER BY clause?
In SQL, which of the following is true about the use of the ORDER BY clause?
Signup and view all the answers
What does the SELECT clause in a SQL query primarily indicate?
What does the SELECT clause in a SQL query primarily indicate?
Signup and view all the answers
Which wildcard character is used in SQL to select all columns from a table?
Which wildcard character is used in SQL to select all columns from a table?
Signup and view all the answers
In a SQL query, how can duplicates in the results be addressed?
In a SQL query, how can duplicates in the results be addressed?
Signup and view all the answers
Which clause in a SQL query indicates which records should be returned based on specific criteria?
Which clause in a SQL query indicates which records should be returned based on specific criteria?
Signup and view all the answers
What is the primary benefit of using the SELECT * syntax in a SQL query?
What is the primary benefit of using the SELECT * syntax in a SQL query?
Signup and view all the answers
When utilizing the ORDER BY clause in a SQL query, what is primarily defined?
When utilizing the ORDER BY clause in a SQL query, what is primarily defined?
Signup and view all the answers
Which is a recommended best practice when retrieving data using a SQL query?
Which is a recommended best practice when retrieving data using a SQL query?
Signup and view all the answers
What is a potential drawback of using the asterisk (*) in a SELECT statement frequently?
What is a potential drawback of using the asterisk (*) in a SELECT statement frequently?
Signup and view all the answers
What is the purpose of using a LEFT OUTER JOIN in a SQL query?
What is the purpose of using a LEFT OUTER JOIN in a SQL query?
Signup and view all the answers
Which statement accurately describes the difference between INNER JOIN and OUTER JOIN?
Which statement accurately describes the difference between INNER JOIN and OUTER JOIN?
Signup and view all the answers
What is the impact of using the keyword ALL with the UNION operator in SQL?
What is the impact of using the keyword ALL with the UNION operator in SQL?
Signup and view all the answers
When using the GROUP BY statement in SQL, what is its primary function?
When using the GROUP BY statement in SQL, what is its primary function?
Signup and view all the answers
Which of the following is NOT a synonym for an OUTER JOIN?
Which of the following is NOT a synonym for an OUTER JOIN?
Signup and view all the answers
How does SQL handle duplicates in result sets when using the INTERSECT operator?
How does SQL handle duplicates in result sets when using the INTERSECT operator?
Signup and view all the answers
What potential issue may arise from executing a SQL query with multiple JOINs?
What potential issue may arise from executing a SQL query with multiple JOINs?
Signup and view all the answers
What is the role of aggregate functions in SQL?
What is the role of aggregate functions in SQL?
Signup and view all the answers
What is a requirement for the first_name and last_name fields in a SQL query that includes aggregation functions and GROUP BY clause?
What is a requirement for the first_name and last_name fields in a SQL query that includes aggregation functions and GROUP BY clause?
Signup and view all the answers
What does the GROUP BY clause accomplish in the provided SQL queries?
What does the GROUP BY clause accomplish in the provided SQL queries?
Signup and view all the answers
Which of the following statements is true about the ORDER BY clause in SQL?
Which of the following statements is true about the ORDER BY clause in SQL?
Signup and view all the answers
What is the main purpose of the HAVING clause in SQL?
What is the main purpose of the HAVING clause in SQL?
Signup and view all the answers
In an SQL query using the EXTRACT function, what is the purpose of using 'AS regyear' in the SELECT statement?
In an SQL query using the EXTRACT function, what is the purpose of using 'AS regyear' in the SELECT statement?
Signup and view all the answers
Why might the GROUP BY clause produce different results in PostgreSQL compared to SQLite?
Why might the GROUP BY clause produce different results in PostgreSQL compared to SQLite?
Signup and view all the answers
In which scenario is the HAVING clause necessary instead of the WHERE clause?
In which scenario is the HAVING clause necessary instead of the WHERE clause?
Signup and view all the answers
What does the use of aggregate functions in SQL allow for?
What does the use of aggregate functions in SQL allow for?
Signup and view all the answers
What does the GROUP BY statement do in an SQL query?
What does the GROUP BY statement do in an SQL query?
Signup and view all the answers
When renaming columns in an aggregate query with GROUP BY, what is the effect on the GROUP BY clause?
When renaming columns in an aggregate query with GROUP BY, what is the effect on the GROUP BY clause?
Signup and view all the answers
Which of the following statements correctly uses the HAVING clause to filter results?
Which of the following statements correctly uses the HAVING clause to filter results?
Signup and view all the answers
What is an important consideration when constructing a SQL query with both JOINs and GROUP BY?
What is an important consideration when constructing a SQL query with both JOINs and GROUP BY?
Signup and view all the answers
What type of aggregate functions can be used in the HAVING clause?
What type of aggregate functions can be used in the HAVING clause?
Signup and view all the answers
Which SQL join type is used to combine rows from two or more tables based on a related column?
Which SQL join type is used to combine rows from two or more tables based on a related column?
Signup and view all the answers
When optimizing an SQL query, which strategy can improve performance when dealing with large datasets?
When optimizing an SQL query, which strategy can improve performance when dealing with large datasets?
Signup and view all the answers
What is the primary benefit of using the HAVING clause after GROUP BY in an SQL query?
What is the primary benefit of using the HAVING clause after GROUP BY in an SQL query?
Signup and view all the answers
What is the primary purpose of the primary key in a database table?
What is the primary purpose of the primary key in a database table?
Signup and view all the answers
How are relationship sets typically mapped in a relational database schema?
How are relationship sets typically mapped in a relational database schema?
Signup and view all the answers
In the provided CREATE TABLE statement for the person table, what combination constitutes the primary key?
In the provided CREATE TABLE statement for the person table, what combination constitutes the primary key?
Signup and view all the answers
Which statement accurately describes the attributes of a relation in schema translation?
Which statement accurately describes the attributes of a relation in schema translation?
Signup and view all the answers
What is a weak entity in the context of relational databases?
What is a weak entity in the context of relational databases?
Signup and view all the answers
What happens to attributes when an entity set is mapped to a relation in schema translation?
What happens to attributes when an entity set is mapped to a relation in schema translation?
Signup and view all the answers
Which of the following is a correct statement about foreign key constraints?
Which of the following is a correct statement about foreign key constraints?
Signup and view all the answers
In SQL, which keyword is used to specify that a column must allow NULL values?
In SQL, which keyword is used to specify that a column must allow NULL values?
Signup and view all the answers
Which statement correctly defines a one-to-many relationship in database schema design?
Which statement correctly defines a one-to-many relationship in database schema design?
Signup and view all the answers
What is the correct SQL statement to define a weak entity in a CREATE TABLE command?
What is the correct SQL statement to define a weak entity in a CREATE TABLE command?
Signup and view all the answers
In the CREATE TABLE statement for the 'contract' table, what is the role of the PRIMARY KEY constraint?
In the CREATE TABLE statement for the 'contract' table, what is the role of the PRIMARY KEY constraint?
Signup and view all the answers
When creating foreign key constraints in SQL, what does the phrase 'REFERENCES company(name)' specify?
When creating foreign key constraints in SQL, what does the phrase 'REFERENCES company(name)' specify?
Signup and view all the answers
What is the significance of specifying NOT NULL in the 'CREATE TABLE' statement?
What is the significance of specifying NOT NULL in the 'CREATE TABLE' statement?
Signup and view all the answers
Which of the following is NOT a characteristic of a weak entity?
Which of the following is NOT a characteristic of a weak entity?
Signup and view all the answers
What is an incorrect way to define a relationship in the context of schema translation?
What is an incorrect way to define a relationship in the context of schema translation?
Signup and view all the answers
In the provided table definitions, which of the following columns is set as the primary key in the 'work_for' table?
In the provided table definitions, which of the following columns is set as the primary key in the 'work_for' table?
Signup and view all the answers
In the context of schema translation, what is a defining characteristic of a weak entity?
In the context of schema translation, what is a defining characteristic of a weak entity?
Signup and view all the answers
What is the correct SQL syntax to create a table that properly includes a foreign key constraint referencing another table?
What is the correct SQL syntax to create a table that properly includes a foreign key constraint referencing another table?
Signup and view all the answers
Which statement accurately describes One-to-Many relationships in the context of database design?
Which statement accurately describes One-to-Many relationships in the context of database design?
Signup and view all the answers
Which of the following SQL statements correctly establishes a foreign key relationship in the given CREATE TABLE examples?
Which of the following SQL statements correctly establishes a foreign key relationship in the given CREATE TABLE examples?
Signup and view all the answers
What characteristic is common to the foreign key constraints in the provided SQL examples?
What characteristic is common to the foreign key constraints in the provided SQL examples?
Signup and view all the answers
What is a requirement for a scalar subquery used in the SELECT clause?
What is a requirement for a scalar subquery used in the SELECT clause?
Signup and view all the answers
Which statement best describes the use of subqueries in SQL?
Which statement best describes the use of subqueries in SQL?
Signup and view all the answers
What should you consider for readability and maintainability when constructing SQL queries?
What should you consider for readability and maintainability when constructing SQL queries?
Signup and view all the answers
In the context of nested queries, what role does the IN clause serve?
In the context of nested queries, what role does the IN clause serve?
Signup and view all the answers
What is a common issue with deeply nested SQL queries?
What is a common issue with deeply nested SQL queries?
Signup and view all the answers
How can common table expressions (CTEs) improve the readability of SQL queries?
How can common table expressions (CTEs) improve the readability of SQL queries?
Signup and view all the answers
Which of the following is NOT a recommended practice when using nested queries?
Which of the following is NOT a recommended practice when using nested queries?
Signup and view all the answers
What is a potential downside of using nested queries in SQL?
What is a potential downside of using nested queries in SQL?
Signup and view all the answers
What does a common table expression (CTE) allow you to do in SQL?
What does a common table expression (CTE) allow you to do in SQL?
Signup and view all the answers
Which of the following optimally improves SQL query readability?
Which of the following optimally improves SQL query readability?
Signup and view all the answers
How can the use of views benefit data retrieval in SQL?
How can the use of views benefit data retrieval in SQL?
Signup and view all the answers
What drawback is associated with unmaterialized views in SQL?
What drawback is associated with unmaterialized views in SQL?
Signup and view all the answers
What benefit does rewriting complex queries into simpler queries provide?
What benefit does rewriting complex queries into simpler queries provide?
Signup and view all the answers
What is a common practice when using subqueries in SQL?
What is a common practice when using subqueries in SQL?
Signup and view all the answers
Which of the following best describes how a subquery functions within an SQL statement?
Which of the following best describes how a subquery functions within an SQL statement?
Signup and view all the answers
What is the purpose of using the 'AS' operator when defining a subquery in the FROM clause?
What is the purpose of using the 'AS' operator when defining a subquery in the FROM clause?
Signup and view all the answers
What does the keyword EXISTS evaluate in a subquery context?
What does the keyword EXISTS evaluate in a subquery context?
Signup and view all the answers
Which of the following statements accurately describes a correlated subquery?
Which of the following statements accurately describes a correlated subquery?
Signup and view all the answers
Which SQL construct could improve clarity and understanding in complex queries?
Which SQL construct could improve clarity and understanding in complex queries?
Signup and view all the answers
What limitation is common with nested subqueries regarding optimization?
What limitation is common with nested subqueries regarding optimization?
Signup and view all the answers
When using subqueries, which keyword is crucial for checking both empty and non-empty results?
When using subqueries, which keyword is crucial for checking both empty and non-empty results?
Signup and view all the answers
Which scenario is more appropriate for using nested queries?
Which scenario is more appropriate for using nested queries?
Signup and view all the answers
In SQL, which clause normally follows a SELECT statement in a nested query to limit results?
In SQL, which clause normally follows a SELECT statement in a nested query to limit results?
Signup and view all the answers
What is a potential downside of using nested queries compared to JOINs?
What is a potential downside of using nested queries compared to JOINs?
Signup and view all the answers
Study Notes
Database Design
- Database logical design involves determining the schema for a database application, including the number of tables, table names, number of columns per table, column names, and data types.
- Relational database management systems organize data in tables.
- Tables are multi-sets of rows or records, not lists.
- Rows or records contain fields that correspond to the table's columns.
- Columns or fields have a name and an implicit position determined by their order in creation statements.
- Columns or fields have a domain, which is a type with the possibility of a NULL value.
- Logical design involves selecting an appropriate schema for the database application.
Constraints
- A consistent state of the database adheres to the business rules defined by structural and integrity constraints in the schema.
- SQL supports five types of integrity constraints: NOT NULL, PRIMARY KEY, UNIQUE, FOREIGN KEY, and CHECK.
- A primary key is a set of columns that uniquely identifies a record in a table.
- Each table can contain at most one primary key.
- An instance of a table cannot have two records with the same value or combination of values in the primary key columns.
- Primary key columns cannot contain null values.
Update and Delete Actions
- For more powerful generalization, triggers can be used.
-
ON UPDATE CASCADE
can cascade the update or deletion to other tables using foreign key relationships. - Other options include
NO ACTION
,SET DEFAULT
, andSET NULL
, but these may cause chain reactions. - It is generally considered good practice to constrain all attributes to be not NULL unless there is a compelling design or tuning reason for not doing so.
- It is generally considered good practice to defer all constraints that can be deferred.
-
DELETE FROM
deletes the content of a table without altering its definition. -
DROP TABLE
deletes both the content and definition of a table. -
DELETE
is used to delete the content of a table, whileDROP
is used to delete both the content and definition of a table.
Foreign Keys
-
ON DELETE CASCADE
propagates the update or deletion to other tables. - A unique constraint on a column or combination of columns ensures that a table cannot contain two records with the same value in the corresponding column or combination of columns.
- According to SQL standards, PRIMARY KEY is equivalent to UNIQUE and NOT NULL, but there are differences in side-effects such as foreign-key, index, deferrability, etc.
Transactions
- Transactions in SQL or application code can be specified using blocks with keywords such as BEGIN, END, COMMIT, ABORT, and ROLLBACK.
- Integrity constraints are usually immediate (checked after each operation) but should ideally be deferred to be checked after the end of transactions.
- Most systems, like PostgreSQL, only allow certain constraints to be deferred.
Single Table Operations
-
Basic Operations
- A simple SQL query includes SELECT, FROM, and WHERE clauses.
- SELECT clause indicates the columns to be output.
- FROM clause indicates the tables to be queried.
- WHERE clause indicates the conditions on the records.
- The asterisk (*) is a shorthand for selecting all columns.
Duplicate Ordering
- Ordering columns may result in duplicate rows.
- Columns can be ordered in ascending (ASC) or descending (DESC) order.
- Default order is ASC.
- If ordering on all columns, the result is a distinct ordering
- If only ordering on a subset of columns, the unmentioned columns are in no particular order.
Boolean Operations
- De Morgan's Law: ¬(A ∧ B) ≡ ¬A ∨ ¬B and ¬(A ∨ B) ≡ ¬A ∧ ¬B
- The IN operator can be used to check if a value is within a set of values.
- NOT operator negates a condition.
- BETWEEN operator checks if the value is within a range.
Joins
- Outer Join
- Conditions in the ON clause are not equivalent to conditions in the WHERE clause.
- Conditions in the ON clause determine which rows are dangling.
- LEFT JOIN is a synonym for LEFT OUTER JOIN
- RIGHT JOIN is a synonym for RIGHT OUTER JOIN
- FULL JOIN is a synonym for FULL OUTER JOIN
Set Operators
- UNION, INTERSECT, and EXCEPT return the union, intersection, and non-symmetric difference of the results of two queries, respectively.
- Set operators eliminate duplicates unless annotated with the keyword ALL.
Aggregation
- Aggregate functions can be used in conditions, but not in WHERE clause.
- Aggregate functions are evaluated after groups are formed.
- HAVING clause is performed after GROUP BY clause.
- HAVING clause can only use aggregate functions, columns listed in the GROUP BY clause, and subqueries.
- The order of columns in GROUP BY clause does not change the meaning of the query.
- The logical groups remain the same.
Aggregation Functions
- The following query does not work in PostgreSQL (but works in SQLite with potentially incorrect result).
-
SELECT c.customerid, c.first_name, c.last_name, SUM(g.price) FROM customers c, downloads d, games g WHERE c.customerid = d.customerid AND d.name = g.name and d.version = g.version GROUP BY c.first_name, c.last_name;
-
- The above query works only because first_name and last_name are guaranteed unique.
- Do NOT write such queries for readability and portability.
Grouping
- Renamed columns can be used in GROUP BY clause.
- The following query displays the number of downloads by country and year of birth (using EXTRACT).
-
SELECT c.country, EXTRACT(YEAR FROM c.since) AS regyear, COUNT(*) AS total FROM customers c, downloads d WHERE c.customerid = d.customerid GROUP BY c.country, regyear ORDER BY regyear ASC, c.country ASC;
-
Grouping
- The order of columns in GROUP BY clause does not change the meaning of the query.
- The logical groups remain the same.
-
SELECT c.country, EXTRACT(YEAR FROM c.since) AS regyear, COUNT(*) AS total FROM customers c, downloads d WHERE c.customerid = d.customerid GROUP BY regyear, c.country ORDER BY regyear ASC, c.country ASC;
-
Having
-
SELECT c.country FROM customers c WHERE COUNT(*) >= 100 GROUP BY c.country;
- The query on the left finds the countries in which there are more than 100 customers.
Having
- The following query on the left finds the countries in which there are more than 100 customers.
-
SELECT c.country FROM customers c GROUP BY c.country HAVING COUNT(*) >= 100;
-
Creating Database Tables
- Entities are mapped to relations.
- Entity set attributes are mapped to attributes of the relation.
- Keys are mapped to the primary key when creating tables.
- Relationships are mapped to relations.
- The attributes of the relation consist of the attributes of the relationship set.
- The keys of the participating entities are also included.
Exceptions to Rules
- One-to-Many: One-to-many relationship sets, without a weak entity, can be represented as separate tables containing the participating entity keys.
- Weak Entity: Weak entities are not represented with a primary key in the relationship table.
- Entities in a one-to-many relationship with a weak entity can be represented with a single table containing the weak entity key as the primary key.
- The primary key of a weak entity must be foreign key of the strong entity in the relationship.
- The table represents the relationship, not the weak entity.
- When representing one-to-many relationships with a weak entity and a non-weak entity, the table should include the keys for both entities.
- The key in the table should be the combined key of the participating entities.
- When representing one-to-many relationships with a weak entity and a non-weak entity, the primary key of the table is not the key of the weak entity.
- The table should include the weak entity’s key.
- When representing a relationship between a weak entity and its strong entity, the non-weak entity must be represented in a different table. This table's primary key should be a foreign key in the relationship table.
Schema Translation Tables
-
company
table:-
name
: VARCHAR(64), primary key -
address
: VARCHAR(128)
-
-
person
table:-
first_name
: VARCHAR(32) -
last_name
: VARCHAR(32), primary key -
address
: VARCHAR(128) NOT NULL, primary key
-
-
contract
table:-
start
: DATE NOT NULL, primary key -
end
: DATE NOT NULL, primary key -
first_name
: VARCHAR(32), primary key -
last_name
: VARCHAR(32), primary key -
name
: VARCHAR(64), primary key - Foreign key
(first_name, last_name)
referencesperson
table - Foreign key
name
referencescompany
table.
-
-
work_for
table:-
start
: DATE, NOT NULL, primary key -
end
: DATE, NOT NULL, primary key -
enumber
: CHAR(8) -
cname
: VARCHAR(32) - Foreign key
enumber
referencesemployee
table. - Foreign key
cname
referencescompany
table.
-
-
employee_work_for
table:-
start
: DATE, NOT NULL, primary key -
end
: DATE, NOT NULL, primary key -
enumber
: CHAR(8), primary key -
ename
: CHAR(32), NOT NULL -
cname
: VARCHAR(32), NOT NULL - Foreign key
cname
referencescompany
table.
-
### Splitting Query
- Subqueries can be used in the
SELECT
,FROM
, andWHERE
clauses of a query. - Subqueries in the
SELECT
clause must return a single column and a single row. - Subqueries in the
FROM
clause can be used to create a temporary table for the query. - Subqueries in the
WHERE
clause can be used to filter the results of a query. - Common Table Expressions (CTEs) are temporary tables that only exist for the duration of a query.
- Views are pre-compiled queries that can be used like tables.
- Subqueries can be nested, meaning a subquery can be used within another subquery.
Nesting Query
- Subqueries can be used in the
WHERE
clause to filter the results of a query, using theIN
,ANY
,ALL
, andEXISTS
operators. -
IN
operator evaluates to true if the value in the outer query is found in the subquery. -
ANY
operator evaluates to true if at least one value in the subquery matches the value in the outer query. -
ALL
operator evaluates to true if all the values in the subquery match the value in the outer query. -
EXISTS
operator evaluates to true if the subquery returns at least one row. - Subqueries can be correlated, meaning that the subquery relates to the outer query, often through a common column.
- Correlated subqueries are executed for each row of the outer query.
- Subqueries can be used in the
HAVING
clause to filter the results of a grouped query.
### Query Performance
- Queries with nested subqueries can be harder to read and understand than simpler queries.
- Nested subqueries can be inefficient if the database needs to execute multiple queries to calculate the results.
- It is often possible to rewrite nested subqueries as simpler queries that are easier to read and more efficient.
- Consider alternative methods to achieve the same results, such as joins or CTEs.
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.
Description
This quiz covers essential concepts in database logical design, including schema creation, table structures, and integrity constraints. You'll explore key elements like primary keys and data types, vital for relational database management systems.