Bài tập ôn tập chương 2 môn Cơ sở dữ liệu có lời giải | Đại học Sư phạm Kỹ thuật Thành phố Hồ Chí Minh

Bài tập ôn tập chương 2 môn Cơ sở dữ liệu có lời giải của Đại học Sư phạm Kỹ thuật Thành phố Hồ Chí Minh với những kiến thức và thông tin bổ ích giúp sinh viên tham khảo, ôn luyện và phục vụ nhu cầu học tập của mình cụ thể là có định hướng ôn tập, nắm vững kiến thức môn học và làm bài tốt trong những bài kiểm tra, bài tiểu luận, bài tập kết thúc học phần, từ đó học tập tốt và có kết quả cao cũng như có thể vận dụng tốt những kiến thức mình đã học vào thực tiễn cuộc sống. Mời bạn đọc đón xem!

 

lOMoARcPSD|36625228
ASSIGNMENT 02
ID: 22110032
Name: Lê Gia Huy
------------------------------------------------------------------------------------------------
I. Chapter 2 review questions:
1. What is a relation:
A relation is a table containing data consisting of many columns and many rows, the
data in a relation must satisfy the constraints of that relation
2. What is each column in a relation also called:
Each column is also called an attribute/field of a relation
3. What is each row in a relation also called:
Each row is also called a tuple/record of a relation
4. What is a integrity constraint:
Integrity constraint is a set of rules that all data in a database must satisfy to ensure
that the database is always correct
5. Why do relations need to have integrity constraints:
To ensure:
- The cohesion of the components that make up the database
- Data consistency
- The database always represents the actual semantics
6. What is a domain of an attribute:
Domain is a set of values that an attribute can take
7. What is a relation schema:
Relation schema is a set of all attributes in the relation
8. Which one of these is a relation schema:
a. Q(A, B, C)
b. Q A, B, C
c. Q{A, B, C}
d. All 3 answers above are correct
Answer: a. Q(A, B, C)
9. What is an instance of a relation:
lOMoARcPSD|36625228
Instance of a relation is a set of all tuples of that relation at a specific time
10. What is a relational database:
Relational database is a set of relations that are related to each other about a specific
field
11. What is a relational database schema:
Relational database schema is a set of relation schemas that are related to each other
about a specific field
12. What is a super key of a relation schema Q(R):
K is a superkey of R if values for K are sufficient to identify a unique tuple of each
possible relation
13. Is it correct that there is only 1 super key in a relation: Incorrect
14. Is it correct that a super key in a relation can contains more attributes than those
in that relation: Incorrect
15. Which statement below is incorrect:
a. The attributes in a super key is a subset of attributes set in a
relation
b. Set of a super key is also a super key
c. Any subset of a super key is also a super key
d. All 3 answers above are correct
Answer: c. Any subset of a super key is also a super key
16. Given Q(A, B, C, D, E) has a super key {A, B, C}, which statement below is
incorrect:
a. {A, B, C, D} is a super key
b. {A, B, C, D, E} is a super key
c. Both answers a and b are correct
d. Both answers a and b are incorrectAnswer: d. Both answers a and
b are incorrect 17. What is a candidate key of a relation: Superkey K is a
candidate key if K is minimal
18. Which statement below is correct:
a. Candidate key is a key that can be selected to be the primary key for a
relation
b. Each relation has only 1 candidate key
c. Both answers a and b are correct
lOMoARcPSD|36625228
d. Both answers a and b are incorrect
Answer: a. Candidate key is a key that can be selected to be the primary key
for a relation
19. What is a primary key of a relation:
One of the candidate keys is selected to be the primary key, the primary key must be
not null and unique
20. Is it correct that each relation has only 1 primary key: Correct
21. Given Q1(A, B, C) and Q2(D, A), which statement below is correct:
a. Attribute Q2.A is a foreign key in Q2
b. Q1 is called a referenced relation and Q2 is called a referencing relation
c. Attribute Q2.A referenced through Q1.A
d. All 3 answer above are correct
Answer: d. All 3 answer above are correct
22. Given Q1(A, B, C) and Q2(D, B), which statement below is correct:
a. Q2.B could be a foreign key that referenced through Q1.B if Q1.B has
constraint “unique”
b. Q2.B could be a foreign key that referenced through Q1.B if Q1.B has
constraint “not null”
c. Both answers a and b are correct
d. Both answers a and b are incorrect
Answer: a. Q2.B could be a foreign key that referenced through Q1.B if Q1.B has
constraint “unique”
23. Which statement below is correct:
a. Foreign key in a relation schema represents the association between 2
relations
b. Foreign key constraint is also called referential constraint
c. Both answers a and b are correct
d. Both answers a and b are incorrect
Answer: c. Both answers a and b are correct
24. Explain meaning referential constraint between Q1(A, B, C) and Q2(D, A):
- Q1 is called a referenced relation and Q2 is called a referencing relation
- Attribute Q2.A is a foreign key in Q2
lOMoARcPSD|36625228
- Attribute Q2.A referenced through Q1.A
25. What does “an attribute has ‘unique’ constraint” means:
Each value in the attribute which contains this constraint must be unique
26. What does “an attribute has ‘not null’ constraint” means:
Each cell in the attribute which contains this constraint must has data beside from
null
27. What does “an attribute has ‘primary key’ constraint” means: The ‘primary key
values must be ‘unique’ and ‘not null’
28. Is domain of a meaning attribute a constraint imposes on data of that attribute:
Yes, it is
29. Which one of these symbols: |Sex|, Dom(Sex), MGT(Sex) is the domain symbol of
attribute Sex:
Dom(Sex), MGT(Sex)
30. Given NV(MaNV, Hoten, NgaySinh) and PB(MaPB, TenPB, Matrph), set each
department has 1 staff as a manager. Which statement below is correct about
those 2 relation schemas:
a. MaNV has “unique” and “not null” constraints
b. There is a referential constraint from PB.Matrph to NV.MaNV
c. Both answers a and b are correct
d. Both answers a and b are incorrect
Answer: c. Both answers a and b are correct
31. Are the orders of attributes and tuples important? Why?
No, because in mathematical side, the elements can be anywhere in a set without
changing the essence of that set, so the orders of attributes and tuples are not important II.
Exercise 01:
Suppose that each of the following Update operations is applied directly to the
database state shown above. Discuss all integrity constraints violated by each
operation, if any, and the different ways of enforcing these constraints.
a. Câu lệnh có thể thực hiện mà không vi phạm ràng buộc
b.
- Cột Dnum bảng PROJECT là khóa ngoại tham chiếu với cột Dnumber bảng
DEPARTMENT. Câu lệnh trên cho thấy giá trị cột Dnum là 2 nhưng khi tra vào
cột Dnumber bảng DEPARTMENT thì lại không có. Vậy câu lệnh không thực
hiện được vì vi phạm ràng buộc
lOMoARcPSD|36625228
- Muốn thực hiện được dòng lệnh này ta phải chèn thêm dòng dữ liệu có cột
Dnumber là 2 cho bảng DEPARTMENT
c.
- Câu lệnh trên cho thấy giá trị cột Dnumber là 4 đã có trên bảng và cột Dnumber
lại là khóa chính không được phép trùng nên câu lệnh này không thực hiện được
vì vi phạm ràng buộc
- Muốn thực hiện được dòng lệnh này ta phải thay đổi giá trị cột Dnumber khác 4
để không bị trùng
d.
- Cột Pno là khóa ngoại tham chiếu với cột Pnumber của bảng PROJECT. Theo
câu lệnh trên thì giá trị cột Pno là NULL trong khi cột Pnumber lại không có giá
trị đó vì thế lệnh không được thực hiện vì vi phạm ràng buộc
- Muốn lệnh được thực hiện ta phải thay đổi giá trị NULL của dòng lệnh thành 1
trong các giá trị có sẵn trong cột Pnumber của bảng PROJECT
e. Câu lệnh có thể thực hiện mà không vi phạm ràng buộc
f. Câu lệnh có thể thực hiện mà không vi phạm ràng buộcg.
- Cột Ssn của bảng EMPLOYEE là khóa ngoại tham chiếu với các cột: Mgr_ssn
bảng DEPARTMENT, Essn của bảng WORKS_ON, Essn của bảng
DEPENDENT.
Theo câu lệnh trên khi xóa bộ có giá trị “987654321” ở cột Ssn của bảng
EMPLOYEE thì sẽ vi phạm ràng buộc với những cột tham chiếu trên
- Cột Dno của bảng EMPLOYEE là khóa ngoại tham chiếu với các cột: Dnumber ở
bảng DEPT_LOCATION, Dnumber ở bảng DEPARTMENT, Dnum ở bảng
PROJECT. Theo lệnh trên khi xóa bộ có giá trị “987654321” ở cột Ssn của bảng
EMPLOYEE sẽ không ảnh hưởng đến bảng PROJECT và DEPT_LOCATION
nhưng ảnh hưởng đến bảng DEPARTMENT. Vì thế lệnh này bị vi phạm ràng
buộc
- Muốn thực hiện được câu lệnh trên ta phải thay đổi giá trị Mgr_ssn, và
Mgr_start_date ở bảng DEPARTMENT, xóa bộ có giá trị ở cột Essn "987654321”ở
bảng DEPENDENT, xóa các bộ có giá trị ở cột Essn “987654321” ở bảng
WORKS_ON, sau đó thực hiện lệnh
h.
- Cột Pnumber ở bảng PROJECT là khóa ngoại tham chiếu với cột Pno bảng
WORKS_ON. Theo câu lệnh trên, xóa bộ có giá trị cột Pname là “ProductX” sẽ
làm mất giá trị “1” ở cột Pnumber vì thế các bộ có giá trị cột Pno ở bảng
WORKS_ON sẽ không có giá trị để tham chiếu vào, vì thế vi phạm ràng buộc
lOMoARcPSD|36625228
- Muốn thực hiện được câu lệnh trên ta phải xóa các bộ có giá trị “1” cột Pno ở
bảng
WORKS_ON sau đó mới thực hiện lệnh được
i. Câu lệnh có thể thực hiện mà không vi phạm ràng buộc
j.
- Cột Ssn ở bảng EMPLOYEE là khóa ngoại tham chiếu với cột Essn bảng
WORKS_ON. Theo câu lệnh trên thay đổi giá trị cột Ssn từ “999887777” thành
“943775543” bảng EMPLOYEE nhưng tra cột Essn bảng WORKS_ON vẫn giá trị
cũ vì thế câu lệnh bị vi phạm ràng buộc
- Muốn thực hiện được câu lệnh trên ta phải viết thêm câu lệnh thay đổi tương tự
với bảng WORKS_ON
k.
- Giá trị thay đổi ở câu lệnh trên không thỏa RBTV khi thuộc tính ở cột Pno là số
nguyên dương trong khi dữ liệu thay đổi là số thực dương
- Muốn thực hiện được câu lệnh trên ta phải thay đổi thông tin từ “5.0” thành “5”
| 1/6

Preview text:

lOMoARcPSD| 36625228 ASSIGNMENT 02 ID: 22110032 Name: Lê Gia Huy
------------------------------------------------------------------------------------------------
I. Chapter 2 review questions: 1. What is a relation:
A relation is a table containing data consisting of many columns and many rows, the
data in a relation must satisfy the constraints of that relation
2. What is each column in a relation also called:
Each column is also called an attribute/field of a relation
3. What is each row in a relation also called:
Each row is also called a tuple/record of a relation
4. What is a integrity constraint:
Integrity constraint is a set of rules that all data in a database must satisfy to ensure
that the database is always correct
5. Why do relations need to have integrity constraints: To ensure:
- The cohesion of the components that make up the database - Data consistency
- The database always represents the actual semantics
6. What is a domain of an attribute:
Domain is a set of values that an attribute can take
7. What is a relation schema:
Relation schema is a set of all attributes in the relation
8. Which one of these is a relation schema: a. Q(A, B, C) b. Q A, B, C c. Q{A, B, C} d.
All 3 answers above are correct Answer: a. Q(A, B, C)
9. What is an instance of a relation: lOMoARcPSD| 36625228
Instance of a relation is a set of all tuples of that relation at a specific time
10. What is a relational database:
Relational database is a set of relations that are related to each other about a specific field
11. What is a relational database schema:
Relational database schema is a set of relation schemas that are related to each other about a specific field
12. What is a super key of a relation schema Q(R):
K is a superkey of R if values for K are sufficient to identify a unique tuple of each possible relation
13. Is it correct that there is only 1 super key in a relation: Incorrect
14. Is it correct that a super key in a relation can contains more attributes than those
in that relation: Incorrect
15. Which statement below is incorrect: a.
The attributes in a super key is a subset of attributes set in a relation b.
Set of a super key is also a super key c.
Any subset of a super key is also a super key d.
All 3 answers above are correct
Answer: c. Any subset of a super key is also a super key
16. Given Q(A, B, C, D, E) has a super key {A, B, C}, which statement below is incorrect: a.
{A, B, C, D} is a super key b.
{A, B, C, D, E} is a super key c.
Both answers a and b are correct d.
Both answers a and b are incorrectAnswer: d. Both answers a and
b are incorrect 17. What is a candidate key of a relation: Superkey K is a candidate key if K is minimal
18. Which statement below is correct:
a. Candidate key is a key that can be selected to be the primary key for a relation
b. Each relation has only 1 candidate key
c. Both answers a and b are correct lOMoARcPSD| 36625228
d. Both answers a and b are incorrect
Answer: a. Candidate key is a key that can be selected to be the primary key for a relation
19. What is a primary key of a relation:
One of the candidate keys is selected to be the primary key, the primary key must be not null and unique
20. Is it correct that each relation has only 1 primary key: Correct
21. Given Q1(A, B, C) and Q2(D, A), which statement below is correct:
a. Attribute Q2.A is a foreign key in Q2
b. Q1 is called a referenced relation and Q2 is called a referencing relation
c. Attribute Q2.A referenced through Q1.A
d. All 3 answer above are correct
Answer: d. All 3 answer above are correct
22. Given Q1(A, B, C) and Q2(D, B), which statement below is correct:
a. Q2.B could be a foreign key that referenced through Q1.B if Q1.B has
constraint “unique”
b. Q2.B could be a foreign key that referenced through Q1.B if Q1.B has
constraint “not null”
c. Both answers a and b are correct
d. Both answers a and b are incorrect
Answer: a. Q2.B could be a foreign key that referenced through Q1.B if Q1.B has constraint “unique”
23. Which statement below is correct:
a. Foreign key in a relation schema represents the association between 2 relations
b. Foreign key constraint is also called referential constraint
c. Both answers a and b are correct
d. Both answers a and b are incorrect
Answer: c. Both answers a and b are correct
24. Explain meaning referential constraint between Q1(A, B, C) and Q2(D, A):
- Q1 is called a referenced relation and Q2 is called a referencing relation
- Attribute Q2.A is a foreign key in Q2 lOMoARcPSD| 36625228
- Attribute Q2.A referenced through Q1.A
25. What does “an attribute has ‘unique’ constraint” means:
Each value in the attribute which contains this constraint must be unique
26. What does “an attribute has ‘not null’ constraint” means:
Each cell in the attribute which contains this constraint must has data beside from null
27. What does “an attribute has ‘primary key’ constraint” means: The ‘primary key’
values must be ‘unique’ and ‘not null’
28. Is domain of a meaning attribute a constraint imposes on data of that attribute: Yes, it is
29. Which one of these symbols: |Sex|, Dom(Sex), MGT(Sex) is the domain symbol of attribute Sex: Dom(Sex), MGT(Sex)
30. Given NV(MaNV, Hoten, NgaySinh) and PB(MaPB, TenPB, Matrph), set each
department has 1 staff as a manager. Which statement below is correct about those 2 relation schemas:
a. MaNV has “unique” and “not null” constraints
b. There is a referential constraint from PB.Matrph to NV.MaNV
c. Both answers a and b are correct
d. Both answers a and b are incorrect
Answer: c. Both answers a and b are correct
31. Are the orders of attributes and tuples important? Why?
No, because in mathematical side, the elements can be anywhere in a set without
changing the essence of that set, so the orders of attributes and tuples are not important II. Exercise 01:
Suppose that each of the following Update operations is applied directly to the
database state shown above. Discuss all integrity constraints violated by each
operation, if any, and the different ways of enforcing these constraints.

a. Câu lệnh có thể thực hiện mà không vi phạm ràng buộc b.
- Cột Dnum bảng PROJECT là khóa ngoại tham chiếu với cột Dnumber bảng
DEPARTMENT. Câu lệnh trên cho thấy giá trị cột Dnum là 2 nhưng khi tra vào
cột Dnumber bảng DEPARTMENT thì lại không có. Vậy câu lệnh không thực
hiện được vì vi phạm ràng buộc lOMoARcPSD| 36625228
- Muốn thực hiện được dòng lệnh này ta phải chèn thêm dòng dữ liệu có cột
Dnumber là 2 cho bảng DEPARTMENT c.
- Câu lệnh trên cho thấy giá trị cột Dnumber là 4 đã có trên bảng và cột Dnumber
lại là khóa chính không được phép trùng nên câu lệnh này không thực hiện được vì vi phạm ràng buộc
- Muốn thực hiện được dòng lệnh này ta phải thay đổi giá trị cột Dnumber khác 4 để không bị trùng d.
- Cột Pno là khóa ngoại tham chiếu với cột Pnumber của bảng PROJECT. Theo
câu lệnh trên thì giá trị cột Pno là NULL trong khi cột Pnumber lại không có giá
trị đó vì thế lệnh không được thực hiện vì vi phạm ràng buộc
- Muốn lệnh được thực hiện ta phải thay đổi giá trị NULL của dòng lệnh thành 1
trong các giá trị có sẵn trong cột Pnumber của bảng PROJECT
e. Câu lệnh có thể thực hiện mà không vi phạm ràng buộc
f. Câu lệnh có thể thực hiện mà không vi phạm ràng buộcg.
- Cột Ssn của bảng EMPLOYEE là khóa ngoại tham chiếu với các cột: Mgr_ssn
bảng DEPARTMENT, Essn của bảng WORKS_ON, Essn của bảng DEPENDENT.
Theo câu lệnh trên khi xóa bộ có giá trị “987654321” ở cột Ssn của bảng
EMPLOYEE thì sẽ vi phạm ràng buộc với những cột tham chiếu trên
- Cột Dno của bảng EMPLOYEE là khóa ngoại tham chiếu với các cột: Dnumber ở
bảng DEPT_LOCATION, Dnumber ở bảng DEPARTMENT, Dnum ở bảng
PROJECT. Theo lệnh trên khi xóa bộ có giá trị “987654321” ở cột Ssn của bảng
EMPLOYEE sẽ không ảnh hưởng đến bảng PROJECT và DEPT_LOCATION
nhưng ảnh hưởng đến bảng DEPARTMENT. Vì thế lệnh này bị vi phạm ràng buộc
- Muốn thực hiện được câu lệnh trên ta phải thay đổi giá trị Mgr_ssn, và
Mgr_start_date ở bảng DEPARTMENT, xóa bộ có giá trị ở cột Essn "987654321”ở
bảng DEPENDENT, xóa các bộ có giá trị ở cột Essn “987654321” ở bảng
WORKS_ON, sau đó thực hiện lệnh h.
- Cột Pnumber ở bảng PROJECT là khóa ngoại tham chiếu với cột Pno bảng
WORKS_ON. Theo câu lệnh trên, xóa bộ có giá trị cột Pname là “ProductX” sẽ
làm mất giá trị “1” ở cột Pnumber vì thế các bộ có giá trị cột Pno ở bảng
WORKS_ON sẽ không có giá trị để tham chiếu vào, vì thế vi phạm ràng buộc lOMoARcPSD| 36625228
- Muốn thực hiện được câu lệnh trên ta phải xóa các bộ có giá trị “1” cột Pno ở bảng
WORKS_ON sau đó mới thực hiện lệnh được
i. Câu lệnh có thể thực hiện mà không vi phạm ràng buộc j.
- Cột Ssn ở bảng EMPLOYEE là khóa ngoại tham chiếu với cột Essn bảng
WORKS_ON. Theo câu lệnh trên thay đổi giá trị cột Ssn từ “999887777” thành
“943775543” bảng EMPLOYEE nhưng tra cột Essn bảng WORKS_ON vẫn giá trị
cũ vì thế câu lệnh bị vi phạm ràng buộc
- Muốn thực hiện được câu lệnh trên ta phải viết thêm câu lệnh thay đổi tương tự với bảng WORKS_ON k.
- Giá trị thay đổi ở câu lệnh trên không thỏa RBTV khi thuộc tính ở cột Pno là số
nguyên dương trong khi dữ liệu thay đổi là số thực dương
- Muốn thực hiện được câu lệnh trên ta phải thay đổi thông tin từ “5.0” thành “5”