University of Electronic Science and Technology of China Database and Software Engineering III

Applicable to network engineering and Internet of Things majors

Table of contents

1. Experimental purpose

2. Experimental content

3. Experimental software

4. Experimental procedures and data recording

1. Subquery

2. Pseudo column

3. Database objects

4. Blocks, stored procedures and triggers

5. Experimental conclusions and questions

Experimental results:

Thought questions:

6. Summary and experience

7. Suggestions for improving the experimental process, methods and means


1. Experimental purpose

1. Learn subqueries and common functions;

2. Understand the operation of database views;

3. Learn PL/SQL language to implement operations on database stored procedures, triggers, etc.

2. Experimental content

 Use student users to connect to the Oracle database in SQL Developer, and use the EMP table (employee table), DEPT table (department table) and SALGRADE table (salary grade table) imported in Experiment 2 to implement database subqueries, views, The operation process of stored procedures, triggers, etc.

3. Experimental software

Oracle Database, SQL Developer

4. Experimental procedures and data recording

1. Subquery

1) Query the names, jobs, and salaries of employees under the sales department (SALES).

SELECT NAME,JOB,SAL FROM EMP

WHERE DEPTNO=(SELECT DEPTNO FROM WHERE DNAME='SALES');

The result is as follows:

figure 1

2) Query the name, job and salary of employees in the EMP table whose salary is lower than that of any salesperson (SALESMAN) (less than the maximum value in the salesperson salary query result).

SELECT ENAME,JOB,SAL FROM EMP

WHERE SAL<ANY(SELECT SAL FROM EMP WHERE JOB='SALESMAN');

The result is as follows:

figure 2

2. Pseudo column

1) Query the names, jobs and salaries of the top 5 employees in the employee table.

SELECT ROWNUM,ENAME,JOB,SAL FROM EMP WHERE ROWNUM<=5;

The result is as follows:

image 3

2) Query the names, salaries and wages of the top 5 employees with the highest salary.

SELECT ROWNUM,T.* FROM

(SELECT ENAME,JOB,SAL

FROM EMP ORDER BY SAL DESC) T

WHERE ROWNUM<=5;

The result is as follows:

Figure 4

3. Database objects

1) Create a read-only view based on the EMP table and DEPT table. This view only contains the employee number, name, job, joining date and department name, and hides the employee's salary, bonus, department number and other information.

CREATE OR REPLACE VIEW EMPDETAIL

AS

SELECT EMPNO,ENAME,JOB,HIREDATE,DNAME

FROM EMP JOIN DEPT ON EMP.DEPTNO=DEPT.DEPTNO

WITH READ ONLY;

Query the EMPDETAIL view created:

The result is as follows:

Figure 5

2) Create a unique index for the ENAME column of the EMP table, create a normal index for the salary column of the EMP table, change the JOB column to lowercase first and then create the index.

CREATE UNIQUE INDEX UQ_ENAEM_IDX ON EMP(ENAME);

CREATE INDEX IDX_SAL ON EMP(SAL);

CREATE INDEX IDX_JOB_LOWER ON EMP(LOWER(JOB));

 Use the following SELECT command to view the indexes of table EMP:

SELECT INDEX_NAME,INDEX_TYPE,TABEL_OWNER,TABLE_NAME,UNIQUENESS

FROM USER_INDEXES WHERE TABLE_NAME='EMP';

The result is as follows:

Figure 6

4. Blocks, stored procedures and triggers

1) Declare a variable named sname

DECLARE

SNAME VARCHAR2(20):='jerry';

BEGIN

SNAME:SNAME||'and tom';

DBMS_OUTPUT.PUT_LINE(sname);

END;

When outputting data in SQL *Plus, there may be no results displayed. You can use the command: set serveroutput on to set the output to SQL a> Plus console. *

The output result is as shown in the figure

Figure 7

2) Create a stored procedure that displays the total number of employees.

CREATE OR REPLACE PROCEDURE EMP_COUNT

AS

V_TOTAL NUMBER(10);

BEGIN

SELECT COUNT(*) INTO V_TOTAL FROM EMP;

DBMS_OUTPUT.PUT_LINE('The total number of employees is:'||V_TOTAL);

END;

The stored procedure needs to click the "Run" button of SQL Developer to execute. If there is an error, it will display: "Warning: The created procedure has compilation errors." If errors exist, modify the script until no errors occur. If the compilation result is correct, "Process compiled" will be displayed.

Call the stored procedure, enter the following statement in the worksheet and execute it:

The result is as follows:

Figure 8

 3) Make a database trigger. Log all INSERT, UPDATE, and DELETE operations on EMP tables.

CREATE TABLE EMP_LOG(

who varchar2(30),

when date);

CREATE OR REPLACE TRIGGER EMP_OP

BEFORE INSERT OR UPDATE OR DELETE

ON EMP

BEGIN

INSERT INTO EMP_LOG (who,when)

VALUES(user,sysdate);

END EMP_OP;

Execute the EMP table update operation and view the log table. The results are as follows:

Figure 9

5. Experimental conclusions and questions

Experimental results:

SQL language changes must be used flexibly, and the code use and application of queries, views, and storage triggers must be more flexible.

For some queries, you need to use Cartesian correlation operations in the database to solve them.

Thought questions:

  1. Find the names, jobs, and salaries of employees who earn more than all salespeople. Please write the SELECT command and give a screenshot of the query results

 SELECT ENAME,JOB,SAL FROM EMP

WHERE SAL>ALL (SELECT SAL FROM EMP WHERE JOB='SALESMAN');

The query results are as follows:

Figure 10

2. Query the records between No. 5 and No. 10 in the EMPDETAIL view. Please write the SELECT command and provide a screenshot of the query results.

SELECT *FROM (SELECT rownum no, EMPNO, ENAME, JOB, HIREDATE, DNAME FROM EMPDETAIL WHERE rownum<=10 ORDER BY rownum ASC) WHERE no>=5;

The result is as follows:

Figure 11

3. Create a stored procedure to display the name of the employee with the lowest salary. Please write the code to create the stored procedure and provide a screenshot of the execution result of the stored procedure.

CREATE OR REPLACE PROCEDURE SAL_LOW

AS

V_LOW STRING(20);

BEGIN

SELECT ENAME INTO V_LOW FROM EMP WHERE ROWNUM=1 ORDER BY SAL ASC;

DBMS_OUTPUT.PUT_LINE('Minimum wage:'||V_LOW);

END;

The result is as follows:

Figure 12

6. Summary and experience

  1. During the experiment, you will encounter some command execution failures, and later you will find that there is an error in the usage of your own related commands.
  2. During the experiment, it is necessary to integrate and adapt the commands described in class.
  3. At the same time, you should be careful during the experiment, analyze and summarize every process where problems arise, and use classroom knowledge to solve them.

7. Suggestions for improving the experimental process, methods and means

1. In addition, some SQL language exercises can be added appropriately to master the knowledge learned.

2. It is recommended that the code should not be given for the relevant parts of the experimental steps, and students can write them themselves.

Guess you like

Origin blog.csdn.net/weixin_53284122/article/details/129219643