PL/SQL面试题汇总

1. What is the purpose of the PL/SQL language?

PL/SQL is an extension of SQL.

SQL is non-procedural.

PL/SQL is a procedural language designed by Oracle to overcome the limitations that exist in SQL.

2. Say True or False . IF False , explain why.

Routines written in PL/SQL can be called in Oracle call interface, Java, Pro*C/C++, COBOL etc.     (True)

 3. State few notable characteristics of PL/SQL.

  • Block-structured language
  • Stored procedures help better sharing of application
  • Portable to all environments that support Oracle
  • Integration with the Oracle data dictionary.

4. Name few schema objects that can be created using PL/SQL?

  • Storage procedures and functions
  • Packages
  • Triggers
  • Cursors

5. State some features or programming constructs supported by PL/SQL.

  • Variable and constants
  • Embedded SQL support
  • Flow control
  • Cursor management
  • Exception handling
  • Stored procedures and packages
  • Triggers

6.What are the three basic sections of a PL/SQL block?

  • Declaration section
  • Execution section
  • Exception section

7.What is wrong in the following assignment statement?

balance = balance + 2000;

Use of wrong assignment operator. The correct syntax is: balance := balance + 2000;

8.Write a single statement that concatenates the words ‘Hello’ and ‘World’ and assign it in a variable named greeting.

greeting := ‘Hello’ || ‘World’;

9.What is the purpose of %type data type? Explain with example.

It assigns a variable the same data type used by the column, for which the variable is created. For example,

dcode := dept.detpno%type;

The variable dcode is created with the same data type as that of the deptno column of the dept table.

10.What is the purpose of %rowtype data type? Explain with example.

It declares a composed variable that is equivalent to the row of a table. After the variable is created, the fields of the table can be accessed, using the name of this variable.

For example

emptype := emp%rowtype;

name := emptype.empname;

11.What is a PL/SQL package?

A package is a file that groups functions, cursors, stored procedures, and variables in one place.

12.What is a trigger?

A trigger is a PL/SQL program that is stored in the database and executed immediately before or after the INSERT, UPDATE, and DELETE commands.

 

13. What are the PL/SQL cursors?

Oracle uses workspaces to execute the SQL commands. In other words, when Oracle processes a SQL command, it opens an area in the memory called Private SQL Area. A cursor is an identifier for this area. It allows programmers to name this area and access it’s information.

PL/SQL游标用于在查询结果集上迭代的数据库对象。它可以理解为一种指向查询结果的句柄,可以通过游标提取和处理这些结果集。在PL/SQL中,有两种类型的游标:显式游标和隐式游标。

显式游标需要程序员进行显式的打开、读取和关闭操作。通过显式游标,我们可以逐行或批量地处理查询结果集,进行数据处理和计算等操作

隐式游标则是由PL/SQL自动创建和管理的游标。它适用于一些简单的数据操作,如单行查询等。

在实际工作中,通常使用显式游标来处理复杂的数据操作,如数据分析、数据处理和数据转换等。使用游标可以使数据操作更加灵活和高效,同时也能够提高程序的可维护性和可读性。但需要注意,过多地使用游标会影响程序的性能,因此需要合理地使用游标来平衡性能和可维护性的要求。

14. What is returned by the cursor attribute SQL%ROWCOUNT?

It returns the number of rows that are processed by a SQL statement.

15.What is returned by the cursor attribute SQL%FOUND?

It returns the Boolean value TRUE if at least one row was processed.

16.从 employees 表中检索员工的姓名和工资信息,并将它们打印出来。 使用PL/SQL程序实现。

当需要按行处理一个结果集时,可以使用 PL/SQL 游标。

DECLARE
  -- 定义游标
  CURSOR emp_cursor IS
    SELECT first_name, last_name, salary
    FROM employees;
    
  -- 定义变量用于存储每一行的数据
  emp_first_name VARCHAR2(50);
  emp_last_name VARCHAR2(50);
  emp_salary NUMBER(8,2);
BEGIN
  -- 打开游标
  OPEN emp_cursor;
  
  -- 循环遍历游标的每一行,将每行的数据存储到变量中
  LOOP
    FETCH emp_cursor INTO emp_first_name, emp_last_name, emp_salary;
    EXIT WHEN emp_cursor%NOTFOUND;
    
    -- 处理每行数据,这里只是简单地打印出来
    DBMS_OUTPUT.PUT_LINE(emp_first_name || ' ' || emp_last_name || ': ' || emp_salary);
  END LOOP;
  
  -- 关闭游标
  CLOSE emp_cursor;
END;

16.What is returned by the cursor attribute SQL%NOTFOUND?

It returns the Boolean value TRUE if no rows were processed.

17. Which command/commands allow iteration a use of loops in a PL/SQL block?

LOOP command, FOR.. LOOP command, WHILE command.

18.What is the difference in execution of triggers and stored procedures?

A trigger is automatically executed without any action required by the user, whereas, a stored procedure needs to be explicitly invoked.

触发器和存储过程是Oracle数据库中两种不同的程序单元,它们的作用和使用场景有所不同。

触发器是一种特殊类型的PL/SQL程序单元,可以自动执行某些操作,如在表中插入、更新或删除数据时执行其他操作。触发器与表关联,当满足一定条件时自动触发执行。触发器通常用于执行诸如数据验证、日志记录、数据同步等操作。

存储过程是一种可重复使用的程序单元,它包含一组SQL语句和过程控制语句,可以在需要时调用执行。存储过程可以接受参数、返回结果集,并支持流程控制语句、条件语句、循环语句等。存储过程通常用于实现复杂的业务逻辑和数据操作,并且可以提高应用程序的性能和安全性。

因此,触发器和存储过程的主要区别在于使用场景和执行方式触发器主要用于自动化处理与数据表有关的操作,而存储过程则用于实现复杂的业务逻辑和数据操作并且可以被其他程序或应用程序调用执行

 

19.什么是PL/SQL 触发器?

PL/SQL触发器是一段预定义的PL/SQL代码,它在一个特定的事件发生时自动执行。事件可以是数据插入、更新或删除。当这些事件发生时,触发器可以用来验证数据或者执行特定的操作,例如记录修改的信息调用其他程序触发器可以在表或视图上定义,并且在特定的操作发生时自动激活可以定义多个触发器来响应同一个事件

20.PL/SQL触发器的使用场景?

L/SQL触发器可以在数据库表的插入、更新、删除操作发生时自动执行一些特定的代码逻辑。通常,PL/SQL触发器用于以下场景:

  1. 数据完整性约束:在执行插入、更新或删除操作时,可以使用触发器来强制执行特定的数据完整性约束条件,例如,检查外键约束、检查唯一性、限制值范围等。
  2. 数据审计:在执行插入、更新或删除操作时,可以使用触发器来记录特定的审计信息,例如,记录谁、什么时间和在哪个操作执行了数据更改
  3. 自动化数据处理:在执行插入、更新或删除操作时,可以使用触发器来自动执行一些数据处理逻辑,例如,自动计算相关数据的值、自动更新其他表中的数据等。
  4. 数据同步:在执行插入、更新或删除操作时,可以使用触发器来自动同步相关的数据到其他表或数据库中,例如,同步相关数据到备份数据库或数据仓库中
     

    触发器在数据库表上注册,以便在表的特定事件发生时自动执行。触发器可以在触发事件发生之前或之后执行,并且可以在每行或每个语句级别上触发执行。

    触发器有两种类型:行级触发器和语句级触发器。行级触发器在表的每一行上执行,而语句级触发器会在每个SQL语句上执行一次而不管该语句将影响多少行。

    以下是一个简单的示例,展示如何创建一个简单的行级触发器

CREATE OR REPLACE TRIGGER my_trigger
AFTER INSERT ON my_table
FOR EACH ROW
BEGIN
  -- do something here
END;

在上面的示例中,当my_table表插入一条新记录时,触发器会自动执行。可以在BEGINEND之间编写PL/SQL代码来完成触发器的逻辑。在这个例子中,触发器被定义为行级触发器,因为它使用了FOR EACH ROW子句。

实际工作中,PL/SQL触发器可以用于许多用途,如自动记录更改日志、在数据插入或更新时验证数据完整性在某些条件下自动更新相关数据等。在使用触发器时,需要仔细考虑其逻辑,以确保其行为符合预期。

语句级触发器:
这个触发器会在 my_table 表插入一行后触发,并向控制台输出一行信息。这个触发器是语句级触发器,因为它是在整个 INSERT 语句完成之后触发的,而不是在每一行插入时都触发。

CREATE OR REPLACE TRIGGER my_trigger
AFTER INSERT ON my_table
BEGIN
  DBMS_OUTPUT.PUT_LINE('A row has been inserted into my_table.');
END;
/

21.如何操作触发器?

Oracle数据库中,可以使用 ALTER TRIGGER 语句来启用或禁用触发器。语法如下:

  • 启用触发器:ALTER TRIGGER trigger_name ENABLE;
  • 禁用触发器:ALTER TRIGGER trigger_name DISABLE;

其中,trigger_name 是指要操作的触发器的名称。

如果需要在创建触发器时就指定其状态(启用或禁用),可以使用以下语法:

CREATE [OR REPLACE] TRIGGER trigger_name 
    {BEFORE | AFTER} {INSERT | UPDATE | DELETE} 
    ON table_name 
    [FOR EACH ROW]
    [WHEN condition]
    [ENABLE/DISABLE]
    [trigger_body]

其中,ENABLE 和 DISABLE 可以在 CREATE TRIGGER 语句中指定触发器的状态。如果不指定,默认为启用状态。

22.What is the difference between a function and a stored procedure?

A function returns a value and a stored procedure doesn’t return a value.

23.How do you declare a user-defined exception?

User defined exceptions are declared under the DECLARE section, with the keyword EXCEPTION. Syntax −

EXCEPTION;

在 PL/SQL 中,可以使用 EXCEPTION 关键字来声明用户定义异常。下面是一个示例:

DECLARE
  my_exception EXCEPTION;
BEGIN
  RAISE my_exception;
EXCEPTION
  WHEN my_exception THEN
    DBMS_OUTPUT.PUT_LINE('My custom exception was raised');
END;

在上面的示例中,使用 EXCEPTION 关键字声明了一个名为 my_exception 的用户定义异常。在 BEGIN 块中,通过 RAISE 语句引发了这个异常。在 EXCEPTION 块中,当 my_exception 异常被捕获时,会执行自定义的异常处理程序

24. What do you understand by explicit cursors?

Explicit cursors are a type of cursor used in PL/SQL programming language to fetch and process rows returned by a SELECT statement. Unlike implicit cursors, which are automatically created by Oracle whenever a SQL statement is executed, explicit cursors are explicitly defined and explicitly manipulated by the programmer.

Explicit cursors are created in the declaration section of a PL/SQL block, and their lifecycle involves several steps: opening the cursor, fetching rows from it, and finally closing the cursor. They provide more control and flexibility over the result set, allowing for operations such as scrolling, filtering, and aggregation.

In general, explicit cursors are useful in situations where the result set needs to be manipulated before being processed, or when multiple fetch operations are needed to retrieve all the required rows.


显式游标是一种由程序员显式定义、打开、获取数据和关闭的游标类型。相对于隐式游标,程序员需要显式地声明并指定游标的属性,包括游标名、游标的SQL语句、游标的属性(例如游标的选择条件和排序方式)、游标的参数等等。

显式游标可以在程序中实现更精细的控制,例如可以通过设置游标的参数来限制获取的数据量,也可以通过游标的属性来进行数据排序等等。但是相对于隐式游标,显式游标需要程序员手动进行游标的打开、获取数据和关闭操作,需要更多的代码实现,但也提供了更加灵活的数据访问方式。

25.What are the steps that need to be performed to use an explicit cursor? Discuss briefly.

The steps that need to be performed on explicit cursor are −

  • DECLARE − assigns a name to the cursor and defines the structure of query within it.

  • OPEN − executes the query, whereby the rows returned by the query are available for fetching.

  • FETCH − assigns values from the current row (cursor position) into specified variables.

  • CLOSE − releases the memory space.

26.PL/SQL packages usually have two parts. What are these two parts?

PL/SQL packages have two parts −

  • Specification part − where the interface to the application are defined.

  • Body part − where the implementation of the specification are defined.

PL/SQL包是一种可重用的程序单元,它由一组相关的过程、函数、变量、游标和触发器组成,可以封装和隐藏复杂的业务逻辑。包中的子程序可以被其他程序单元调用,从而实现了代码的复用和封装。

包的主要功能包括:

  1. 封装:可以封装和隐藏业务逻辑,提高代码的安全性和可维护性。
  2. 可重用性:包中的程序单元可以在其他程序中多次调用,提高代码的复用性。
  3. 性能优化:可以使用包来预编译一些常用的程序单元,提高执行效率。
  4. 数据隔离:包中的变量和常量可以作为共享变量使用,也可以作为私有变量使用,实现数据的隔离和封装。

在实际应用中,可以使用包来封装和隐藏复杂的业务逻辑,提高代码的可读性和可维护性。同时,可以使用包来提高代码的性能,预编译一些常用的程序单元,避免重复编译和解析。此外,包还可以作为共享变量的容器,实现数据的隔离和封装。

27.Which command(s) are used for creating PL/SQL packages?

 CREATE PACKAGE command is used for creating the specification part. CREATE PACKAGE BODY command is used for creating the body part.

28.How do you refer to the types, objects and subprograms declared within a package? 

To refer to the types, objects, and subprograms declared within a package in PL/SQL, you need to qualify their names with the package name.

For example, suppose you have a package called my_package that declares a function called my_function. To call the function, you would need to reference it using the package name and function name as follows:

result := my_package.my_function(arg1, arg2);

Similarly, if the package declares a type called my_type, you can refer to it using the package name and type name as follows:

declare
    my_var my_package.my_type;
begin
    -- do something with my_var
end;

By qualifying the names with the package name, you can avoid naming conflicts and make your code more modular and organized.

29.Which command is used to delete a package?

The DROP PACKAGE command is used to delete a package in PL/SQL. The syntax is as follows:

DROP PACKAGE package_name;

Where package_name is the name of the package to be deleted. This command will remove the package specification and body from the database.

30. What is the difference between implicit and explicit cursors?

Oracle implicitly declares a cursor to all the DDL and DML commands that return only one row. For queries returning multiple rows, an explicit cursor is created.

隐式游标和显式游标是两种不同的游标类型,它们在以下方面有所区别:

  1. 声明方式不同:隐式游标是由Oracle自动分配的游标,而显式游标是由程序员明确声明并分配的游标。
  2. 处理方式不同:隐式游标由Oracle自动管理,通常用于处理单行记录;而显式游标由程序员控制,可以处理多行记录。
  3. 可维护性不同:显式游标可以进行更精细的控制,程序员可以根据需要在游标生命周期内执行不同的操作,从而更好地维护和管理游标。
  4. 开销不同:隐式游标的内存开销比显式游标要小,但在处理大量数据时,显式游标的性能通常更好。

总之,隐式游标和显式游标在用法、处理方式、可维护性和性能等方面都有所不同,程序员应根据实际需要选择适合的游标类型。

31. 关于触发器的实际应用有哪些?

  1. 日志记录:在 INSERT、UPDATE 或 DELETE 操作时,使用触发器将操作的详细信息记录到日志表中。

  2. 强制执行业务规则:在 INSERT、UPDATE 或 DELETE 操作时,使用触发器确保输入的数据满足业务规则,如果不满足,则拒绝操作。

  3. 数据同步:在 INSERT、UPDATE 或 DELETE 操作时,使用触发器将操作同步到其他数据库中,以保证数据一致性。

  4. 自动编号:在 INSERT 操作时,使用触发器自动为记录分配一个唯一的编号。

  5. 数据修改审计:在 UPDATE 或 DELETE 操作时,使用触发器记录修改前后的数据,以便于审计和追踪数据修改历史。

这些实例仅是触发器应用的一部分,实际上,触发器可以用于多种情况,具体应用根据业务需求而定。

32

你可能感兴趣的:(程序员面试题基地,sql,oracle,数据库,PL/SQL)