Showing posts with label Oracle 9i Tutorial. Show all posts
Showing posts with label Oracle 9i Tutorial. Show all posts

Oracle PL/SQL Quick Help : Developers


A PL/SQL Block consists of three sections:
1.The Declaration section (optional).
2.The Execution section (mandatory).
3.The Exception (or Error) Handling section (optional).
Sample below :
      DECLARE
     Variable declaration
BEGIN
     Program Execution
EXCEPTION
    
Exception handling
END;

PL/SQL Variables
The General Syntax to declare a variable is:
variable_name datatype [NOT NULL := value ];
variable_name is the name of the variable.
datatype is a valid PL/SQL datatype.
NOT NULL is an optional specification on the variable.
value or DEFAULT value is also an optional specification, where you can initialize a variable.
Each variable declaration is a separate statement and must be terminated by a semicolon.
Example :
DECLARE Empcode number(4);
EmpName varchar2(10) NOT NULL := “Abhi”;

assign values to variables
•       variable_name:= value;
•       SELECT column_nameINTO variable_name FROM table_name [WHERE condition];

Scope of Variables
•       Local variables - These are declared in a inner block and cannot be referenced by outside Blocks.
•       Global variables - These are declared in a outer block and can be referenced by its itself and by its inner blocks.
•       Example :
DECLARE
 var_num number;  // Global variables
BEGIN
var_num := 100;
DECLARE
var_sqr number;  // Local variables
BEGIN
var_sqr := var_num * var_num2
END;
END;

PL/SQL Constants

The General Syntax to declare a constant is: constant_name CONSTANT datatype := VALUE;

Example :
DECLARE empCode CONSTANT number (3) := 10;

Conditional Statements in PL/SQL
IF condition 1 THEN  
statement 1;  
statement 2;
ELSIF condtion2 THEN  
statement 3;
 ELSE  
statement 4;
END IF
Parameters in Procedure and Functions
1)      IN type parameter: These types of parameters are used to send values to stored procedures.
CREATE [OR REPLACE] PROCEDURE procedure_name ( param_name1 IN datatype, param_name12 IN datatype ... )
2) OUT type parameter: These types of parameters are used to get values from stored procedures. This is similar to a return type in functions.
CREATE [OR REPLACE] PROCEDURE proc2 (param_name OUT datatype)
3) IN OUT parameter: These types of parameters are used to send values and get values from stored procedures.
CREATE [OR REPLACE] PROCEDURE proc3 (param_name IN OUT datatype)

Types of Exception

a) Named System Exceptions
b) Unnamed System Exceptions
c) User-defined Exceptions

Named System Exceptions
•       CURSOR_ALREADY_OPEN
When you open a cursor that is already open.
ORA-06511
•       INVALID_CURSOR
When you perform an invalid operation on a cursor like closing a cursor, fetch data from a cursor that is not opened.
ORA-01001
•       NO_DATA_FOUND
When a SELECT...INTO clause does not return any row from a table.
ORA-01403
•       TOO_MANY_ROWS
When you SELECT or fetch more than one row into a record or variable.
ORA-01422
•       ZERO_DIVIDE
When you attempt to divide a number by zero.
ORA-01476
Example :
BEGIN
Execution section
EXCEPTION WHEN NO_DATA_FOUND THEN
dbms_output.put_line ('No record found.');
END;
User-defined Exceptions
DECLARE 
userDefinedExp EXCEPTION;  
 BEGIN    
RAISE userDefinedExp;     
EXCEPTION    WHEN userDefinedExp THEN         
raise_application_error(-2100, 'The User Defined Exception.');
END;
RAISE_APPLICATION_ERROR
•       RAISE_APPLICATION_ERROR is a built-in procedure in oracle which is used to display the user-defined error messages along with the error number whose range is in between -20000 and -20999.
•       RAISE_APPLICATION_ERROR (error_number, error_message);
• The Error number must be between -20000 and -20999
• The Error_message is the message you want to display when the error occurs.

Unnamed System Exceptions
DECLARE  
rec_exception EXCEPTION;   
PRAGMA   
EXCEPTION_INIT (rec_exception, -2296);
BEGIN  
EXCEPTION   
WHEN rec_exception   THEN
Dbms_output.put_line('Unnamed System Exceptions.');
END;


What are few Oracle DDL statements or commands ?

Below are few  DDL commands to create, alter, drop, and rename tables.


CREATE TABLE


  • Create a table.
  • Create a table based on another table by using a subquery.


ALTER TABLE


  • Modify table structures.
  • Change column widths, change column data types, and add columns.


DROP TABLE




  • Remove rows and a table structure.
  • Once executed, this statement cannot be rolled back.


RENAME




  • Rename a table, view, sequence, or synonym.

TRUNCATE


  • Remove all rows from a table and release the storage space used by the table.
COMMENT


  •  Add a comment to a table or a column.
  • Query the data dictionary to view the comment.

Oracle 9i Tutorial : What is syntax of DELETE Statement Syntax


You can remove existing rows by using the DELETE statement.


Delete Statement Syntax below :

  DELETE [FROM]    table

  [WHERE    condition];


Oracle 9i Tutorial : UPDATE Statement Syntax

UPDATE Statement Syntax



Update Statement is used to modify existing rows in table.

Below is Update State Syntax :

UPDATE  table
SET  column = value [, column = value, ...]
[WHERE   condition];

In the syntax:
  table  is the name of the table
  column  is the name of the column in the table to populate
  value  is the corresponding value or subquery for the column
  condition  identifies the rows to be updated and is composed of column names   expressions, constants, subqueries, and comparison operators

Updating Rows in a Table

•Specific row or rows are modified if you specify the WHERE clause.
   •All rows in the table are modified if you omit the WHERE clause.
 

Updating Rows Based on Another Table

You can use subqueries in UPDATE statements to update rows in a table. The example on the slide updates the COPY_EMP table based on the values from the EMPLOYEES table. It changes the department number of all employees with employee 200’s job ID to employee 100’s current department number.

Oracle 9i Tutorial : INSERT Statement Syntax

INSERT Statement Syntax


INSERT statement is used to add new rows to table.


Syntax of INSERT statement below :


INSERT Statement Syntax








Only one row can be inserted at a time with this syntax.

INSERT statement can also be used for copying rows from another table.

Syntax below :

INSERT INTO table1 ( column11 , column12 , ..... )
SELECT column21 , column22 , .......
FROM
table2

 Example below :

Oracle 9i Tutorial : Data Manipulation Language

      Data Manipulation Language ( DML )


A DML statement is executed when you:

–Add new rows to a table

–Modify existing rows in a table

–Remove existing rows from a table

Transaction

A transaction consists of a collection of DML statements that form a logical unit of work.

Oracle 9i Tutorial : Adding Comments to Table

Adding a Comment to a Table

You can add a comment of up to 2,000 bytes about a column, table, view, or snapshot by using the COMMENT statement. The comment is stored in the data dictionary and can be viewed in one of the following data dictionary views in the COMMENTS column:
ALL_COL_COMMENTS
USER_COL_COMMENTS
ALL_TAB_COMMENTS
USER_TAB_COMMENTS
Syntax
    COMMENT ON TABLE table | COLUMN table.column
      IS 'text';
In the syntax:
  table  is the name of the table
 
column  is the name of the column in a table
 
text  is the text of the comment
You can drop a comment from the database by setting it to empty string (''):
    COMMENT ON TABLE  employees IS ' ';

Oracle 9i Tutorial : Drop,Rename and Truncate table

DROP TABLE

  Remove rows and a table structure.
  Once executed, this statement cannot be rolled   back.
  -DROP TABLE EMPLOYEE

RENAME

  Rename a table, view, sequence, or synonym.
  -RENAME EMPLOYEE TO EMPL;

TRUNCATE

  Remove all rows from a table and release the   storage space used by the table.
  The DELETE statement removes only rows.
  -TRUNCATE TABLE EMPLOYEE;

Oracle 9i Tutorial : ALTER TABLE Statement

 ALTER TABLE Statement

         After you create a table, you may need to change the table structure because: you omitted column, your column definition needs to be changed, or you need to remove columns. You can do this by using the ALTER TABLE statement.

Use the ALTER TABLE statement to:
•Add a new column
–Alter table employee add(manager number(4));
•Modify an existing column
–Alter table employee modify(empname varchar2(10));
•Define a default value for the new column
–Alter table employee modify(hiredate date default sysdate);
•Drop a column
                     - Alter table employee drop column manager;

Oracle 9i Tutorial : Creating a Table by Using a Subquery Syntax

Creating a Table  by Using a Subquery Syntax


•Create a table and insert rows by combining the CREATE TABLE statement and the AS subquery option.
•Match the number of specified columns to the number of subquery columns.
Define columns with column names and default values.


Creating a Table from Rows in Another Table
A second method for creating a table is to apply the AS subquery clause, which both creates the table and inserts rows returned from the subquery.
In the syntax:
  table  is the name of the table
  column  is the name of the column, default value, and integrity constraint
  subquery  is the SELECT statement that defines the set of rows to be inserted into   the new table
Guidelines
The table is created with the specified column names, and the rows retrieved by the SELECT statement are inserted into the table.
The column definition can contain only the column name and default value.
If column specifications are given, the number of columns must equal the number of columns in the subquery SELECT list.
If no column specifications are given, the column names of the table are the same as the column names in the subquery.
The integrity rules are not passed onto the new table, only the column data type definitions.
Example :

Oracle 9i Tutorial : Table and Data Dictionary

User Tables:

–Are a collection of tables created and maintained by the user
–Contain user information

Data Dictionary:

–Is a collection of tables created and maintained by the Oracle Server
Contain database information

 Tables in the Oracle Database

User tables are tables created by the user, such as EMPLOYEES. There is another collection of tables and views in the Oracle database known as the data dictionary. This collection is created and maintained by the Oracle server and contains information about the database.
All data dictionary tables are owned by the SYS user. The base tables are rarely accessed by the user because the information in them is not easy to understand. Therefore, users typically access data dictionary views because the information is presented in a format that is easier to understand. Information stored in the data dictionary includes names of the Oracle server users, privileges granted to users, database object names, table constraints, and auditing information.
There are four categories of data dictionary views; each category has a distinct prefix that reflects its intended use.

Table and Data Dictionary

Labels

.NET Framework Interview Questions (7) .NET Interview Questions (10) .NET Remoting Interview Questions (1) ADO.NET and BLOB Error (1) ADO.NET Interview Questions (4) Agile Articles (9) AJAX Articles (5) AJAX Interview Questions (11) Algorithms (2) Analytics Articles (2) Analytics Interview Questions (3) Android FAQs - Part 1 (2) Articles (13) ASP.NET Articles (24) ASP.NET Error and Resolution (4) ASP.NET Interview Questions (23) ASP.NET Tutorial (8) AWS Interview Questions (16) Business Analyst Interview Questions (1) Cloud Computing Interview Questions (16) CSharp Articles (17) CSharp Interview Questions (32) CSharp Tutorial (17) Data Analysis (2) Data Structure (1) Design Pattern Articles (5) DevOps Tutorial (1) Digital Marketing Interview Questions (1) Download Templates (1) Error Resolution (6) Excel Articles (9) Excel Macros (1) Excel Tips and Tricks (10) HTML5 Interview Questions (3) HTML5 Tutorial (3) Interview Preparation (2) Interview Questions (24) Introduction to Business Analytics (10) Introduction to Python (7) Introduction to R Programming (23) JAVA Articles (6) Java Tutorial (5) LINQ Articles (4) LINQ Interview Questions (2) LINQ Tutorial (3) Microservices Interview Questions (1) MVCInterviewQuestions (2) OOPs Interview Questions (4) Oracle 9i Tutorial (14) Oracle Articles (2) Oracle Interview Questions (15) Outlook Error (1) PHP Interview Questions (3) PHP Tutorial (3) Product Management (12) Product Management Interview Questions (14) Product Owner Interview Questions (2) Program Management (5) Project Management (13) Project Management Articles (34) Project Management Interview Questions (25) Quiz (1) RallyDev Help (1) Scrum Master Interview Questions (11) Selenium Tutorial (1) Sharepoint Articles (1) SQL Interview Questions (23) SQL Server Articles (20) SSIS Interview Questions (6) SSRS Interview Questions (1) Technical Program Management (12) Technical Program Management - Interview Questions (24) TechnicalProgramManagement (5) Threading Interview Questions (2) Tutorial (8) UML Articles (3) UML Interview Questions (2) Unix (3) UNIX Tutorial (3) WCF Articles (20) WCF Interview Questions (9) WCF Quiz (2) WCF Tutorial (16) Web Service Articles (5) Web Service Interview Questions (3) Window Azure (1) XML Articles (6) XML Interview Questions (3) XML Tutorial (3)