1. Trang chủ
  2. » Giáo Dục - Đào Tạo

BÁO CÁO MÔN CƠ SỞ DỮ LIÊU NÂNG CAO

47 13 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Cơ Sở Dữ Liệu Nâng Cao
Tác giả Bùi Thị Thu Huệ
Trường học Trường Đại Học Bách Khoa Hà Nội
Chuyên ngành Toán Tin
Thể loại báo cáo
Năm xuất bản 2020
Thành phố Hà Nội
Định dạng
Số trang 47
Dung lượng 702,72 KB

Cấu trúc

  • Phần 1: Cơ sở dữ liệu lớn (3)
    • 1.1 Big Data là gì? (3)
    • 1.2 Đặc trưng của Big Data (3)
    • 1.3 Cơ sở dữ liệu No SQL (4)
  • Phần 2: Cơ sở dữ liệu phân tán (5)
    • 2.1 Khái niệm (5)
    • 2.2 Ưu và nhược điểm của CSDL phân tán (5)
      • 2.2.1 Ưu điểm (5)
      • 2.2.2 Nhược điểm (5)
    • 2.3 Kiển trúc của một hệ cơ sở dữ liệu phân tán (6)
    • 2.4 Tính trong suốt của cơ sở dữ liệu phân tán (6)
  • Phần 3: Hệ quản trị cơ sở dữ liệu Oracle (8)
  • Lecture 1 (8)
  • Lecture 2 (8)
  • Lecture 3 (10)
  • Lecture 4 (13)
  • Lecture 5 (17)
  • Lecture 6 (23)
  • Lecture 8 (28)
  • Phần 4: Bài tập kết thúc môn (33)
  • Bài 1: (33)
  • Bài 2: (36)
  • Bài 3. (38)
  • Bài 4. (39)
  • Bài 6. (40)
  • Bài 7: (42)
  • Bài 8: (45)
  • Tài liệu tham khảo (47)

Nội dung

Cơ sở dữ liệu lớn

Big Data là gì?

Big Data đề cập đến các tập dữ liệu lớn và phức tạp, đến mức mà các phần mềm xử lý dữ liệu truyền thống không thể thu thập, quản lý và xử lý hiệu quả trong thời gian hợp lý.

Thuật ngữ “Big Data” không chỉ đề cập tới dữ liệu mà còn chỉ cơ cấu tổ chức dữ liệu, các công cụ và công nghệ liên quan

Theo Gartner, Big Data được xem là tài sản thông tin với khối lượng lớn, tốc độ cao và tính đa dạng Để xử lý hiệu quả loại dữ liệu này, cần áp dụng công nghệ mới nhằm đưa ra quyết định chính xác, khám phá các yếu tố ẩn sâu trong dữ liệu và tối ưu hóa quy trình xử lý.

Đặc trưng của Big Data

Big Data được mô tả bởi những đặc trưng sau:

Dung lượng dữ liệu là số lượng thông tin được tạo ra và lưu trữ, xác định giá trị cũng như tiềm năng của những thông tin đó Kích thước dữ liệu đóng vai trò quan trọng trong việc xác định liệu nó có đủ lớn để được coi là "dữ liệu lớn" hay không.

Tính đa dạng của dữ liệu thể hiện qua nhiều dạng và kiểu khác nhau, với nguồn gốc thu thập từ nhiều nơi Các loại dữ liệu này có cấu trúc đa dạng, bao gồm video, văn bản, cơ sở dữ liệu, số liệu và dữ liệu cảm biến.

Tốc độ (Velocity) đề cập đến khả năng tạo ra và xử lý dữ liệu nhanh chóng nhằm đáp ứng nhu cầu và thách thức trong quá trình tăng trưởng và phát triển.

• Veracity (Tính xác thực): Chất lượng của dữ liệu thu được có thể khác nhau rất nhiều, ảnh hưởng đến sự phân tích chính xác

Cơ sở dữ liệu No SQL

Để đáp ứng nhu cầu về cơ sở dữ liệu và hạ tầng, các nhà phát triển đã thực hiện nhiều thay đổi đáng kể Công nghệ đám mây với chi phí hợp lý đã thay thế các máy chủ phức tạp và đắt tiền Ngoài ra, các kỹ sư đang áp dụng các phương pháp tăng tốc nhằm phát triển liên tục và rút ngắn chu trình, giúp truy vấn dữ liệu nhanh chóng và đáp ứng nhu cầu người dùng hiệu quả.

NoSQL ra đời để đáp ứng nhu cầu lưu trữ và quản lý dữ liệu hiện đại, hỗ trợ tốc độ vận hành cao và mang lại tính linh hoạt cho các nhà phát triển Khác với cơ sở dữ liệu SQL, nhiều hệ thống NoSQL có khả năng mở rộng ngang trên hàng trăm hoặc hàng ngàn máy chủ.

Một số đặc điểm chung:

• High Scalability: Gần như không có một giới hạn cho dữ liệu và người dùng trên hệ thống

• High Availability: Do chấp nhận sự trùng lặp trong lưu trữ nên nếu một node nào đó bị chết cũng không ảnh hưởng tới toàn bộ hệ thống

• Consistency: chấp nhận tính nhất quán yếu, có thể không thấy ngay được sự thay đổi mặc dù đã cập nhật dữ liệu

• Durability: dữ liệu có thể tồn tại trong bộ nhớ máy tính nhưng đồng thời cũng được lưu trữ lại đĩa cứng

Triển khai linh hoạt cho phép bổ sung hoặc loại bỏ các node một cách tự động, giúp hệ thống nhận biết và lưu trữ mà không cần can thiệp thủ công Hơn nữa, hệ thống không yêu cầu cấu hình phần cứng mạnh mẽ và đồng nhất, tạo điều kiện thuận lợi cho việc mở rộng và quản lý.

Cơ sở dữ liệu phân tán

Khái niệm

• Cơ sở dữ liệu phân tán: Một tuyển tập dữ liệu có quan hệ logic với nhau, được phân bố trên các máy tính của một mạng máy tính

Hệ quản trị cơ sở dữ liệu phân tán là phần mềm giúp quản lý các cơ sở dữ liệu được phân tán, đồng thời đảm bảo tính minh bạch cho người dùng về sự phân tán này.

• Ứng dụng cục bộ: được yêu cầu và thực hiện trên máy tính ở một nút trong hệ CSDL phân tán và chỉ liên quan đến CSDL tại nút đó

• Ứng dụng toàn cục: yêu cầu truy nhập dữ liệu ở nhiều nút thông qua hệ thống truyền thông.

Ưu và nhược điểm của CSDL phân tán

• Phù hợp với cấu trúc của tổ chức lớn

• Nâng cao khả năng chia sẻ và tính tự trị địa phương

• Nâng cao tính sẵn sàng

• Nâng cao tính tin cậy

• Thiết kế cơ sở dữ liệu phức tạp hơn

• Khó điều khuyển tính nhất quán dữ liệu

• Khó phát hiện và xử lý lỗi

Kiển trúc của một hệ cơ sở dữ liệu phân tán

Do sự đan dạng, không có kiên trúc nào được công nhận tương đương với kiến trúc 3 mức ANSI/SPARC

Một kiến trúc tham khảo bao gồm:

• Tập các sơ đồ ngoài toàn cục (Global external schemas)

• Sơ đồ khái niệm toàn cục (Global conceptual schema)

• Sơ đồ phân đoạn (Fragmentation schema) và sơ đồ định vị (Allocation schema)

• Tập các sơ đồ cho mỗi hệ CSDL cục bộ tuân theo tiêu chuẩn 3 mức ANSI/SPARC

Lược đồ toàn cục: định nghĩa tất cả dữ liệu được chứa trong cơ sở dữ liệu phân tán

Lược đồ phân đoạn là phương pháp chia một quan hệ tổng thể thành các phần nhỏ hơn, không giao nhau, được gọi là đoạn Điều này tạo ra một ánh xạ giữa quan hệ toàn cục và các đoạn, giúp quản lý và tổ chức dữ liệu hiệu quả hơn.

Lược đồ định vị: định vị các nút chứa một đoạn

Lược đồ ánh xạ cục bộ: phụ thuộc vào loại Hệ quản trị cơ sở dữ liệu phân tán.

Tính trong suốt của cơ sở dữ liệu phân tán

Các mức trong suốt của hệ cơ sở dữ liệu phân tán:

Trong suốt phân đoạn (fragmentation transparency) là mức độ cao nhất của tính trong suốt trong cơ sở dữ liệu, cho phép người dùng cuối và lập trình viên không cần phải biết về sự phân tán của dữ liệu Họ không cần quan tâm đến tên và vị trí phân bố các đoạn dữ liệu, giúp đơn giản hóa quá trình truy cập và quản lý thông tin.

Định vị trong cơ sở dữ liệu được hiểu là sự minh bạch về vị trí, nghĩa là người dùng cuối hoặc lập trình viên nhận thức được rằng cơ sở dữ liệu được chia thành các đoạn và biết tên của các đoạn đó, nhưng không cần biết chính xác vị trí phân bố của chúng.

• Trong suốt ánh xạ địa phương (local mapping transparency): Người dùng cuối hoặc người lập trình biết tên các đoạn và vị trí của các đoạn

To create the DEPT table, begin by referencing the provided table instance chart Write the necessary SQL syntax in a script named lab_09_01.sql, and then execute this script to create the table Finally, verify that the table has been successfully created.

(id NUMBER(7) CONSTRAINT dept_department_id PRIMARY KEY, name VARCHAR2(25));

Practice 2: Populate the DEPT table with data from the DEPARTMENTS table Include only columns that you need

SELECT department_id, department_name

To create the EMP table, begin by referencing the provided table instance chart Write the necessary SQL syntax in a script named lab_09_03.sql, and execute the script to create the table Finally, verify the successful creation of the table to ensure everything has been implemented correctly.

(id NUMBER(7) CONSTRAINT emp_employee_id PRIMARY KEY, last_name VARCHAR2(25), first_name VARCHAR2(25), dept_id NUMBER(7) CONSTRAINT empdept_fk1

Practice 4: Create the EMPLOYEES2 table based on the structure of the EMPLOYEES table

To create a new table, select only the EMPLOYEE_ID, FIRST_NAME, LAST_NAME, SALARY, and DEPARTMENT_ID columns from the original dataset Rename these columns to ID, FIRST_NAME, LAST_NAME, SALARY, and DEPT_ID in the new table.

SELECT employee_id id, first_name, last_name, salary, department_id dept_id

Practice 5: Drop the EMP table

Practice 6: Create a nonunique index on the DEPT_ID colums in the DEPT table

CREAT INDEX emp_dept_id_idx ON emp(dept_id);

The HR department aims to conceal certain data within the EMPLOYEES table by creating a view named EMPLOYEES_VU This view will display employee numbers, employee names—labeled as EMPLOYEE—and department numbers, ensuring that sensitive information remains protected while providing essential details.

CREATE OR REPLACE VIEW employees_vu

SELECT employee_id, last_name employee, department_id

Practice 2: Confirm that the view works Display the contents of the EMPLOYEES_VU view

Practice 3: Using your EMPLOYEES_VU view, write a query for the HR department to display all employee names and department numbers

SELECT employee, department_id FROM employees_vu;

To facilitate access to employee data for Department 50, create a view named DEPT50 that includes employee numbers, last names, and department numbers, labeled as EMPNO, EMPLOYEE, and DEPTNO Ensure that this view restricts any reassignment of employees to different departments for security reasons.

Display the structure and contents of the DEPT50 view

Test your view Attempt to reassign Mohammed to department 80

CREATE OR REPLACE VIEW dept50

SELECT employee_id empno, last_name employee, department_id deptno

Create a sequence named DEPT_ID_SEQ for the primary key column of the DEPT table, starting at 200 and incrementing by 10, with a maximum value of 1000.

To validate your sequence, create a script that inserts two new rows into the DEPT table using the sequence you established for the ID column Add the departments "Education" and "Administration," then confirm the successful addition of these entries by executing the commands in your script.

CREATE SEQUENCE dept_id_seq

INSERT INTO dept(ID, NAME)

VALUES (DEPT_ID_SEQ.NEXTVAL, 'Education');

INSERT INTO dept(ID, NAME)

VALUES (DEPT_ID_SEQ.NEXTVAL, 'Administration');

Practice 4: Create a synonym for your EMPLOYEES table Call it EMP

1 The following SELECT statement executes successfully:

SELECT last_name, job_id, salary AS Sal

2 The following SELECT statement executes successfully:

3 There are four coding errors in the following statement Can you identify them?

SELECT employee_id, last_name, salary*12 “ANNUAL SALARY”

4 The HR department needs a query to display all unique job codes from the EMPLOYEES table

SELECT DISTINCT job_id FROM employees;

The HR department has requested a comprehensive report detailing all employees along with their job IDs The report should present the last name of each employee concatenated with their respective job ID, separated by a comma and a space This combined information will be labeled under the column heading "Employee and Title."

SELECT last_name || ', ' || job_id AS "Employee and Title"

1 The HR departments needs to find high-salary and low-salary employees Display the last name and salary of employees who earn between $5,000 and $12,000 and are in department

20 or 50 Label the columns Employee and Monthly Salary, respectively

SELECT last_name "Employee", salary "Monthly Salary"

2 Create a report to display the last name, salary, and commission of all employees who earn commissions Sort data in descending order of salary and commissions

SELECT last_name, salary, commission_pct

WHERE commission_pct IS NOT NULL

ORDER BY salary DESC, commission_pct DESC;

3 Display the last name of all employees who have both an a and an e in the last name

WHERE last_name LIKE '%a%' AND last_name LIKE '%e%';

4 Display the last name, job, and salary for all employees whose job is SA_REP or ST_CLERK and whose salary is not equal to $ 2,500, $3,500, or $7,000

SELECT last_name, job_id, salary

WHERE job_id IN ('SA_REP', 'ST_CLERK')

To retrieve the last names of employees starting with the letters J, A, or M, formatted with the first letter capitalized and the remaining letters in lowercase, along with the length of each last name, you can execute the following SQL query The results will display the last name as "Last Name" and its length as "Name Length."

SELECT INITCAP(last_name) " Name ", LENGTH(last_name) " Length "

The HR department aims to determine the duration of employment for each employee by displaying their last names and calculating the number of months between the current date and their hire date This calculation will be labeled as MONTHS_WORKED, and the results will be sorted by the total months employed Additionally, the number of months will be rounded up to the nearest whole number for clarity.

ROUND((sysdate - hire_date)/30) AS " MONTHS_WORKED "

To display each employee's last name, hire date, and salary review date, which occurs on the first Monday after six months of service, label the column as "REVIEW." Ensure that the dates are formatted to resemble "Monday, the Thirty-First of July, 2000."

SELECT last_name, hire_date,

TO_CHAR(NEXT_DAY(ADD_MONTHS(hire_date, 6),'MONDAY'),

'fmDay, "the" Ddspth "of" Month, YYYY') REVIEW

4 Create a query that displays the employees’ last names and commission amounts.if an employee does not earn commission, show “ No Commission” Label the column COMM

SELECT last_name, NVL(to_char(commission_pct),'No commission') COMM

5 Using the DECODE function, write a query that displays the grade of all employees based on the value of the column JOB_ID, using the following data:

DECODE(job_id, 'AD_PRES','A',

1 Find the highest, lowest, sum, and average salary of all employees Label the columns Maximum, Minimum, Sum, and Average, respectively Round your results to the nearest whole number

SELECT ROUND(MAX(salary)) "Maximum", ROUND(MIN(salary)) "Minimum", ROUND(SUM(salary)) "Sum", ROUND(AVG(salary)) "Average"

2 Modify the query in Ex1 to display the minimum, maximum, sum, average salary for each job type

SELECT job_id, ROUND(MAX(salary)) "Maximum", ROUND(MIN(salary))

"Minimum", ROUND(SUM(salary)) "Sum", ROUND(AVG(salary)) "Average"

3 Determine the number of managers without listing them Label the column Number of Managers Hint: Use the MANAGER_ID column to determine the number of managers

SELECT COUNT(DISTINCT manager_id)"Number of Manager"

Generate a report that lists each manager's number alongside the salary of their lowest-paid employee, ensuring to omit any employees with unknown managers Additionally, exclude groups where the minimum salary is $6,000 or lower, and sort the final results in descending order based on salary.

SELECT manager_id, MIN(salary)

WHERE manager_id is NOT NULL

ORDER BY MIN(salary) DESC;

Practice 1: The HR department needs a report of all employees Write a query to display the last name, department number, and department name for all employees

SELECT e.last_name, d.department_id, d.department_name

(A) Create a report to display employees’ last name and employee number along with their manager’s last name and manager number Label the columns Employee, Emp#, Manager, and Mgr#, respectively

SELECT e.last_name "Employee", e.employee_id "Emp#", m.last_name "Manager", m.manager_id "Mgr#"

ON (e.employee_id = m.manager_id);

(B) Modify part A to display all employees including King, who has no manager Order the results by the employee number

SELECT e.last_name "Employee", e.employee_id "Emp#", m.last_name "Manager", m.manager_id "Mgr#"

FROM employees e LEFT OUTER JOIN employees m

ON e.employee_id = m.manager_id

Practice 3: The HR department needs to find the names and hire for all employees who were hired before their managers, along with their managers’ names and hire dates

SELECT e.last_name , e.hire_date ,m.last_name , m.hire_date

ON (e.employee_id = m.manager_id)

WHERE e.hire_date < m.hire_date;

To retrieve the details of employees earning above the average salary, you can query the employee number, last name, and salary of all employees Additionally, focus on those working in departments that include at least one employee with a last name containing the letter 'u' This approach ensures that you highlight high-earning staff while filtering for specific departmental criteria.

SELECT employee_id, last_name, salary

WHERE salary > (SELECT AVG(salary) FROM employees)

AND department_id IN (SELECT department_id FROM employees

Practice 5: The HR department needs a report with the following specifications:

- Last name and department ID of all the employees from the EMPLOYEES table, regardless of whether or not they belong to a department

- Department ID and department name of all the departments from the DEPARTMENTS table, regardless of whether or not they have employees working in them

Write a compound query to accomplish this

SELECT last_name, department_id, TO_CHAR(NULL)

SELECT TO_CHAR(NULL), department_id, department_name

Generate a report that includes the employee IDs and job IDs of employees who have returned to their original job titles since being hired by the company, despite having changed positions in the interim.

SELECT employee_id, job_id FROM employees

SELECT employee_id, job_id FROM job_history;

Practice 7: The HR department needs a list of countries that have no department located in them

Display the country ID and the name of the countries Use set operators to create this report

SELECT country_id, country_name FROM countries

SELECT l.country_id, c.country_name FROM locations l

ON (l.country_id = c.country_id)

ON (d.location_id = l.location_id);

Practice 1: Write a query to display the following for those employees whose manager ID is less than 120:

- Job ID and total salary for every job ID for employee who report to the same manager

- Total salary of those managers

- Total salary of those managers, irrespective of the job IDs

SELECT manager_id, job_id, sum(salary)

GROUP BY ROLLUP(manager_id, job_id);

To analyze the output from the first question, create a query utilizing the GROUPING function to identify if the NULL values in the columns related to the GROUP BY expressions are a result of the ROLLUP operation This approach will help clarify the impact of ROLLUP on the data aggregation process.

SELECT manager_id MGR, job_id JOB, SUM(salary),

GROUPING(manager_id), GROUPING(job_id)

GROUP BY ROLLUP(manager_id,job_id);

Practice 3: Write a query to display the following for those employees whose manager ID is less than 120:

- Job and total salaries for every job for employees who report to the same manager

- Total salary of those managers

- Cross-tabulation values to display the total salary for every job, irrespective of the manager

- Total salary irrespective of all job titles

SELECT manager_id,job_id, SUM(salary)

GROUP BY CUBE(manager_id,job_id);

Practice 4: Using GROUPING SETS, write a query to display the following groupings:

- department_id, manager_id, job_id

The query should calculate the sum of the salaries for each of these groups

SELECT department_id, manager_id, job_id, SUM(salary)

GROUP BY GROUPING SETS((department_id,manager_id,job_id),

Kiểm tra 1 sinh viên đã đủ điều kiện tốt nghiệp chưa biết rằng các kiều sinh viên tốt nghiệp là:

1 Tích lũy đủ số tín chỉ

2 Điểm phẩy tốt nghiệp không nhỏ hơn 1.0, biết bảng đổi điểm như sau:

Thang điểm 4 Điểm chữ Điểm số ĐẠT

Bước 1: Tạo view chuyển từ điểm chữ sang điểm số lấy từ bảng takes

CREATE OR REPLACE VIEW takes_number AS

SELECT id, course_id, sec_id, semester, year,

Bước 2: Tạo view chỉ chưa điểm cao nhất của 1 môn khi sinh viên học cải thiện hoặc học lại

CREATE OR REPLACE VIEW takes_bester AS

SELECT id, course_id, MAX(point) count

GROUP BY id, course_id;

Bước 3: Xây dựng một stored procedure nhận đầu vào là mã sinh viên và kiểm tra xem sinh viên đó có đủ điều kiện tốt nghiệp hay không, với tiêu chí tích lũy ít nhất 128 tín chỉ và CPA đạt từ 1 trở lên.

CREATE OR REPLACE PROCEDURE SP_Bai1( student_id IN VARCHAR) AS tot_cred number;

SELECT tot_cred INTO tot_cred

SELECT SUM(t.count * c.credits) / SUM(c.credits)

JOIN course c ON t.id = student_id

AND t.course_id = c.course_id;

If the CPA is less than 1.0, the system will output that the conditions for graduation are not met, displaying the average score Conversely, if the CPA is 1.0 or higher, it will indicate that the graduation conditions are satisfied, along with the average score.

ELSE dbms_output.put_line('Khong du dieu kien tot nghiep So tin chi tich luy la:' || tot_cred); END IF;

EXCEPTION when no_data_found Then dbms_output.put_line('Ma so sinh vien khong dung:' || student_id); end;

Kết quả: thử với id_student = 93004 exec SP_Bai1(93004);

Thủ tục SP_LOC_DU_LIEU cho phép người dùng nhập tên trường và giá trị tương ứng để lọc dữ liệu Ví dụ, khi sử dụng SP_LOC_DU_LIEU với tham số 'dept_name' và 'Physics', hệ thống sẽ trả về dữ liệu đã được lọc theo giá trị 'Physics' trong trường 'dept_name'.

Bảng kết quả bao gồm các thông tin quan trọng như Mã sinh viên, Họ tên sinh viên, Năm học, Kỳ học, Khóa học, Thời gian học, Phòng học, Giảng viên và Khoa viện.

Bước 1: Tạo view để lấy dữ liệu theo yêu cầu đề bài

CREATE OR REPLACE VIEW Temp AS

The query retrieves essential details about students, including their ID and name, along with their academic year and semester It also provides information about the course title, the schedule including day, start and end times, room number, and building Additionally, the instructor's name is included in the results.

JOIN course c ON se.course_id = c.course_id

JOIN teaches te ON se.course_id = te.course_id

AND se.sec_id = te.sec_id

AND se.semester = te.semester

AND se.year = te.year

JOIN instructor i ON te.id = i.id

JOIN takes ta ON se.sec_id = ta.sec_id

AND se.semester = ta.semester

AND se.year = ta.year

AND se.course_id = ta.course_id

JOIN student st ON ta.id = st.id

LEFT JOIN time_slot ts ON se.time_slot_id = ts.time_slot_id;

Bước 2: Tạo thủ tục lọc dữ liệu

CREATE OR REPLACE PROCEDURE SP_BAI2(field_name IN VARCHAR2, field_value IN VARCHAR, mycursor OUT SYS_REFCURSOR)

BEGIN str_query := 'SELECT * FROM Temp WHERE ' || field_name || ' LIKE ''%' || field_value || '%'''; OPEN mycursor FOR str_query;

DBMS_OUTPUT.PUT_LINE('Khong ton tai truong du lieu: ' || field_name);

Kết quả: Thử với trường dữ liệu: field_name = 'ID' field_value = '93004'

Thủ tục SP_LOC_DU_LIEU cho phép nhập vào một biến kiểu table với hai trường: tên trường và giá trị của trường Kết quả trả về là dữ liệu đã được lọc theo danh sách các giá trị của các trường dữ liệu tương ứng.

Bước 1: Tạo object mới gồm 2 trường field_name và field_value

CREATE OR REPLACE TYPE my_object

AS OBJECT(field_name VARCHAR(50), field_value VARCHAR(50));

Bước 2: Tạo kiểu dữ liệu dạng table cho object đã tạo

AS TABLE OF my_object;

Bước 3: Tạo thủ tục theo dạng bảng

CREATE OR REPLACE PROCEDURE SP_BAI3 (mytable IN my_table, mycursor OUT SYS_REFCURSOR)

BEGIN str_query := 'SELECT * FROM Temp WHERE 1=1';

FOR i IN mytable.FIRST mytable.LAST

LOOP str_query := str_query || ' AND ' || mytable(i).field_name ||' LIKE ''%' || mytable(i).field_value || '%'' ';

OPEN mycursor FOR str_query;

DBMS_OUTPUT.PUT_LINE('Dau vao khong hop le!');

Sinh viên A muốn học môn ‘Mobile Computing’ hỏi A cần phải học qua những môn gì? Đầu tiên ta tìm mã môn học có tên ‘Mobile Computing’

SELECT course_id FROM course

Sau đó ta chạy truy vấn phân cấp để tìm những môn trước:

CONNECT BY PRIOR prereq_id = course_id;

Kết quả với môn có course_id = 612 là:

Tức là để học môn có mã 612 cần phải học qua môn có mã 123

Kết quả với môn có mã 810:

CONNECT BY PRIOR prereq_id = course_id;

Tức là để học môn có mã 810 cần phải học qua môn có mã 966.

Để viết thủ tục tra cứu kết quả học tập của sinh viên, đầu vào sẽ là mã sinh viên Kết quả đầu ra bao gồm mã sinh viên, tên sinh viên, số tín chỉ tích lũy, điểm trung bình học kỳ và điểm trung bình tích lũy theo từng học kỳ.

Sử dụng view takes_number của bài 2

CREATE OR REPLACE PROCEDURE SP_BAI6(student_id NUMBER, mycursor OUT sys_refcursor )

SELECT t.id, s.name, t.semester, t.year,

ROUND( SUM( t.point * c.credits ) / SUM( c.credits), 2 ) GPA,

ROUND((SELECT SUM(MAX( t1.point) * c1.credits) /SUM( c1.credits ) FROM takes_number t1,course c1

AND t1.course_id = c1.course_id

GROUP BY t1.course_id,c1.credits), 2) AS CPA,

AND t1.course_id = c1.course_id

GROUP BY t1.id) total_cred

JOIN course c ON c.course_id = t.course_id

GROUP BY t.id,s.name, t.semester, t.year

ORDER BY t.year, t.semester DESC;

Kết quả: Thử với sinh viên có ID = 93004;

Để đánh giá kết quả học tập của một sinh viên, cần thực hiện thủ tục với đầu vào là mã sinh viên Kết quả đầu ra sẽ bao gồm xếp hạng trình độ sinh viên và xếp hạng học lực của sinh viên, từ đó phản ánh chính xác mức độ học tập và thành tích của sinh viên trong quá trình học tập.

Step 1: From the view takes_bester, we create a new view that includes the student ID, course ID, highest exam score, and credit hours The SQL command used is: create or replace view vw_bai1_Ultimate AS.

SELECT vw.id, vw.course_id, vw.FinalScore, cs.credits from takes_bester vw join course cs on vw.course_id = cs.course_id order by vw.id;

Bươc 2: Ta tạo view chỉ chữa những bản ghi có điểm lần học cao nhất >0.5 (hay điểm chữ >C-) tức là chỉ lấy những môn đạt

CREATE OR REPLACE VIEW vw_bai1_PassOnly as

SELECT * FROM vw_bai1_Ultimate WHERE FinalScore > 0.5;

Bước 3: Tạo thủ tục để kiểm tra học lực và trình độ của sinh viên đó create or replace procedure sp_bai7_2(student_id IN VARCHAR)

The SQL code calculates the total credits and cumulative grade point average (CPA) for a student based on their ID It retrieves the sum of credits and computes the CPA by dividing the weighted score by the total credits Additionally, it assesses the total credits earned to determine the student's academic level, categorizing them into five levels based on the total credits: less than 32 indicates level 1, 32 to 63 level 2, 64 to 95 level 3, 96 to 127 level 4, and over 128 level 5 The CPA is also evaluated, with scores below 1 indicating poor performance, between 1 and 2.0 as weak, and between 2.0 and 2.5 as average.

The code snippet categorizes students' academic performance based on their CPA scores If the CPA is between 2.5 and 3.2, it outputs 'Average performance'; for a CPA between 3.2 and 3.6, it indicates 'Good performance'; and for a CPA between 3.6 and 4.0, it reflects 'Excellent performance'.

Kết quả: exec sp_bai7_2(93004);

Đánh chỉ mục các bảng takes, student, advisor So sánh tốc độ truy vấn sau khi đã thực hiện đánh chỉ mục

We created additional tables—takes_test_index, student_test_index, and advisor_test_index—as copies of the original takes, student, and advisor tables to experiment with indexing The tables were generated using the following SQL commands: `create table takes_test_index as select * from takes;`, `create table student_test_index as select * from student;`, and `create table advisor_test_index as select * from advisor;`.

Tạo các index mới cho các bảng vừa tạo:

CREATE INDEX idx_takes_grade on takes_test_index(grade);

CREATE INDEX idx_student_ID ON student(id);

CREATE INDEX idx_advisor_ID ON advisor(id);

Khi truy vấn trên bảng takes_test_index theo trường grade, tốc độ đã có cải thiện so với bảng takes ban đầu select * from takes where grade = 'A';

So với: select * from takes_test_index where grade = 'A';

Ngày đăng: 14/02/2022, 13:23

TỪ KHÓA LIÊN QUAN

w