Relational Databases (Ch. 4, 5, & 7)

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/82

flashcard set

Earn XP

Description and Tags

Chapter 4 & 5 & 7

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

83 Terms

1
New cards

no - can be broken down into two attributes firstname and lastname

Given the value “Jeremy Brown” is this value atomic? Why?

2
New cards

R1 = (orderID, supplierID, productName, dateOrdered)

R2 = (supplierID, supplierName, city, state)

R3 = (city, state, zip)

Given the relation R and the functional dependencies below, decompose R to be in 3NF. State all foreign and primary keys.

R = (order_id, supplier_id, productName, supplierName, dateOrdered, city, state, zip)

supplierID → {supplierName, city, state, zip}

{city, state} → zip

3
New cards

relation

In a relational database, a table is also called a

4
New cards

1NF

What normal form ensures all attributes are atomic (no multi-valued attributes)?

5
New cards

3NF

What normal form removes transitive dependencies?

6
New cards

foreign, candidate, primary, super

Name the four types of keys in a relational database?

7
New cards

no

Is a super key a minimal candidate key?

8
New cards

Boyce-Codd Normal Form

What does BCNF stand for?

9
New cards

domain

What specifies the allowed values for an attribute type?

10
New cards

2NF

What normal form removes partial key dependencies?

11
New cards

a. selection

Which of these is not a type of anomaly we try to prevent with normalizing?

a. selection

b. insertion

c. modification

d. deletion

12
New cards

CREATE TABLE car (

vin CHAR(20) PRIMARY KEY,

model VARCHAR(10) NOT NULL,

constraint c1 FOREIGN KEY(model) REFERENCES model(id),

cost FLOAT(5, 2),

code INT NOT NULL)

Write a SQL statement to make a table called car based on the following:

  • a string of length 20 vin that can be used to uniquely identify a car

  • a string of up to length 10 called model. This is required. This is a reference to the model table’s id

  • a number that can be of length 5 and have 2 decimal places called cost

  • an integer called code. This is required

13
New cards

SELECT * FROM book WHERE name > “Databases”

Write a SQL query to get all rows from a book table such that you want all books with a name greater than “Databases” where name is an attribute of the book table.

14
New cards

…ORDER BY level DESC, hit_points DESC

Complete the query below to return all heroes sorted first by level in descending order, and then by hit points in descending order.

SELECT name, hit_points, level FROM hero …

15
New cards

SELECT COUNT(*)…

Complete the query below to return the total number of heroes stored in the table.

…FROM hero

16
New cards

distinct

What keyword do you use in a select to ensure you won’t get duplicate values in a select query?

17
New cards

not null

What constraint do you use in a create table to ensure an attribute must have a value?

18
New cards

0 to 20

An attribute of type varchar(20) means that the attribute can contain how many characters?

19
New cards

no

Does a when clause in SQL restrict the number of rows returned?

20
New cards

unordered

Are SQL results ordered or unordered?

21
New cards

all rows of foo

What will this return:

SELECT * FROM foo

22
New cards

DROM TABLE bar

Write a SQL statement to remove the table bar from the database?

23
New cards

the value of each attribute type A must be an atomic and single value from the domain dom(A)

What does the domain constraint state?

24
New cards

a subset of attribute types of a relation R with the property that no two tuples in any relation state should have the combination of values for these attribute types

What is a superkey?

25
New cards

attribute types that make up the primary key should always satisfy a NOT NULL constraint

What is the entity integrity constraint?

26
New cards

a foreign key F has the same domain as the primary key P attribute type(s) it refers to and either occurs as a value of P or as null

What is the referential integrity constraint?

27
New cards

when you don’t update everything

What is an update anomaly?

28
New cards

the value of X uniquely determines the value of Y

What does the functional dependency X → Y imply

29
New cards

an attribute type that is part of a candidate key

What is a prime attribute type?

30
New cards

R1(SSN, ename, dnumber, dname)

R2(SSN, pnumber, pname, hours)

Put the following in 1NF form:

R(SSN, ename, dnumber, dname, project(pnumber, pname, hours))

31
New cards

partial

if an attribute type can be removed from a functional dependency X → Y and the dependency still holds, is it full or partial?

32
New cards

R1(SSN, pnumber, hours)

R2(pnumber, pname)

Put the following into 2NF:

R(SSN, pnumber, pname, hours)

Assumption: many people can work on the same project and a project has a unique name

33
New cards

transitive

What type of dependency X → Y in a relation R has a set of attribute types Z that is neither a candidate key nor a subset of any key of R and both X → Z and Z → Y hold

34
New cards

R1(SSN, ename, dnumber)

R2(dnumber, dname, dmgrssn)

Put the following in 3NF:

R(SSN, ename, dnumber, dname, dmgrssn)

Assumption: an employee works in one department, a department can have many workers, and a department has one manager

35
New cards

BCNF

Which normal form guarantees that for every non-trivial functional dependency X → Y, X is either a candidate key or a superset thereof

36
New cards

trivial

In which type of functional dependency X → Y is Y a subset of X?

37
New cards

R1(supnr, prodnr, quantity)

R2(supnr, supname)

Put the following in BCNF:

R(supnr, supname, prodnr, quantity)

Assumption: a supplier can supply multiple products, a product can be supplied by multiple suppliers, a supplier has a unique name

38
New cards

4NF

Which normal form ensures that for each non-trivial multivalued dependencies X → Y, X is a superkey - that is X is either a candidate key or a superset thereof

39
New cards

multivalued

What type of dependency X → Y has every X value exactly determining a set of Y values, independently of the other attribute types

40
New cards

R1(course, textbook)

R2(course, instructor)

Put the following in 4NF:

R(course, instructor, textbook)

Assumption: a course can be taught by different instructors, and a course uses the same set of textbooks for each instructor

41
New cards

a foreign key (which is the primary key of the other entity) should be added to the weak entity

What is required when mapping a weak entity to another entity through a weak relationship?

42
New cards

Style I:

ARTIST(anr, aname)

SINGER(anr, musicStyle)

ACTOR(anr, movie)

Style II:

SINGER(anr, aname, musicStyle)

ACTOR(anr, aname, movie)

Style III:

ARTIST(anr, aname, musicStyle, movie)

Map the image to a relational model. ARTIST has two attributes: ANR (primary key) and aname. SINGER has one attribute called musicStyle. ACTOR has one attribute called movie

<p>Map the image to a relational model. ARTIST has two attributes: ANR (primary key) and aname. SINGER has one attribute called musicStyle. ACTOR has one attribute called movie</p>
43
New cards

PERSON(pnr, pname, custNumber)

COMPANY(cnr, cname, custNumber)

ACCOUNT_HOLDER(custNumber)

Map the image to a relational model.

<p>Map the image to a relational model.</p>
44
New cards

CONSULTANT(cnr)

PROJECT(pnr)

PARTICIPATION(cnr, pnr, contnr, date)

CONTRACT(contnr)

Map the image to a relational model.

<p>Map the image to a relational model.</p>
45
New cards

CREATE TABLE supplier

(supnr CHAR(4) NOT NULL PRIMARY KEY,

supname VARCHAR(40) NOT NULL,

supaddress VARCHAR(50),

supcity VARCHAR(20),

supstatus SMALLINT)

Convert the following relational model to a SQL table:

SUPPLIER(supnr, supname, supaddress, supcity, supstatus)

46
New cards

CREATE TABLE po_line

(ponr CHAR(7) NOT NULL,

prodnr CHAR(6) NOT NULL,

quantity INT,

PRIMARY KEY (ponr, prodnr),

FOREIGN KEY (ponr) REFERENCES purchase_order (ponr),

FOREIGN KEY (prodnr) REFERENCES product (prodnr))

Convert the following relational model to a SQL table:

PO_LINE(ponr, prodnr, quantity)

Nore: ponr is the primary key of the purchase_order table

Note: prodnr is the primary key of the product table

47
New cards

DROP TABLE tablename CASCADE/RESTRICT

What command is used to delete a schema or table? What two commands can it combine with?

48
New cards

ALTER TABLE tablename

What command is used to modify a schema or table?

49
New cards

SELECT prodName FROM products WHERE prodName LIKE ‘_COW%‘

Write a SQL statement that takes a products name from the product table. Only return product names that begin with any letter followed by “COW” and then ending in any letters

50
New cards

SELECT COUNT (DISTINCT purchase_price) FROM supplies WHERE prodnr = ‘0178’

Write a SQL query that counts all unique purchase_price’s in the supplies table. The prodnr should be ‘0178’.

51
New cards

SELECT prodnr FROM po_line GROUP BY prodnr HAVING COUNT(*) >= 2

Write a SQL query that returns the prodnr from the po_line table where the number of entries in po_line that have that prodnr is at least 2. Group the results by prodnr.

52
New cards

SELECT r.supname FROM supplier r, supplies s WHERE r.supnr = s.supnr AND s.prodnr = ‘0899’

Write a query that gets the supname from the supplier table where that suppliers supnr = supnr of the supplies table and where the suppplies table prodnr = ‘0899’

53
New cards

SELECT supname FROM supplier WHERE supnr = (SELECT supnr FROM purchase_order WHERE ponr = ‘1560’)

Write a nested query that finds the supname of a supplier whose supnr is equal to the supnr of a purchase_order that has a ponr of 1560

54
New cards

a condition in the WHERE clause of the nested query references some column of a table declared in the outer query

What does it mean for two queries to be correlated?

55
New cards

SELECT p.prodnr FROM product p WHERE 1 < (SELECT COUNT(*) FROM po_line pol WHERE p.prodnr = pol.prodnr)

Write a query to give me the count from po_line where p.prodnr = pol.prodnr…if that count > 1, output prodnr. do this for each product p

56
New cards

EXISTS - checks that any instance occurs

ALL - checks that the condition satisfies every entry in table

ANY - checks that the condition satisfies any entry in table

What do EXISTS, ANY, and ALL do in SQL?

57
New cards

union, intersect, except

What are the three set operations you can perform on queries?

58
New cards

INSERT INTO product VALUES (attr1, attr2, etc.)

Write a SQL statement that adds a row to the product table

59
New cards

INSERT INTO product(name, price, qty) VALUES (‘yoyo’, 5.00, 400), (‘slinky’, 3.50, 200)

Write a SQL statement that adds 2 rows to the product table. The data entered should be a name, price, and qty in that order.

60
New cards

DELETE FROM product WHERE prodnr = ‘1000’

Write a SQL statement that deletes all entries of the product table where prodnr = ‘1000;

61
New cards

UPDATE product SET qty = 26 WHERE prodnr = ‘0185’

Write a SQL statement that updates a product whose prodnr = ‘0185’ and sets its qty to 26.

62
New cards

ALTER TABLE supplier ADD color VARCHAR(10) DEFAULT silver

Write a SQL query that adds a column to the supplier table called color. Set the default value to silver.

63
New cards

CREATE VIEW topsuppliers AS SELECT supnr, supname FROM supplier WHERE supstatus > 50

Write a SQL statement that creates a view called topsuppliers. The values should be supnr and supname from supplier table (only where supstatus > 50)

64
New cards

GRANT select, insert, update, delete ON supplier TO bbaesens

REVOKE to remove

Write a SQL statement that grants selection, insertion, update abilities, and deletion on the supplier table to the user bbaesens.

How would you take away this ability from bbaesens?

65
New cards

CREATE TRIGGER foo AFTER UPDATE OF dog ON (age)

REFERENCING NEW row AS nrow FOR EACH row WHEN nrow.age == 0 BEGIN ATOMIC UPDATE nrow.age = null END

Write a row level trigger on the age attribute of dog to set the age to null when someone sets the age to 0.

66
New cards

row and statement

Triggers can be performed on a database in multiple ways, for each ___ and for each ___

67
New cards

yes

Can a stored procedure have both IN and OUT variables?

68
New cards

no

Are both relational algebra and SQL pure languages?

69
New cards

no

Can triggers cause deadlocks?

70
New cards

trigger

What is the SQL code that is run when a specific event, such as inserting happens?

71
New cards

procedure

What can be used as a container for several SQL instructions

72
New cards

INSERT INTO Software (Name, OS, Year_Created, Month_Created) VALUES (‘Steam’, ‘Windows’, 2015, ‘March’)

Write SQL statements to insert a new product called Steam into the Software table. Steam was made in March of 2015, and works on Windows and MacOS.

<p>Write SQL statements to insert a new product called Steam into the Software table. Steam was made in March of 2015, and works on Windows and MacOS.</p>
73
New cards

UPDATE Software SET OS = ‘OS X’ WHERE name = ‘Steam’

Write SQL statement(s) to change the OS from MacOS to OS X.

<p>Write SQL statement(s) to change the OS from MacOS to OS X.</p>
74
New cards

SELECT name FROM Software WHERE year_created > 2002

Write SQL statement(s) to find all the Software created after 2002.

<p>Write SQL statement(s) to find all the Software created after 2002.</p>
75
New cards

DELETE FROM Software WHERE os LIKE ‘%Linux%’

Write SQL statement(s) to remove all the software that works on Linux.

<p>Write SQL statement(s) to remove all the software that works on Linux.</p>
76
New cards

SELECT name

FROM Software

WHERE os =

( SELECT COUNT(os)

FROM Software

GROUP BY os)

Write SQL statement(s) to count all the software items that work on each OS. i.e. (Windows: 5. Mac: 2. etc).

<p>Write SQL statement(s) to count all the software items that work on each OS. i.e. (Windows: 5. Mac: 2. etc).</p>
77
New cards

yes

Does the relational model require all relations to be normalized?

78
New cards

tuple and set constructor

What are the 2 types of constructors for the relational model?

79
New cards

atomic

What type of data can tuple constructors be used on?

80
New cards

A piece of SQL code consisting of declarative and/or procedural instructions and stored in the catalog of the RDBMS. A trigger occurs any time an event occurs.

Define a trigger.

81
New cards

CREATE TRIGGER salary_total

AFTER INSERT ON employee

FOR EACH ROW

WHEN (new.dnr IS NOT NULL)

UPDATE department

SET total_salary = total_salary + new.salary

WHERE dnr = new.dnr

Create a trigger called SALARYP_TOTAL that does the following after you insert a new entry into the EMPLOYEE table:

when DNR equals NEW.DNR and NEW.DNR is not null, update the DEPARTMENT to be equal to TOTAL_SALARY + NEW.SALARY in each row.

82
New cards
  • deadlocks

  • debugging complexities

  • performance problems

  • infinite loop

What are the disadvantages of using a trigger?

83
New cards

A piece of SQL code consisting of declarative and/or procedural instructions and stored in the catalog of the RDBMS.

What is a stored procedure?