0% found this document useful (0 votes)
43 views4 pages

Bài Tập Thực Hành Quản Lý Thông Tin

The document provides instructions for creating test data in a database including tables for courses, students, subjects, student scores, and student course enrollment data stored in XML format. It then poses a series of questions to write XPath, XQuery, and SQL queries to retrieve, update, insert and delete data from the XML and related database tables. Students are asked to submit their solutions in a PDF file along with the SQL code file.

Uploaded by

Tan Dat Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views4 pages

Bài Tập Thực Hành Quản Lý Thông Tin

The document provides instructions for creating test data in a database including tables for courses, students, subjects, student scores, and student course enrollment data stored in XML format. It then poses a series of questions to write XPath, XQuery, and SQL queries to retrieve, update, insert and delete data from the XML and related database tables. Students are asked to submit their solutions in a PDF file along with the SQL code file.

Uploaded by

Tan Dat Nguyen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

IE103 – Quản lý Thông tin

Bài tập thực hành môn Quản lý thông tin tuần 5


Tạo dữ liệu test như sau:

CREATE DATABASE TEST_XML


GO

USE TEST_XML GO

CREATE TABLE KhoaHoc


(
MaKhoaHoc INT IDENTITY(1,1)NOT NULL,
TenKhoaHoc VARCHAR(200) NOT NULL,
CONSTRAINT PK_KhoaHoc PRIMARY KEY(MaKhoaHoc)
)
INSERT INTO KhoaHoc (TenKhoaHoc) SELECT 'Mang May Tinh Truyen Thong'
INSERT INTO KhoaHoc (TenKhoaHoc) SELECT 'Khoa Hoc May Tinh'
INSERT INTO KhoaHoc (TenKhoaHoc) SELECT 'Ky Thuat May Tinh'

CREATE TABLE SinhVien


(
MSSV BIGINT IDENTITY(1,1)NOT NULL CONSTRAINT PK_SinhVien PRIMARY KEY(MSSV),
TenSV VARCHAR(200) NOT NULL,
MaKhoaHoc INT NOT NULL CONSTRAINT FK_SinhVien_MaKhoaHoc FOREIGN KEY REFERENCES
KhoaHoc(MaKhoaHoc)
)

INSERT INTO SinhVien SELECT 'Sang',1


INSERT INTO SinhVien SELECT 'Duy',2
INSERT INTO SinhVien SELECT 'Sa', 3

CREATE TABLE MonHoc


(
MaMonHoc INT IDENTITY NOT NULL CONSTRAINT PK_MonHoc PRIMARY KEY(MaMonHoc),
TenMonHoc VARCHAR(200)
)

INSERT INTO MonHoc (TenMonHoc) SELECT ('Co So Du Lieu')


INSERT INTO MonHoc (TenMonHoc) SELECT ('Cau Truc Du Lieu')
INSERT INTO MonHoc (TenMonHoc) SELECT ('Lap Trinh Di Dong')
INSERT INTO MonHoc (TenMonHoc) SELECT ('Toan Giai Tich')
INSERT INTO MonHoc (TenMonHoc) SELECT ('Lap Trinh Java')
INSERT INTO MonHoc (TenMonHoc) SELECT ('He Quan Tri CSDL')
INSERT INTO MonHoc (TenMonHoc) SELECT ('Anh Van')
INSERT INTO MonHoc (TenMonHoc) SELECT ('Thiet Ke Web ')
INSERT INTO MonHoc (TenMonHoc) SELECT ('An Toan Thong Tin')
CREATE TABLE KhoaHocMonHoc
(
MaKhoaHoc INT CONSTRAINT FK_KhoaHocMonHoc_MaKhoaHoc FOREIGN KEY REFERENCES
KhoaHoc(MaKhoaHoc),

1
IE103 – Quản lý Thông tin

MaMonHoc INT CONSTRAINT FK_KhoaHocMonHoc_MaMonHoc FOREIGN KEY REFERENCES


MonHoc(MaMonHoc)
)

INSERT INTO KhoaHocMonHoc (MaKhoaHoc,MaMonHoc) SELECT 1,1


INSERT INTO KhoaHocMonHoc (MaKhoaHoc,MaMonHoc) SELECT 1,2
INSERT INTO KhoaHocMonHoc (MaKhoaHoc,MaMonHoc) SELECT 1,3
INSERT INTO KhoaHocMonHoc (MaKhoaHoc,MaMonHoc) SELECT 2,4
INSERT INTO KhoaHocMonHoc (MaKhoaHoc,MaMonHoc) SELECT 2,5
INSERT INTO KhoaHocMonHoc (MaKhoaHoc,MaMonHoc) SELECT 2,6
INSERT INTO KhoaHocMonHoc (MaKhoaHoc,MaMonHoc) SELECT 3,7
INSERT INTO KhoaHocMonHoc (MaKhoaHoc,MaMonHoc) SELECT 3,8
INSERT INTO KhoaHocMonHoc (MaKhoaHoc,MaMonHoc) SELECT 3,9

CREATE TABLE Diem


(
MSSV BIGINT CONSTRAINT FK_Diem_MSSV FOREIGN KEY REFERENCES SinhVien(MSSV),
MaMonHoc INT CONSTRAINT FK_Diem_MaMonHoc FOREIGN KEY REFERENCES
MonHoc(MaMonHoc),
Diem INT
)
INSERT INTO Diem (MSSV,MaMonHoc,Diem) SELECT 1,1,75
INSERT INTO Diem (MSSV,MaMonHoc,Diem) SELECT 1,2,80
INSERT INTO Diem (MSSV,MaMonHoc,Diem) SELECT 1,3,70
INSERT INTO Diem (MSSV,MaMonHoc,Diem) SELECT 2,4,80
INSERT INTO Diem (MSSV,MaMonHoc,Diem) SELECT 2,5,80
INSERT INTO Diem (MSSV,MaMonHoc,Diem) SELECT 2,6,90
INSERT INTO Diem (MSSV,MaMonHoc,Diem) SELECT 3,7,80
INSERT INTO Diem (MSSV,MaMonHoc,Diem) SELECT 3,8,80
INSERT INTO Diem (MSSV,MaMonHoc,Diem) SELECT 3,9,90

CREATE TABLE QuanLySV


(MSDH INT NOT NULL,
TenDH VARCHAR(20),
ChiTietSV XML)

INSERT INTO QuanLySV VALUES (1,'DH CNTT','<THONGTINSV>


<sinhvien ID="10" Ten="Nam">
<monhoc ID="1" Ten="Co So Du Lieu" />
<monhoc ID="2" Ten="Cau Truc Du Lieu" />
<monhoc ID="3" Ten="Lap Trinh Mobile" />
</sinhvien>
<sinhvien ID="11" Ten="An">
<monhoc ID="4" Ten="Toan Giai Tich" />
<monhoc ID="5" Ten="Lap Trinh Java" />
<monhoc ID="6" Ten="He Quan Tri CSDL" />
</sinhvien>
<sinhvien ID="12" Ten="Thanh">
<monhoc ID="7" Ten="Anh Van" />
<monhoc ID="8" Ten="Thiet Ke Web" />
<monhoc ID="9" Ten="An Toan Thong Tin" />
</sinhvien>

2
IE103 – Quản lý Thông tin

</THONGTINSV>')

INSERT INTO QuanLySV VALUES (2,'DH KHTN','<THONGTINSV>


<sinhvien ID="10" Ten="Khang">
<monhoc ID="1" Ten="Co So Du Lieu" />
<monhoc ID="2" Ten="Cau Truc Du Lieu" />
<monhoc ID="3" Ten="Lap Trinh Mobile" />
</sinhvien>
<sinhvien ID="11" Ten="Vinh">
<monhoc ID="4" Ten="Toan Giai Tich" />
<monhoc ID="5" Ten="Lap Trinh Java" />
<monhoc ID="6" Ten="He Quan Tri CSDL" />
</sinhvien>
<sinhvien ID="12" Ten="Hoa">
<monhoc ID="7" Ten="Anh Van" />
<monhoc ID="8" Ten="Thiet Ke Web" />
<monhoc ID="9" Ten="An Toan Thong Tin" />
</sinhvien>
</THONGTINSV>')

Từ câu 1 đến câu 6 (gợi ý dùng bảng QuanLySV)

Câu 1: Viết lệnh Xpath lấy Sinh viên có ID=10. Lệnh lấy sinh viên ở vị trí cuối cùng ở
trường CNTT.

Câu 2: Viết lệnh Xpath lấy tên Sinh viên có ID=10 trong trường Đại học CNTT.

Gợi ý: dùng hàm value() thay cho query().

Câu 3: Viết lệnh trả về tất cả các nút từ nút gốc là THONGTINSV.

Câu 4: Viết lệnh Xquery trả về danh sách sinh viên có ID < 12 với MSDH = 1.

Câu 5: Viết lệnh Xquery trả về danh sách sinh viên sắp xếp theo tên với MSDH=2.

Câu 6: Viết lệnh Xquery trả về MSDH và TenDH theo định dạng sau:
<QuanLySV>
<ChiTietSV>1 DH CNTT</ChiTietSV>
</QuanLySV>

Câu 7: Viết lệnh Xquery xóa tên các sinh viên trường DH KHTN.

Gợi ý: Dùng lệnh modify() thay cho query(). Sử dụng câu truy vấn SQL để cập nhật dữ
liệu.

Câu 8: Viết lệnh Xquery trả về thông tin các sinh viên có tên là ‘Nam’ hoặc ‘Thanh’.

3
IE103 – Quản lý Thông tin

Câu 9: Viết lệnh Xquery thay đổi tên sinh viên thứ 2 thành tên ‘Binh’ trong trường
CNTT.

Gợi ý: dùng lệnh 'replace value of <XPath> with "new_value" ' trong hàm modify()

Câu 10: Viết lệnh Xquery kiểm tra xem có tồn tại sinh viên có ID là 12 trong trường
KHTN không? (Nếu có trả về 1, nếu không thì trả về 0).

Gợi ý: Dùng lệnh exist() thay cho query().

Câu 11: Thêm môn học có ID = 13 vào đối tượng sinh viên có ID = 10 của trường đại
học Công nghệ thông tin.

Gợi ý: dùng lệnh insert('<dữ liệu xml'>) into (<tên node>) trong hàm modify()

Câu 12: Thêm thuộc tính tên môn học là "Quản lý thông tin" cho môn học có ID = 13
vào đối tượng sinh viên có ID = 10 của trường đại học Công nghệ thông tin.

Câu 13*: Viết lệnh Xquery kiểm tra xem có tồn tại sinh viên tên ‘Lan’ trong trường
CNTT không? (Nếu có trả về 1, nếu không thì trả về 0). Và INSERT thêm vào
THONGTINSV:

Gợi ý: dùng lệnh insert('<dữ liệu xml'>) after (<tên node>) trong hàm modify()

Câu 14*: Thực hiện phép nối bằng các lệnh Xquery như một câu truy vấn trong SQL để
trả về thông tin là sinh viên nào học khóa học tên là gì? (Nếu được có thể mở rộng thêm
truy vấn sinh viên nào học môn học gì?).

Gợi ý: Sử dụng lệnh FOR XML AUTO, ELEMENTS và FOR XML RAW, ELEMENTS

Hướng dẫn nộp bài:

+ Nộp file PDF. Đặt tên file: MSSV_HoTen_BTTH5.pdf.

+ Các bạn trình bày code SQL thực thi vào báo cáo ứng với từng câu hỏi.

+ Các bạn nộp kèm theo file .sql chứa code thực thi của các bạn với file báo cáo.

+ Nộp qua hệ thống [Link]. Lưu ý: KHÔNG NÉN FILE.

Common questions

Powered by AI

The QuanLySV XML table plays a critical role in managing complex data relationships in a semi-structured format. It holds detailed student information and their course enrollments, allowing for rich, hierarchical data storage and retrieval through XPath and XQuery functionalities. This table supports advanced querying, such as looking up students by certain criteria or iterating through nested hierarchies, thus complementing the structured relational database tables and offering flexibility in data handling .

Maintaining an XML field like ChiTietSV increases design complexity as it requires careful integration with relational data structures and may necessitate additional parsing overhead, affecting performance. The need to balance efficient retrieval versus update capabilities can lead to complex query designs. Moreover, such fields may require additional indexing strategies or transformation pipelines to ensure they meet performance requirements relative to relational counterparts .

The XML data format offers advantages such as flexibility in representing complex and hierarchical data structures, which suits the 'QuanLySV' table's need to manage detailed student and course relationships. It facilitates complex querying and data manipulation using XPath and XQuery, allowing for efficient retrieval, updates, and restructuring of data with significant variability or optional elements .

Using foreign keys in the database schema effectively maintains data integrity by ensuring that relationships between tables are consistent. For instance, the table SinhVien links to KhoaHoc via MaKhoaHoc, enforcing that each student's course enrollment is valid only if a corresponding course exists. This avoids orphan records and ensures referential integrity across relational tables .

The identity column feature contributes to scalability by automatically generating unique identifiers for new entries in tables like 'KhoaHoc' and 'SinhVien', simplifying data insertion processes and ensuring unique primary keys. This feature aids in efficiently managing growing datasets, avoiding manual ID assignment, and minimizing the risk of key duplication, which is vital for database growth and maintenance .

The database creation commands are used to set up a systematic way to manage information related to courses, students, and their scores. This involves creating tables such as KhoaHoc, SinhVien, MonHoc, and others, which are used to store information about courses, students, and their respective scores. The structure also establishes relationships through foreign keys to ensure data integrity .

Using XQuery to update student information is crucial in this database system as it allows for precise modifications of XML data within a relational database context. This is particularly useful for adjusting nested or hierarchical data structures that are less straightforward to manage using standard SQL, enabling dynamic updates and insertion based on complex conditions or attributes without affecting unrelated data .

Inserting data into the 'MonHoc' table helps ensure data consistency by maintaining a centralized record of all courses, which different parts of the database can reference. This centralized approach reduces errors of discrepancies in course naming or details across different tables or records, thereby promoting consistency and accurate reference through foreign key constraints linking to the MonHoc table .

Using XML for storing student information, while flexible for hierarchical data, presents challenges such as performance overhead due to the complexity of parsing XML data. Additionally, maintaining XML structures alongside relational tables can complicate data synchronization, risk data redundancy, and increase the complexity of data integrity management across systems, potentially leading to higher maintenance costs .

To retrieve the name of the student with ID 10 from the university of technology using Xpath, you would use the query: `/THONGTINSV/sinhvien[@ID='10']/@Ten`, which targets the attribute 'Ten' of the 'sinhvien' element where the attribute 'ID' equals 10 .

You might also like