Oracle PLSQL编程手册(sql大全)
Oracle PL SQL 编程规范指南

Oracle PL/SQL 编程规范指南一、PL/SQL 编程规范之大小写就像在 SQL 中一样,PL / SQL 中是不区分大小写的。
其一般准则如下: 关键字(BEGIN, EXCEPTION, END, IF THEN ELSE,LOOP, END LOOP)、数据类型 (VARCHAR2, NUMBER)、内部函数(LEAST, SUBSTR)和用户定义的子程序(procedures, functions,packages),使用大写。
变量名以及 SQL 中的列名和表名,使用小写。
二、PL/SQL 编程规范之空白空白(空行和空格)在 PL/SQL 中如同在 SQL 中一样重要,因为它是提高代码可读性的一个重要因素。
换句话说,可以通过在代码中使用缩进来体现程序的逻辑结构。
以下是一些建议: 在等号或比较操作符的左右各留一个空格; 结构词(DECLARE, BEGIN, EXCEPTION, END,IF and END IF, LOOP and END LOOP) 居左排列。
另外,结构中的嵌套结构要缩进三个空格(使用空格键,而不是 Tab 键); 主要代码段之间用空行隔开; 把同一结构的不同逻辑部分分开写在独立的行, 即使这个结构很短。
例如, IF 和 THEN 被放在同一行, 而 ELSE 和 END IF 则放在独立的行。
三、PL/SQL 编程规范之命名约定使用以下前缀对于避免与关键字和表名列名相冲突是很有帮助的:v_变量名 con_常量名 i_输入参数名,o_输出参数名,io_输入输出参数名 c_游标名 或者 游标名_cur rc_ Ref Cursor 名 r_Record 名 或者 Record 名_rec FOR r_stud IN c_stud LOOP… FOR stud_rec IN stud_cur LOOP type_名称,名称_type (用户定义的类型) t_表名,表名_tab (PL/SQL 表) rec_Record 名,Record 名_rec (Record 变量) e_异常名 (用户定义的异常) 包的名称应该描述包内的存储过程和函数主要所完成的功能存储过程的名称应该描述该存储过程所执行的动作函数的名称应该描述所返回的变量例如:PACKAGE student_admin – admin 后缀可能是用于表示管理功能。
Oracle PL SQL编程教程说明书

About the T utorialPL/SQL is a combination of SQL along with the procedural features of programming languages. It was developed by Oracle Corporation in the early 90's to enhance the capabilities of SQL.PL/SQL is one of three key programming languages embedded in the Oracle Database, along with SQL itself and Java.This tutorial will give you great understanding on PL/SQL to proceed with Oracle database and other advanced RDBMS concepts.AudienceThis tutorial is designed for Software Professionals, who are willing to learn PL/SQL Programming Language in simple and easy steps. This tutorial will give you great understanding on PL/SQL Programming concepts, and after completing this tutorial, you will be at an intermediate level of expertise from where you can take yourself to a higher level of expertise.PrerequisitesBefore proceeding with this tutorial, you should have a basic understanding of software basic concepts like what is database, source code, text editor and execution of programs, etc. If you already have an understanding on SQL and other computer programming language, then it will be an added advantage to proceed.Copyright & DisclaimerCopyright 2018 by Tutorials Point (I) Pvt. Ltd.All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher.We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or inthistutorial,******************************************iT able of ContentsAbout the Tutorial (i)Audience (i)Prerequisites (i)Copyright & Disclaimer (i)Table of Contents ................................................................................................................................... i ii PL/SQL — OVERVIEW . (1)Features of PL/SQL (1)Advantages of PL/SQL (1)PL/SQL — ENVIRONMENT SETUP (3)Text Editor (14)PL/SQL — BASIC SYNTAX (15)PL/SQL — DATA TYPES (19)PL/SQL Scalar Data Types and Subtypes (19)PL/SQL Numeric Data Types and Subtypes (20)PL/SQL Character Data Types and Subtypes (21)PL/SQL Boolean Data Types (22)PL/SQL Datetime and Interval Types (22)PL/SQL Large Object (LOB) Data Types (23)PL/SQL User-Defined Subtypes (24)NULLs in PL/SQL (25)PL/SQL — VARIABLES (26)Variable Declaration in PL/SQL (26)Initializing Variables in PL/SQL (27)Variable Scope in PL/SQL (28)iiAssigning SQL Query Results to PL/SQL Variables (29)PL/SQL — CONSTANTS AND LITERALS (31)Declaring a Constant (31)The PL/SQL Literals (32)PL/SQL — OPERATORS (34)Arithmetic Operators (34)Relational Operators (36)[Comparison Operators (39)Logical Operators (44)PL/SQL Operator Precedence (46)PL/SQL — CONDITIONS (49)IF-THEN Statement (50)IF-THEN-ELSE Statement (53)IF-THEN-ELSIF Statement (55)CASE Statement (56)Searched CASE Statement (58)Nested IF-THEN-ELSE Statements (60)PL/SQL — LOOPS (62)Basic Loop Statement (63)WHILE LOOP Statement (65)FOR LOOP Statement (66)Reverse FOR LOOP Statement (68)Nested Loops (69)Labeling a PL/SQL Loop (71)The Loop Control Statements (72)iiiEXIT Statement (73)The EXIT WHEN Statement (75)CONTINUE Statement (77)GOTO Statement (80)PL/SQL — STRINGS (83)Declaring String Variables (83)PL/SQL String Functions and Operators (84)PL/SQL — ARRAYS (89)Creating a Varray Type (89)PL/SQL — PROCEDURES (94)Parts of a PL/SQL Subprogram (94)Creating a Procedure (95)Executing a Standalone Procedure (96)Deleting a Standalone Procedure (97)Parameter Modes in PL/SQL Subprograms (98)Methods for Passing Parameters (100)PL/SQL — FUNCTIONS (103)Creating a Function (103)Calling a Function (104)PL/SQL Recursive Functions (106)PL/SQL — CURSORS (108)Implicit Cursors (108)Explicit Cursors (110)Declaring the Cursor (112)Opening the Cursor (112)ivFetching the Cursor (112)Closing the Cursor (112)PL/SQL — RECORDS (114)Table-Based Records (114)Cursor-Based Records (115)User-Defined Records (116)PL/SQL — EXCEPTIONS (120)Syntax for Exception Handling (120)Raising Exceptions (121)User-defined Exceptions (122)Pre-defined Exceptions (123)PL/SQL — TRIGGERS (126)Creating Triggers (126)Triggering a Trigger (129)PL/SQL — PACKAGES (130)Package Specification (130)Package Body (130)Using the Package Elements (131)PL/SQL — COLLECTIONS (138)Index-By Table (139)Nested Tables (141)Collection Methods (144)Collection Exceptions (145)vPL/SQL — TRANSACTIONS (147)Starting and Ending a Transaction (147)Committing a Transaction (147)Rolling Back Transactions (148)Automatic Transaction Control (149)PL/SQL — DATE & TIME (150)Field Values for Datetime and Interval Data Types (150)The Datetime Data Types and Functions (151)The Interval Data Types and Functions (155)PL/SQL — DBMS OUTPUT (157)DBMS_OUTPUT Subprograms (157)PL/SQL — OBJECT-ORIENTED (160)Instantiating an Object (161)Member Methods (161)Using Map method (162)Using Order method (164)Inheritance for PL/SQL Objects (166)Abstract Objects in PL/SQL (168)viPL/SQL7The PL/SQL programming language was developed by Oracle Corporation in the late 1980s as procedural extension language for SQL and the Oracle relational database. Following are certain notable facts about PL/SQL:∙PL/SQL is a completely portable, high-performance transaction-processing language. ∙PL/SQL provides a built-in, interpreted and OS independent programming environment. ∙PL/SQL can also directly be called from the command-line SQL*Plus interface . ∙Direct call can also be made from external programming language calls to database. ∙PL/SQL's general syntax is based on that of ADA and Pascal programming language. ∙ Apart from Oracle, PL/SQL is available in TimesTen in-memory database and IBM DB2.Features of PL/SQLPL/SQL has the following features:∙PL/SQL is tightly integrated with SQL. ∙It offers extensive error checking. ∙It offers numerous data types. ∙It offers a variety of programming structures. ∙It supports structured programming through functions and procedures. ∙It supports object-oriented programming. ∙ It supports the development of web applications and server pages. Advantages of PL/SQLPL/SQL has the following advantages:∙ SQL is the standard database language and PL/SQL is strongly integrated with SQL. PL/SQL supports both static and dynamic SQL. Static SQL supports DML operations and transaction control from PL/SQL block. In Dynamic SQL, SQL allows embedding DDL statements in PL/SQL blocks.PL/SQL — OverviewPL/SQL8∙PL/SQL allows sending an entire block of statements to the database at one time. This reduces network traffic and provides high performance for the applications.∙PL/SQL gives high productivity to programmers as it can query, transform, and update data in a database.∙PL/SQL saves time on design and debugging by strong features, such as exception handling, encapsulation, data hiding, and object-oriented data types.∙Applications written in PL/SQL are fully portable.∙PL/SQL provides high security level.∙PL/SQL provides access to predefined SQL packages.∙PL/SQL provides support for Object-Oriented Programming.∙PL/SQL provides support for developing Web Applications and Server Pages.PL/SQL 9In this chapter, we will discuss the Environment Setup of PL/SQL. PL/SQL is not a stand-alone programming language; it is a tool within the Oracle programming environment. SQL* Plus is an interactive tool that allows you to type SQL and PL/SQL statements at the command prompt. These commands are then sent to the database for processing. Once the statements are processed, the results are sent back and displayed on screen.To run PL/SQL programs, you should have the Oracle RDBMS Server installed in your machine. This will take care of the execution of the SQL commands. The most recent version of Oracle RDBMS is 11g. You can download a trial version of Oracle 11g from the following link: Download Oracle 11g Express EditionYou will have to download either the 32-bit or the 64-bit version of the installation as per your operating system. Usually there are two files. We have downloaded the 64-bit version. You will also use similar steps on your operating system, does not matter if it is Linux or Solaris. ∙win64_11gR2_database_1of2.zip ∙ win64_11gR2_database_2of2.zipAfter downloading the above two files, you will need to unzip them in a single directory database and under that you will find the following sub-directories:Step 1Let us now launch the Oracle Database Installer using the setup file. Following is the first screen. You can provide your email ID and check the checkbox as shown in the following screenshot. Click the Next button.PL/SQL — Environment Setup10Step 2You will be directed to the following screen; uncheck the checkbox and click the Continue button to proceed.11Just select the first option Create and Configure Database using the radio button and click the Next button to proceed.12We assume you are installing Oracle for the basic purpose of learning and that you are installing it on your PC or Laptop. Thus, select the Desktop Class option and click the Next button to proceed.13Provide a location, where you will install the Oracle Server. Just modify the Oracle Base and the other locations will set automatically. You will also have to provide a password; this will be used by the system DBA. Once you provide the required information, click the Next button to proceed.14Again, click the Next button to proceed.15Click the Finish button to proceed; this will start the actual server installation.16This will take a few moments, until Oracle starts performing the required configuration.17Here, Oracle installation will copy the required configuration files. This should take a moment:18Once the database files are copied, you will have the following dialogue box. Just click the OK button and come out.19Upon installation, you will have the following final window.Final StepIt is now time to verify your installation. At the command prompt, use the following command if you are using Windows:You should have the SQL prompt where you will write your PL/SQL commands and scripts:PL/SQL20T ext EditorRunning large programs from the command prompt may land you in inadvertently losing some of the work. It is always recommended to use the command files. To use the command files: ∙Type your code in a text editor, like Notepad, Notepad+, or EditPlus, etc.∙Save the file with the .sql extension in the home directory.∙Launch the SQL*Plus command prompt from the directory where you created your PL/SQL file.∙Type @file_name at the SQL*Plus command prompt to execute your program.If you are not using a file to execute the PL/SQL scripts, then simply copy your PL/SQL code and right-click on the black window that displays the SQL prompt; use the paste option to paste the complete code at the command prompt. Finally, just press Enter to execute thecode, if it is not already executed.PL/SQL21In this chapter, we will discuss the Basic Syntax of PL/SQL which is a block-structured language; this means that the PL/SQL programs are divided and written in logical blocks of code. Each block consists of three sub-parts:Every PL/SQL statement ends with a semicolon (;). PL/SQL blocks can be nested within other PL/SQL blocks using BEGIN and END . Following is the basic structure of a PL/SQL block: PL/SQL — Basic Syntax22The 'Hello World' ExampleThe end; line signals the end of the PL/SQL block. To run the code from the SQL command line, you may need to type/ at the beginning of the first blank line after the last line of the code. When the above code is executed at the SQL prompt, it produces the following result: The PL/SQL IdentifiersPL/SQL identifiers are constants, variables, exceptions, procedures, cursors, and reserved words. The identifiers consist of a letter optionally followed by more letters, numerals, dollar signs, underscores, and number signs and should not exceed 30 characters.By default, identifiers are not case-sensitive . So you can use integer or INTEGER to represent a numeric value. You cannot use a reserved keyword as an identifier.The PL/SQL DelimitersA delimiter is a symbol with a special meaning. Following is the list of delimiters in PL/SQL:2324The PL/SQL CommentsProgram comments are explanatory statements that can be included in the PL/SQL code that you write and helps anyone reading its source code. All programming languages allow some form of comments.The PL/SQL supports single-line and multi-line comments. All characters available inside any comment are ignored by the PL/SQL compiler. The PL/SQL single-line comments start with the delimiter -- (double hyphen) and multi-line comments are enclosed by /* and */.When the above code is executed at the SQL prompt, it produces the following result:PL/SQL Program UnitsA PL/SQL unit is any one of the following:∙PL/SQL block∙Function∙Package∙Package bodyPL/SQL25∙Procedure∙Trigger∙Type∙Type bodyEach of these units will be discussed in the following chapters.PL/SQL26End of ebook previewIf you liked what you saw…Buy it from our store @ https://。
ORACLE-PLSQL编程详解--全8篇

ORACLE PL/SQL编程详解第一章:PL/SQL 程序设计简介SQL语言只是访问、操作数据库的语言,并不是一种具有流程控制的程序设计语言,而只有程序设计语言才能用于应用软件的开发。
PL /SQL是一种高级数据库程序设计语言,该语言专门用于在各种环境下对ORACLE数据库进行访问。
由于该语言集成于数据库服务器中,所以PL/SQL代码可以对数据进行快速高效的处理。
除此之外,可以在ORACLE数据库的某些客户端工具中,使用PL/SQL语言也是该语言的一个特点。
本章的主要内容是讨论引入PL/SQL 语言的必要性和该语言的主要特点,以及了解PL/SQL语言的重要性和数据库版本问题。
还要介绍一些贯穿全书的更详细的高级概念,并在本章的最后就我们在本书案例中使用的数据库表的若干约定做一说明。
SQL与PL/SQL什么是PL/SQLPL/SQL是 Procedure Language & Structured Query Language 的缩写。
ORACLE的SQL是支持ANSI(American national Standards Institute)和ISO92 (International Standards Organization)标准的产品。
PL/SQL是对SQL语言存储过程语言的扩展。
从ORACLE6以后,ORACLE的RDBMS附带了PL/SQL。
它现在已经成为一种过程处理语言,简称PL/SQL。
目前的PL/SQL包括两部分,一部分是数据库引擎部分;另一部分是可嵌入到许多产品(如C语言,JAVA语言等)工具中的独立引擎。
可以将这两部分称为:数据库PL/SQL和工具PL/SQL。
两者的编程非常相似。
都具有编程结构、语法和逻辑机制。
工具PL/SQL另外还增加了用于支持工具(如ORACLE Forms)的句法,如:在窗体上设置按钮等。
本章主要介绍数据库PL/SQL 内容。
PL/SQL的优点或特征有利于客户/服务器环境应用的运行对于客户/服务器环境来说,真正的瓶颈是网络上。
PLSQL速成手册—史上最全最新

SQL速成手册By 晨稳PLSQL目录PLSQL语言基础---------------------------------------------------------------------02 PLSQL流程控制---------------------------------------------------------------------04 PLSQL存储过程---------------------------------------------------------------------08 PLSQL触发器------------------------------------------------------------------------11 PLSQL函数---------------------------------------------------------------------------16 PLSQL游标---------------------------------------------------------------------------22 PLSQL索引---------------------------------------------------------------------------24 PLSQL异常处理--------------------------------------------------------------------28 PLSQL包的创建与管理----------------------------------------------------------31 PLSQL动态SQL--------------------------------------------------------------------36 PLSQL使用EXPLAINPLAN获取SQL语句执行计划---------------------43PL/SQL语言基础PL/SQL是过程化的SQL语言,是Oracle对SQL语言的扩展,在普通SQL语言上面增加了编程语言的特点,使得该语言不仅具有编程语言的特点,如循环、条件分支等,同时也具有对象编程语言的特点,如重载、继承等。
plsql 使用手册

PL/SQL 是Oracle 数据库中用于存储过程、函数、触发器和包等程序的编程语言。
以下是PL/SQL 使用手册的参考指南:1、连接Oracle 数据库:在开始编写PL/SQL 程序之前,您需要先连接到Oracle 数据库。
可以通过以下步骤连接到数据库:•运行PLSQL,将弹出数据库连接对话框。
•在对话框中选择要连接的Oracle 服务名,并输入用户名和密码。
•点击“OK”按钮进行连接。
2、登录信息保存功能设置:如果设置了登录信息保存功能,可以通过以下方式连接数据库,不必每次输入用户名和密码。
•进入PLSQL 后,在菜单区点击右键,出现PLSQL 配置界面。
•将“Store with password” 选中即可。
这样,第一次通过用户名/密码登录某数据库后,下次就不用再输入用户名/密码了。
3、切换数据库连接:在PLSQL 中,可以通过以下步骤切换到不同的数据库连接:•在菜单中选择“Change Database” 选项。
•在弹出的对话框中,选择要连接的数据库。
•点击“OK” 按钮完成切换。
4、编写PL/SQL 程序:在连接到数据库后,可以开始编写PL/SQL 程序。
以下是一些常见的PL/SQL 程序示例:•存储过程:用于封装复杂的SQL 查询和数据处理逻辑。
可以使用PL/SQL 编写一个或多个SQL 语句的集合,并将其封装在一个可重用的过程中。
•函数:用于计算并返回一个值。
可以编写一个或多个SQL 语句,将其封装在一个函数中,并使用输入参数来控制计算过程。
•触发器:用于在数据库中执行自动操作。
可以在特定的数据库事件(如插入、更新或删除记录)发生时触发自动执行的操作。
•包:用于封装多个PL/SQL 程序和逻辑单元。
可以将相关的存储过程、函数和数据类型封装在一个包中,以便更好地组织和管理代码。
5、执行SQL 语句:在PLSQL 中,可以使用以下步骤执行SQL 语句:•在菜单中选择“Execute” 或“Run” 选项。
plsql教程

plsql教程PL/SQL是一种与Oracle数据库一起使用的过程化编程语言。
它是操纵、定义和控制Oracle数据库对象的语言,并提供了一种编写存储过程、触发器、函数、包等数据库程序模块的方式。
PL/SQL的基本语法与SQL相似,可以执行SQL语句和存储过程的调用。
以下是一些常用的PL/SQL代码示例:1. 声明变量和常量:```DECLAREnum1 NUMBER := 10;text1 VARCHAR2(20) := 'Hello';constant1 CONSTANT NUMBER := 5;BEGIN-- 执行代码END;```2. 条件语句:```IF num1 > 0 THENNULL;ELSIF num1 = 0 THENNULL;ELSENULL;END IF;```3. 循环语句:```FOR i IN 1..5 LOOPNULL;END LOOP;WHILE num1 > 0 LOOP NULL;num1 := num1 - 1; END LOOP;LOOPNULL;EXIT WHEN num1 = 0; num1 := num1 - 1; END LOOP;```4. 异常处理:```BEGIN-- 执行代码EXCEPTIONWHEN OTHERS THEN -- 处理异常END;```5. 创建存储过程:```CREATE OR REPLACE PROCEDURE procedure_name (param1 IN NUMBER, param2 OUT VARCHAR2) IS-- 变量声明BEGIN-- 执行代码param2 := 'Hello';END;```这些只是PL/SQL语言的一部分功能和用法。
通过学习和实践,您可以掌握更多PL/SQL的知识和技巧,提高数据库编程的效率和质量。
plsql 使用手册

plsql 使用手册(最新版)目录1.PL/SQL简介2.PL/SQL的基本语法3.数据类型与变量4.控制结构5.函数与过程6.触发器与存储过程7.异常处理8.PL/SQL与SQL的交互9.PL/SQL的应用实例10.PL/SQL的优缺点及发展前景正文【PL/SQL 简介】PL/SQL(Procedural Language/Structured Query Language)是一种过程式编程语言,它是为了与 SQL(结构化查询语言)协同工作而设计的。
PL/SQL 通常用于 Oracle 数据库中,用于编写存储过程、触发器、函数等,从而实现对数据库的高级操作和控制。
【PL/SQL 的基本语法】PL/SQL的基本语法包括变量声明、数据类型、控制结构(条件语句、循环语句等)、函数与过程、触发器与存储过程等。
【数据类型与变量】PL/SQL的数据类型与SQL的数据类型基本一致,包括数字类型、字符串类型、日期类型等。
在PL/SQL中,需要先声明变量,再进行使用。
【控制结构】PL/SQL的控制结构包括条件语句(IF-THEN、IF-THEN-ELSE)、循环语句(WHILE、FOR)等,用于实现复杂的逻辑控制。
【函数与过程】函数是一种返回某个值的过程,过程则是一种不返回值的操作。
在PL/SQL 中,函数与过程可以通过参数传递数据,并返回结果。
【触发器与存储过程】触发器是一种在对表执行 INSERT、UPDATE 或 DELETE 操作时自动执行的存储过程。
存储过程是一组预先编译的 SQL 语句,可以实现复杂的业务逻辑。
【异常处理】PL/SQL中可以处理异常,通过EXCEPTION和WHEN子句可以捕获和处理异常情况。
【PL/SQL 与 SQL 的交互】PL/SQL可以与SQL进行交互,例如在PL/SQL过程中调用SQL语句,或者在SQL语句中调用PL/SQL过程。
【PL/SQL 的应用实例】一个典型的PL/SQL应用实例是编写一个存储过程,用于实现数据的增、删、改、查等操作。
Oracle+PLSQL语句大全

--修改表的内容 DDLselect*from emp;--使用insert添加行insert into emp(ename,job,empno)values('WANGYI','DBA','2000');commit;--忽略列的的列表insert into emp values(3000,'ZHANG','DBA',1000,to_date('1990-07-15','yyyy-mm-dd'),2000,null,10); commit;--为列指定空值insert into emp values(3001,'ZHANG1','DBA',1000,to_date('1990-07-15','yyyy-mm-dd'),null,null,null); rollback;--在列值中使用单引号和双引号单引号里面的双引号表示单引号,例如插入 O’nal insert into emp(ename,empno)values('O''nal',2222);--复制一个表create table emp2 as select*from emp ;truncate table emp2;--从一个表向另外一个表复制行insert into emp2(empno,ename,sal)select empno,ename,sal from emp where empno=7369;rollback;insert into emp2 select*from emp where empno=7369;--使用update修改行update emp set ename='KKKK'where ename='WANGYI';--如果有两个WANG则两个都修改COMMIT;--returning 子句variable sk numberupdate emp set sal=3000where ename='KKKK'returning avg(sal)into: sk;----有问题--使用delete 删除行delete from emp where ename='KKKK';commit;--jdbc_lobselect*from emp;drop table JDBCTEST;SELECT*FROM jdbct1;alter table jdbct1 add(comm number(10));alter table jdbct1 drop(comm);--包:规范+包体--创建包规范create or replace package emp2_package as --指定包的用户可以使用的过程和函数的列表(同时包括变量,类型定义,游标)function func_sum(n number,m number) return number ;end emp2_package;--创建包体create or replace package body emp2_package as --实现声明中的方法 function func_sum(n number, m number) return number assums number;beginsums:=n+m;return sums;end func_sum;end emp2_package;--调用包中的函数和过程select emp2_package.func_sum(1,2) from dual;--获取包中函数和过程的信息select * from user_procedures ;--删除包drop package emp2_package;--触发器--创建触发器(简单的)create or replace trigger trigger_testbefore insert on emp2for each row when (new.sal>10000) --行级触发器begin-- raise_application_error('-20011','工资不能超过1W!');dbms_output.put_line('工资不能超过1W!');end trigger_test;--测试触发器insert into emp2(empno,ename,sal) values (9527,'gggg',11111);--创建触发器create or replace trigger trigger_test3after insert or update of sal on empfor each row when (new.sal>old.sal*0.75)begin--update emp set sal=:old.sal;dbms_output.put_line(:old.sal);end trigger_test3;--创建触发器(insert)create or replace trigger trigger_2before insert or update on empfor each row when (new.sal<100)begininsert into emp(empno,ename,sal) values(1111,'tttttt',:new.sal*10);end;--测试触发器insert into emp(empno,ename,sal) values(2312,'rrrr',1000);update emp set sal=9999 where ename='rrrr';select * from emp;select * from dual;--dual是oracle 中的一张虚表,用于测试函数用--大小写转换select upper('abc') from dual; --转换为大写select upper(DUMMY) from dual;select lower(DUMMY) from dual; --转换为小写select lower(dummy) from dual; --字段不区分大小写select initcap('this is a test') from dual; --单词首字母大写--字符操作select concat('aaa', 'bbb') from dual; --连接两个字符串select 'aaa' || 'bbb' from dual; --也可以用||连接字符串select substr('abcdef', 3) from dual; --拆分字符串。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Oracle PL/SQL 编程手册(SQL大全)(转)一、SQL PLUS1 引言SQL命令以下17个是作为语句开头的关键字:alter drop revokeaudit grant rollback*commit* insert selectcomment lock updatecreate noaudit validatedelete rename这些命令必须以“;”结尾带*命令句尾不必加分号,并且不存入SQL缓存区。
SQL中没有的SQL*PLUS命令这些命令不存入SQL缓存区@ define pause# del quit$ describe remark/ disconnect runaccept document saveappend edit setbreak exit showbtitle g et spoolchange help sqlplusclear host startcolumn input timingcompute list ttitleconnect newpage undefinecopy---------2 数据库查询数据字典TAB 用户创建的所有基表、视图和同义词清单 DTAB 构成数据字典的所有表COL 用户创建的基表的所有列定义的清单 CATALOG 用户可存取的所有基表清单select * from tab;describe命令 描述基表的结构信息describe deptselect *from emp;select empno,ename,jobfrom emp;select * from deptorder by deptno desc;逻辑运算符= !=或<> > >= < <=inbetween value1 and value2like%_in nullnotno in,i s not null谓词in和not in有哪些职员和分析员select ename,jobfrom empwhere job in ('clerk','analyst');select ename,jobfrom empwhere job not in ('clerk','analyst');谓词between和not between哪些雇员的工资在2000和3000之间select ename,job,sal from empwhere sal between 2000 and 3000; select ename,job,sal from empwhere sal not between 2000 and 3000; 谓词like,not likeselect ename,deptno from empwhere ename like 'S%';(以字母S开头)select ename,deptno from empwhere ename like '%K';(以K结尾)select ename,deptno from empwhere ename like 'W___';(以W开头,后面仅有三个字母)select ename,job from empwhere job not like 'sales%';(哪些雇员的工种名不以sales开头)谓词is null,is not null没有奖金的雇员(即commision为null)select ename,job from empwhere comm is null;select ename,job from empwhere comm is not null;多条件查询select ename,jobfrom empwhere deptno=20and job!='clerk';表达式+ - * /算术表达式选择奖金高于其工资的5%的雇员select ename,sal,comm,comm/sal from empwhere comm>.05*salorder by comm/sal desc;日期型数据的运算add two days to 6-Mar-876-Mar-87 + 2 = 8-Mar-87add two hours to 6-Mar-876-Mar-87 + 2/24 = 6-Mar-87 and 2hrsadd 15 seconds to 6-Mar-876-Mar-87 + 15/(24*60*60) = 6-Mar-87 and 15 secs 列名的别名select ename employee from empwhere deptno=10;(别名:employee)select ename,sal,comm,comm/sal "C/S RATIO" from empwhere comm>.05*salorder by comm/sal desc;SQL命令的编辑list or l 显示缓冲区的内容list 4 显示当前SQL命令的第4行,并把第4行作为当前行,在该行号后面有个*。
change or c 用新的内容替换原来在一行中第一次出现内容SQL>c/(...)/('analyst')/input or i 增加一行或多行append or a 在一行后追加内容del 删除当前行 删除SQL缓冲区中的当前行run 显示并运行SQL缓冲区中的命令/ 运行SQL缓冲区中的命令edit 把SQL缓冲区中的命令写到操作系统下的文本文件,并调用操作系统提供的编辑器执行修改。
-------------3 数据操纵数据的插入insert into deptvalues (10,'accounting','new york');insert into dept (dname,deptno)values ('accounting',10);从其它表中选择插入数据insert into emp (empno,ename,deptno)select id,name,departmentfrom old_empwhere department in(10,20,30,40);使用参数insert into deptvalues(&deptno,&dname,&loc);执行时,SQL/PLUS对每个参数将有提示用户输入参数对应日期型或字符型数据时,可在参数上加引号,输入时就可不用引号insert into deptvalues(&deptno,'&dname','&loc');插入空值(NULL)insert into deptvalues(50,'education',null);插入日期型数据日期型数据缺省格式:DD-MON-YYinsert into emp(empno,ename,hiredate)values(7963,'stone','07-APR-87');系统时间:SYSDATEinsert into emp(empno,ename,hiredate)values(7600,'kohn',SYSDATE);数据更新update empset job='manager'where ename='martin';update empset job='market rep'where ename='salesma n';update empset deptno=40,job='market rep'where job='salesman';数据删除delete empwhere empno=765;更新的提交commit自动提交方式set autocommit on如果状态设为开,则使用inesrt,update,delete会立即提交。
更新取消rollback两次连续成功的commit之间的操作,称为一个事务---------------4 创建基表、视图创建基表create table dept(deptno number(2),dname char(14),loc char(13));数据字典会自动更新。
一个基表最多254列。
表名列名命名规则:限制第一个字符必须是字母,后面可任意(包括 $ # _ 但不能是逗号)。
名字不得超过30个字符。
唯一某一用户的基表名必须唯一,不能是ORACLE的保留字,同一基表的列名互不相同。
使用双引号如果表名用双引号括起来,则可不满足上述规则;只有使用双引号,才能区别大、小写;命名时使用了双引号,在以后的操作也必须使用双引号。
数据类型:char(n) (不得超过240字符)number(n,d)datelong (最多65536字符)raw (二进制原始数据)空值处理有时要求列值不能为空create table dept(deptno number(2) not null,dname char(14),loc char(13));在基表中增加一列alter table deptadd (headcnt number(3));修改已有列属性alter table deptmodify dname char(20);注:只有当某列所有值都为空时,才能减小其列值宽度。
只有当某列所有值都为空时,才能改变其列值类型。
只有当某列所有值都为不空时,才能定义该列为not null。
例:alter table dept modify (loc char(12));alter table dept modify loc char(12);alter table dept modify (dname char(13),loc char(12));创建视图create view managers asselect ename,job,salfrom empwhere job='manager';为视图列名取别名create view mydept(person,title,salary)as select ename,job,salfrom empwhere deptno=10;with check option选项使用with check option,保证当对视图插入或更新数据时, 该数据必须满足视图定义中select命令所指定的条件。