How do the relations (tables) in SQL differ from the relations defined formally in Chapter 3?
[How do the relations (tables) in SQL differ from the relations defined formally in Chapter 3?] Discuss other differences in terminology
Formal Relational Model || SQL relation | table attribute | column tuple | row N/A | schema N/A | catalog
SELECT (relational algebra) != SELECT (SQL)
[How do the relations (tables) in SQL differ from the relations defined formally in Chapter 3?] Why does SQL allow duplicate tuples in a table or in a query result?
in SQL a relation is a multiset (AKA bag) of tuples, whereas in formal relational model a relation is a set of tuples
SQL allows for duplicate tuples in the result because sometimes it isn’t practical for all results to be unique, and sometimes you want all results including the duplicates
SQL treats tables not as a set but rather as a multiset; duplicate tuples can appear more than once in a table and in the result of a query.
SQL does not automatically eliminate duplicate tuples in the results of queries for the following reasons:
* Duplicate elimination is an expensive operation. One way to implement it is to sort the tuples first and then eliminate duplicates.
* The user may want to see duplicate tuples in the result of a query.
* When an aggregate function is applied to tuples, in most cases we do not want to eliminate duplicates.
Explain the character-string data types and bit-string data types.
character-string:
bit-string:
How does SQL allow implementation of the entity integrity and referential integrity constraints described in Chapter 3?
in SQL you can specify entity integrity constraints and referential integrity constraints during the CREATE TABLE statement or later with an ALTER TABLE statement
[entity integrity constraint: no PK value can be NULL]
[referential integrity constraint: a tuple in one relation that refers to another relation must refer to a tuple that exists in the other relation]
ex: CREATE TABLE Department (num INT NOT NULL, name VARCHAR(20) NOT NULL, PRIMARY KEY(num) );
CREATE TABLE User (email VARCHAR(80) NOT NULL, last_name VARCHAR(50) NOT NULL, first_name VARCHAR(50) NOT NULL, middle_name VARCHAR(50), department INT NOT NULL DEFAULT 99999, PRIMARY KEY(email), FOREIGN KEY(department) REFERENCES Department(num) ON DELETE SET DEFAULT ON UPDATE CASCADE );
[How does SQL allow implementation of the entity integrity and referential integrity constraints described in Chapter 3?] What about referential triggered actions?
a referential triggered action is the action you can specify when specifying a foreign key constraint for what action to take for an integrity violation
options:
qualifying actions:
Give reasons why SQL does not automatically eliminate duplicate tuples in the results of queries.
SQL treats tables not as a set but rather as a multiset; duplicate tuples can appear more than once in a table and in the result of a query.
SQL does not automatically eliminate duplicate tuples in the results of queries for the following reasons:
* Duplicate elimination is an expensive operation. One way to implement it is to sort the tuples first and then eliminate duplicates.
* The user may want to see duplicate tuples in the result of a query.
* When an aggregate function is applied to tuples, in most cases we do not want to eliminate duplicates.
[Give reasons why SQL does not automatically eliminate duplicate tuples in the results of queries.] Which keyword is used to eliminate duplicate tuples from the result? Give an example.
DISTINCT
ex table: Player last_name, first_name, position, number Foles, Nick, QB, 9 Wentz, Carson, QB, 11 Sproles, Darren, RB, 43 Smallwood, Wendell, RB, 28 Agholar, Nelson, WR, 13 Jeffrey, Alshon, WR, 17 Ertz, Zach, TE, 86 Elliot, Jake, K, 4
SELECT COUNT(position) FROM Player; 8
SELECT COUNT(DISTINCT position) FROM Player; 5