Oracle-PL/SQL基础-数据库对象(未完待续)

    SQL(Structured Query Language)与PL\SQL(Procedural Language/SQL)的区别:SQL是结构化查询语言,非过程化。PL/SQL也是一种程序语言,叫做过程化SQL语言,是Oracle数据库对SQL语句的扩展。

    在讲PL/SQL基础语法之前需要了解Oracle都有哪些数据库对象,通过数据库对象的使用引入PL/SQL的语法。

    DDL(数据库定义语言)与DML(数据库操作语言)的理解:简单的来讲,创建数据库对象的都是数据库定义语言,对数据库对象进行调用或使用的是数据库操作语言。

/*查询当前数据库有哪些数据库对象*/
SELECT object_type FROM DBA_OBJECTS GROUP BY object_type;

    Oracle数据库对象分为了以下几种:

1. TABLE 表

    官方描述:A table is the basic unit of data organization in an Oracle database. A table describes an entity, which is something of significance about which information must be recorded. For example, an employee could be an entity.

/*常用语法*/
CRETE TABLE [schema.]table_name [ORGANIZATION HEAP](column_name datatype [DEFAULT expreession]);
CREATE TABLE [schema.]table_name AS subquery;
ALTER TABLE [schema.]table_name ADD(column_name datatype,….);
ALTER TABLE [schema.]table_name DROP CLOUMN column_name;
ALTER TABLE [schema.]table_name RENAME CLOUMN A TO B;
DROP TABLE [schema.]table_name cascade constraints;
CRETE GLOBAL TEMPORARY TABLE [schema.]table_name [ORGANIZATION HEAP](column_name datatype [DEFAULT expreession]) [ON COMMIT { DELETE | PRESERVE } ROWS];

2. INDEX 索引

    官方描述:An index is an optional structure, associated with a table or table cluster, that can sometimes speed data access. By creating an index on one or more columns of a table, you gain the ability in some cases to retrieve a small set of randomly distributed rows from the table. Indexes are one of many means of reducing disk I/O.

/*常用语法*/
CREATE [ UNIQUE | BITMAP] INDEX [schema.] indexname ON [schema.] tablename(column,….);
DROP INDEX indexname;

3. CONSTRAINT 约束

  官方描述:Use a constraint to define an integrity constraint—a rule that restricts the values in a database. 

/*常用语法*/
CREATE TABLE table_name  (  column1 datatype null/not null,column2 datatype null/not null,  ...  CONSTRAINT constraint_name [ UNIQUE | PRIMARY KEY ](column1, column2,...)  ); 
ALTER TABLE table_name ADD CONSTRANT constraint_name [ UNIQUE | PRIMARY KEY ] (columu1,column2..);
ALTER TABLE constraint_name  [ENABLE | DISABLE] constraint_name;
ALTER TABLE DROP CONSTRAINT constraint_name ;
ALTER TABLE table_name MODIFY column_name  [ NOT ] NULL;
CREATE TABLE table_name1  (  column1 datatype null/not null,column2 datatype null/not null,  ...  CONSTRAINT constraint_name FOREIGN KEY(column1) REFERENCES table_name2(column1)  ); 
ALTER TABLE table_name ADD CONSTRANT constraint_name FOREIGN KEY(column1) REFERENCES table_name2(column1)  ); 
ALTER TABLE DROP FOREIGN KEY  constraint_name ;

4. VIEW 视图

  官方描述:A view is a logical representation of one or more tables. In essence, a view is a stored query. A view derives its data from the tables on which it is based, called base tables. Base tables can be tables or other views. 

/*常用语法*/
CREATE TABLE [ OR REPLACE ] [ FORCE | NOFORCE] VIEW [ schema.] viewname [(alias1,alias2,….)] AS subquery [WITH CHECK OPTION [CONSTRAINT] constraintname] [WITH READ ONLY [CONTRAINT constraintname]];
DROP VIEW [schema.]viewname;

5. SYNONYM 同义词

  官方描述:A synonym is an alias for a schema object. For example, you can create a synonym for a table or view, sequence, PL/SQL program unit, user-defined object type, or another synonym. Because a synonym is simply an alias, it requires no storage other than its definition in the data dictionary.

/*常用代码*/
CREATE [OR REPLACE] [PUBLIC] SYNONYM [schema.] synonym_name FOR [schema.] object_name [@dblink];  
DROP [PUBLIC] SYSNONYM synonym;
ALTER SYNONYM sysnonym COMPILE;

6. SEQUENCE 序列

  官方描述:A sequence is a schema object from which multiple users can generate unique integers. A sequence generator provides a highly scalable and well-performing method to generate surrogate keys for a number data type.

  常用语法:

7. PROCEDURE 存储过程

  官方描述:A procedure is a type of PL/SQL subprogram, which is a programming object that can be stored and executed in the database server, and called from other programming objects or applications.

  常用语法:
8. FUNCTION 函数

  官方描述:The CREATE FUNCTION statement creates or replaces a standalone function or a call specification.A standalone function is a function (a subprogram that returns a single value) that is stored in the database.

  常用语法:

9. DBMS_SCHEDULER 调度任务

  官方描述:The DBMS_SCHEDULER package provides a collection of scheduling functions and procedures that can be called from any PL/SQL program.

  常用语法:

10. PACKAGE  包

  官方描述:The CREATE PACKAGE statement creates or replaces the specification for a stored package, which is an encapsulated collection of related procedures, functions, and other program objects stored as a unit in the database. 
11. TRIGGER 触发器

  官方描述:Triggers are defined using PL/SQL. Therefore, this section provides some general information but refers to Oracle Database PL/SQL Language Reference for details of syntax and semantics.

  常用语法:

12. DBLINK 数据库连接

  官方描述:Use the CREATE DATABASE LINK statement to create a database link. A database link is a schema object in one database that enables you to access objects on another database. The other database need not be an Oracle Database system. However, to access non-Oracle systems you must use Oracle Heterogeneous Services.

  常用语法:

13. SUBPROGRAMES 子程序

  官方描述:A PL/SQL subprogram is a named PL/SQL block that can be invoked repeatedly. If the subprogram has parameters, their values can differ for each invocation.

  常用语法:

14. SCHEDULES Job调度任务

  官方描述:Oracle Database includes Oracle Scheduler, an enterprise job scheduler to help you simplify the scheduling of hundreds or even thousands of tasks. Oracle Scheduler (the Scheduler) is implemented by the procedures and functions in the DBMS_SCHEDULER PL/SQL package.

  常用语法:

    以上是Oracle数据库对象的介绍,之后会补充如何使用PL/SQL实现上述对象的创建和使用。

/*PL/SQL标准结构*/
HEADER ---可选 DECLARE ---可选 声明 BEGIN ---必选 执行 EXCEPTION ---可选 异常处理 END; ---必选

 

猜你喜欢

转载自www.cnblogs.com/yangjn/p/11627076.html