Oracle Complete Notes
Oracle Complete Notes
Drop Tables 4) Distinct ,Wild cards and Sub Queries 5) Order By, Group By Clause 6) Set Functions or Set Operator 7) Joins (Inner / Outer / Left / Right) 8) String Functions 9) Date Functions 10) Math Functions and Numeric Functions 11) Create, Manage and Drop Permission Grant / Revoke 12) Create, Manage and Drop View 13) Create, Manage and Drop Index 14) Create, Manage and Drop Constraints 15) Create, Manage and Drop Synonyms 16) Create, Manage and Drop Sequence 17) Procedure Language - SQL (PL - SQL ) 18) Conditions and Loops 19) Exception Handling 20) Create, Manage and Drop Procedures 21) Create, Manage and Drop Functions 22) Create, Manage and Drop Package 23) Cursor 24) Trigger 25) SQL * Plus commands, Forms and Reports
ORACLE Complete Notes Class: 1 (Theory) Introduction of Oracle: Developed by Oracle One of the Server Level Data Base Software Oracle all read by Table Format Oracle is not a case sensitive Oracle Database File is <Database Name>.SQL Types of Oracle: 1) SQL Structure Query Language 2) PL SQL Procedure Language and SQL 3) ISQL Internet Structure Query Language 4) SQL *Plus Advance SQL Commands Diagram of Data Base Mgt System Concept:
Application Server (Http server Run on this Machine)
U1
U2
U3
U4
U5
Way to Open Oracle Software: Start All Programs Oracle Database 10 G Express Edition SQL Command Line Click Or Start Run sqlplus (Type) ok
2 Prepared by A.KARTHIKEYAN ( : 9790768919
ORACLE Complete Notes Class: 2 (Theory) Create, Manage and Drop Users or Schemas: Diagram of Access Database with Schemas:
Oracle Data
Default User Name and Password: User name system Password manager User name scott Password tiger How to Create New Schema or User Name with Password: Syntax: SQL> Create user <user name> Identified by <password > / externally/ globally [Default Tablespace <Table space1> Temporary Tablespace <Table space2> Quota unlimited on <Table space1> Quota 10MB on <Table space3> Password Expire Account Lock/ Unlock]; O/P: User created Eg: SQL> Create User Karthik identified by Hacker; O/P: User Created
3 Prepared by A.KARTHIKEYAN ( : 9790768919
ORACLE Complete Notes How to Give Permission to Access our New User or Schema: Syntax: SQL> Grant [connect, resource, dba] to <new User name>; Eg: SQL> Grant connect, resource to Karthik; To Navigate One Schema or User to Another Schema or User: Syntax: SQL > Connect or Conn; Eg: SQL > Connect or Conn; To Display All Schema or User Name with Schema or User ID: Syntax: SQL> Select * from all_Users; Eg: SQL> Select * from all_Users; To change password for old User Account: Syntax: SQL > Password; Eg: SQL > Password; To Check the Current Schema or User Name: Syntax: SQL> Select User from dual; or SQL> Show User; Eg: SQL> Select User from dual; or SQL> Show User; How to Drop Schema or User Account in Database: Syntax: SQL> Drop User <User name>; Eg: SQL> Drop User Karthik;
ORACLE Complete Notes To Run the Last Query: Syntax: SQL> / or L or list; Eg: SQL> / or L or List; To Display all object in Current User Account: Syntax: SQL> Select * From Tab; Eg: SQL> Select * From Tab; How to Return Oracle Database to Host or System Command: Syntax: SQL > Host; Eg: SQL > Host; How to Change Text in Screen: Syntax: SQL> C /<What Text you want to Change>/ <Your Content>; Eg: SQL> C /Karthik/ Admin; How to Edit Page in Oracle Data Base Screen: Syntax: SQL> Edit or Ed; Eg: SQL> Edit or Ed; How to Exit Oracle Database Software: Syntax: SQL> Exit; Eg: SQL> Exit;
ORACLE Complete Notes Class: 3 (Theory) Introduction of SQL: Types of SQL: 1) Data Definition Language: A data base schema or User or plan (diagram) is specifies by a set of definitions expressed by a special language called DDL 1) Create 2) Alter 3) Drop 4) Truncate 2) Data Manipulation Language: This language that enable user to access or manipulate data as organized by appropriate data mode 1) Insert 2) Update 3) Delete 4) Select 3) Data Control or Transaction Control Language: This language is used to give permission to access our database to another user or to revoke that user 1) Grant 2) Revoke 3) Commit 4) Rollback
ORACLE Complete Notes Define Data type: Data type is types of data Data means collection of information. Types of Data Types: 1) Integer or Number 2) Float 3) ROWID 4) Varchar or Varchar2 5) Date 6) TimeStamp 7) Lob 8) CLob 9) Blob 10) BFile files 11) RAW 12) Long Accept Integer values Accept Floating values Used for sequence Accept Character and String Value Accept Date and Time Value Accept Date and Time with Second Accept Image data types Character large Object string Binary large Object string Store file system directory Store graphics, sounds files Store summary context
Create, Manage and Drop Tables: How to Create New Table to Our Data Base: Syntax: SQL> Create Table <table name> (<Field name 1> <data type> (<size>), <Field name 2> <data type> (<size>),); O/P: Table created Eg: Table Name: Student Field name Rollno Name Mark1 Mark2 Mark3 Data type (<size>) number (3) varchar (20) number (3) number (3) number (3)
Eg: SQL> Create table Student (Rollno number (3), name varchar(20), mark1 number(3), mark2 number(3), mark3 number(3)); To Display Table Structure: Syntax: SQL> Describe < Table Name >; or SQL> Desc <Table Name>; Eg: SQL> Describe student; How to Rename Table Name: Syntax: SQL> Rename <Old Table Name> to <New Table name>; Eg: SQL> Rename Student to Marksheet;
8 Prepared by A.KARTHIKEYAN ( : 9790768919
How to insert values for all the Field Names: Syntax: SQL> Insert into <Table name> values (<field name1 value>, <Field name2 value>, <Field name3 value>,); How to insert values for particular Field Names Only: Syntax: SQL> Insert into <table name> (<Field name1>, < Field name2>,..) values (<field name1 value>, <Field name2 value>,); How to insert values for all the Field Names in Run Time: Syntax: SQL> Insert into <Table name> values (<&Field name1>, <&Field name2 >, <& Field name3>,); Eg: Table name: student Rollno 1 2 3 4 5 name Senthil Karthi Vinoth Rajesh Vignesh mark1 mark2 mark3 23 56 67 89 78 89 67 56 34 56 34 67 56 45
Eg: SQL > Insert into student values (&Rollno, &Name, &Mark1, &Mark2, &Mark3); O/P: 1 row Affected
9 Prepared by A.KARTHIKEYAN ( : 9790768919
Alter: 1) Add Column To Add New Column Name 2) Modify Column To Modify Data Type Column Size 3) Rename Column To Rename Column Name Only 4) Drop Column To Drop Old Column Name It does not affect the values only affects field name How to Add New Field Name in Existing Table: Syntax: SQL> Alter table <Table name> add (<New Filed name1> <Datatype> (<size>), <New Filed name2> <Datatype> (<size>),.); O/P: SQL> Table Altered Eg: Table name: Student New field name Data type (size) Tot Number (3) Avg Number (5, 2) Result Varchar (10) Eg: SQL> Alter table Student add (Tot number (3), Avg number(5,2), Result Varchar (10)); O/P: SQL> Table Altered How to Modify Data Type and Its Size Only: Syntax: SQL> Alter Table <Table name> modify (<old Field name> <new Data type> (<size>),);
ORACLE Complete Notes Eg: Table name: Student Old field name old Data type (size) Result Varchar (10)
Eg: SQL> Alter table student Modify (Result varchar (7)); O/P: Table Altered How to Rename Old Field Name to New Field Name: Syntax: SQL> Alter table <Table name> rename Column <Old field name> to <New Field name>; Eg: Table name: Student Old Field Name New Field Name: Rollno Regno SQL> Alter table student rename Column Rollno to Regno; O/p: SQL>Table Altered How to Drop Particular Field Name in Table: Syntax: SQL> Alter table <table name> Drop (<Old Field name>,); O/p: Table Altered Eg: SQL> Alter table student Drop (Result); O/p: Table Altered
OPERATORS: Operator is a Symbol, Which is used to do some Operations 1) Arithmetic Operator: Operator Meaning + Addition Subtraction * Multiplication / Division Mod Modulus (After division Remainder) 2) Relational Operator: Operator Meaning > Greater Than < Lesser than >= Greater than or equal to <= Less than or equal to = Equal to < > ,! = Not Equal := Assignment operator 3) Logical Operator: Operator Meaning AND AND (Both condition True) OR OR (Any one condition True) NOT NOT 4) Special Operator: Operator Meaning Like Same Between To between IN From the value is present or not
12 Prepared by A.KARTHIKEYAN ( : 9790768919
ORACLE Complete Notes EOF BOF || End of file Beginning of file Attach Two String
To Update Values Only in Particular Table: Syntax: SQL> Update <table name> set <field name> = <values> [where <condition>] To Find the Total Values: Eg: SQL> update student set tot = mark1+mark2+mark3; O/p: SQL> 5 rows updated To Find the Average Values: SQL> update student set avg = tot/3; O/p: SQL> 5 rows updated To Find the Result Values for Pass: SQL> update student set result = Pass where mark1>34 and mark2>34 and mark3>34; O/p: SQL> 3 rows updated To Find the Result Values for Fail: SQL> update student set result = Fail where mark1<35 or mark2<35 or mark3<35; O/p: SQL> 2 rows updated To Display all Values in Particular Table: Syntax: SQL> select * from <table name> [where <Condition>]; Eg: SQL> Select * From Student;
13 Prepared by A.KARTHIKEYAN ( : 9790768919
To Display Select Field Name Values Only in Particular Table: Syntax: SQL> select <Field Name> from <table name> [where <Condition>]; Eg: SQL> select name, result from student; O/p: To Display Output Based on Condition: SQL> Select * from student where result= Pass; O/p: How to Copy all Data to New Table Name: Syntax: SQL> Create table <Table Name> as <Select Statements>; EG: SQL> Create table Student1 as Select *from student; O/P: Table Created
Class: 4 (Theory) Distinct: To display without Duplicate Entries in Data Base: Syntax: SQL> Select Distinct * From <Table Name>; Eg: SQL> Select Distinct * From Hcl; The wild cards: * Asterisk (*) means display all field values % Leave remaining character or number Like Like means similar to those values _ Leave single character or number Syntax for Wild Cards: SQL> Select * from <Table name> where <Field name> like <Wild Card Condition>; Eg: SQL> select * from Student where Name like %S; Or SQL> select * from Student where Name like S%; Or SQL> select * from Student where Name like S_nthil;
Sub Queries: Operator and Or Not Null Not Null Like Between Not Between In Not In Is NVL = Meaning Both Condition True Any One condition True Opposite Empty Value Not Empty Value Similar From the Range Not From the Range Within that Range Not within that Range Same Equal of
Syntax: SQL> Select * From <Table Name> where <Sub Queries Cond>; Eg: SQL> Select * From Hcl where salary>2000 and salary<10000; SQL> Select Idno,Name From Hcl where salary<50000 or Dept=sw and idno<10; SQL> select * from hcl where salary between 8000 and 10000; SQL> select * from hcl where salary like null; SQL> select * from hcl where salary is not null; SQL> select * from hcl where salary is null and idno<=10; SQL> select * from hcl where salary in 9000 or idno<=3; SQL> select * from hcl where idno in (1, 3, 5);
Class: 5 (Theory) Order by Clause: How to Display Values as Ascending or Descending Order: SQL> Select * from <table name> [where <Condition>] order by <Field name or Column Number> Asc or Desc; Eg: SQL>Select * from Student order by idno Asc; SQL>Select Name, Result from Student order by idno Asc; SQL>Select * from Student order by idno Desc; Group by Function or Aggregative Function: Group by clause tells the oracle to group the records together Group or Aggregative Function Disp sum of given field values Disp max of given field value Disp min of given field value Disp number of rows found Disp Avg of Given Field value Syntax Sum (<Field name>) Max (<Field name>) Min (<Field name>) Count (<Field name>) Avg (<Field Name>)
Syntax: SQL> Select <Aggregative function> from <Table name> [Group by <Field name>]; Eg: SQL> Select sum (Salary) from Hcl group by Dept; SQL> Select Avg (Salary) from Hcl group by Dept; SQL> Select Max (Salary) from Hcl group by Dept; SQL> Select Min (Salary) from Hcl group by Dept; SQL> Select Count (Salary) from Hcl group by Dept;
17 Prepared by A.KARTHIKEYAN ( : 9790768919
Class: 6 (Theory) Set Function or Set Operator: Set Function Union UnionAll Minus Intersect Meaning Used to Join two or more than two table Used to Join all field name in more than 2 table Used to display both common values only Used to extract value from 2 table
Syntax for Set Operator or Set Function: SQL><Select Statements1> <Set Function> <Select Statements2>; Eg: SQL> Select Name from Student1 Union Select Name from Student2; SQL> Select Name from Student1 Minus Select Name from Student2; SQL> Select Name from Student1 Intersect Select Name from Student2; How to Delete all or Particular values in Table: Syntax: SQL> Delete from <Table name> [where <Condition>]; Eg: SQL> Delete from Student Where Rollno=10; How to delete all Values Only in Table: Syntax: SQL>Truncate Table <Table name>; Eg: SQL>Truncate Table student;
18 Prepared by A.KARTHIKEYAN ( : 9790768919
ORACLE Complete Notes How to Drop Table Permanently: Syntax: SQL> Drop Table <table name>; Eg: SQL> Drop Table Student; Class: 7 (Theory) Joins Query: A Join is a query that combines rows from two or more tables, views, etc. Eg: Emp.deptno = dept.deptno Types of Joins: 1) Equi Join 2) Non Equi Join 3) Outer Join 4) Selft Join
1. Equi Joins
Selects the department names along with the other details for every employee. Joins emp and dept tables.
Query SELECT empno, ename, dept.deptno, dname FROM emp, dept WHERE emp.deptno = dept.deptno;
1.
Non-equi joins
The query selects the name,department number and department names of those employees whose salary is greater than 3000.
Query SELECT ename, emp.deptno, dname FROM emp, dept WHERE emp.deptno = dept.deptno AND sal > 3000;
Self Join Illustrates the way to a join a table to itself. Displays every employee's number, name and his/her manager's number and name. In order to query an employee and his/her manager the emp table is joined to itself. Uses table aliases to represent the same table as two different tables.
Query SELECT x.empno "EmpNo", x.ename "EmpName",y.empno "MgrNo",y.ename "MgrName" FROM emp x, emp y WHERE x.mgr = y.empno; Query Analysis
Before we analyze the program, let us look at the following illustration which joins two tables X and Y which are exact copies of emp. SELECT X.ename,Y.ename FROM X,Y WHERE X.mgr = Y.empno; We know that the mgr column contains values that are present in the empno column since a non-existing employee cannot be a manager. X.mgr = Y.empno is an equi join condition. Thus X.ename returns the name of the employee from the X table and Y.ename returns the name of the employee from the Y table. The name returned by Y.ename would be the manager name due to the join condition. The following figure explains this concept.
X table X table
20 Prepared by A.KARTHIKEYAN ( : 9790768919
Y table
X.empno X.ename Y.empno(X.mgr) Y.ename 7369 Smith 7902 Ford The query SELECT x.empno "EmpNo",x.ename "EmpName" , y.empno "MgrNo",y.ename "MgrName" works in a similar way. FROM emp x, emp y refers the single table emp as x and y for the purpose of the join.
The emp table appears twice in the FROM clause and is followed by table
aliases, that are used to qualify column names in the join condition. x and y are termed as table aliases. WHERE x.mgr = y.empno creates a join between the mgr column of one emp table with the empno of another copy of emp table. This condition joins a table to itself. Hence termed as Self Join. 1. Another Illustration for Self Join
Selects the employees who get salary greater than their superiors. Use of Self Join and a non-equal condition.
Query SELECT x.ename "EmpName",x.sal "EmpSal",y.ename "MgrName",y.sal "MgrSal" FROM emp x,emp y WHERE x.mgr = y.empno AND x.sal > y.sal; Query Analysis
WHERE x.mgr = y.empno joins the mgr column with the empno column. x.sal > y.sal compares the salary of each employee with his manager's salary.
21 Prepared by A.KARTHIKEYAN ( : 9790768919
Class: 8 (Theory) String Function: String Function Lower(<Text>) Upper(<Text>) Length(<Text>) Replace(<String>,<c1>,<c2>) SubStr (<string>,<start Position>, <No of char>) Trim Ltrim(<String >) Rtrim(<String>) Reverse(<String>) Ascii(<Char>) Chr(<Ascii>) Soundex( ) Lpad( ) Rpad() Initcap Meaning To Convert Lower Case To Convert Upper Case To Find out the Length String with every occurrence of character 1 replace with character2 Extract position of given string Eliminate space for both side Eliminate Left Character or Space Eliminate Right Character or Space To reverse the Given string To convert ASCII value To Convert char value Based on Sound echo Left Pad Right Pad First letter capital others small
Syntax for Applying String Function: SQL> Select <String Function Name> from dual; Eg: SQL> Select Length (Hacker) from dual;
22 Prepared by A.KARTHIKEYAN ( : 9790768919
Class: 9 (Theory) Date and Time Function: Date Function SysDate() Last_day(<Date>)) Add_Months(<date>,<No of Month Add>); Months_between(<First months>,<last Month>) Next_day(<date>,<day>) To_char(<Date>,Day-Monthyear) To_Date(<Date>,Day-monthyear) Current_Date SysTimeStamp Meaning Display Current System Date Display Last day of given month Add no of Months from the given date Find the Diff between two months Display next given days from the date Convert given date as character format Convert Date Format Display current Date Display current sys time stamp
Syntax for Applying Date Function: SQL> Select <Date Function Name> from dual; Eg: SQL> Select sysdate from dual;
23 Prepared by A.KARTHIKEYAN ( : 9790768919
Class: 10 (Theory) Numeric and Mathematical Function: Numeric Meaning Ceil (<Value>) Display Nearest After Whole Integer Value Floor(<Value>) Display Nearest Before Whole Integer Value Round(<Value>) Round up the given Value Power(<Value>) Find out the Power Value Sign(<Value>) Display Positive or Negative Sqrt(Value>) Display Square root value Mod(<Value>) Display Modulus Value Trunc(<Value>) Remove Floating Points Abs(<Value>) Display absolute value Max(<Values>) Display max value Min(<Values>) Display Min Value Sum(<Values>) Sum of all values Count(<Values>) Display total no of counts Rank(<Values>) Set the ranking Avg(<Values>) Display average value Greatest(<Values>) Display greatest value Least(<Values>) Display Least value Syntax for Applying Math & Numeric Function: SQL> Select <Math Function Name> from dual; Eg: SQL> Select Power (4, 2) from dual; SQL> Select Max (4, 2,56,34,245,64,45) from dual;
24 Prepared by A.KARTHIKEYAN ( : 9790768919
ORACLE Complete Notes SQL> Select Avg (4, 2,56,34,245,64,45) from dual; SQL> Select count (4, 2,56,34,245,64,45) from dual;
Class: 11 (Theory) Create, Manage and Drop Permission (Grant / Revoke) (TCL): How to give Permission to access Our New User: Syntax: SQL> Grant [connect, resource, dba] to <New User Name>; Syntax: SQL> Grant connect, resource, dba to Raj; How to Revoke Permission to access Our Old User: Syntax: SQL> Revoke [connect, resource, dba] from <User Name>; Syntax: SQL> Revoke connect, resource, dba from Raj; How to give Permission to access our Table to Other User: Syntax: SQL> Grant <DML Command> on <table name> to <user name>; Eg: SQL> Grant select on Student to Raj; How to Remove Permission to access our Table to Other User: Syntax: SQL> Revoke <DML Command> on <Table name> from <New User name>; Eg: SQL> Revoke select on student from Raj; How to save our Data permanently to our Database: Syntax:
25 Prepared by A.KARTHIKEYAN ( : 9790768919
ORACLE Complete Notes SQL> Commit; Eg: SQL> Commit; How to Undone Last Action until the last Commit Statements: Syntax: SQL> Rollback; Eg: SQL> Rollback; Class: 12 (Theory) Create, Manage and Drop View: View is a logical window on a Table It depends on existing Table How to Create New View: Syntax: SQL> Create View <View Name> As <Select Statements>; Eg: SQL> Create View V1 as Select* From Student; How to Display all View Values: Syntax: SQL> Select* From <View Name>; Eg: SQL> Select* From V1; How to Insert values for View (It Affect both Table and View): Syntax: SQL>Insert into <View name> values (<&field name1>,); Eg: SQL>Insert into V1 values (&name); How to delete values from View (It Affect Both): Syntax: SQL>Delete from <View name> Where <Condition>;
26 Prepared by A.KARTHIKEYAN ( : 9790768919
ORACLE Complete Notes Eg: SQL>Delete from V1 Where Regno=5; How to Drop View in Our Database: Syntax: SQL> Drop View <View Name>; Eg: SQL> Drop View V1; Class: 13 (Theory) Create, Manage and Drop Index: An index is a performance-tuning method of allowing faster retrieval of records. An index creates an entry for each value that appears in the indexed columns. By default, Oracle creates B-tree indexes. Syntax for Create Index: CREATE [UNIQUE] INDEX index_name ON table_name (column1, column2, . column_n) [ COMPUTE STATISTICS ];
Syntax for Rename Index Name: Alter Index <Old Index name Rename to <New Index name>; Syntax for Drop Index: Drop index <Index Name>;
Class: 14 (Theory) Create and Manage Constraints: Constraints Keys Unique Meanings This key accept null number only but is does not allow duplicate Values Not Null This key does not allow null values but it accept the duplicate Values Primary Key This Key Does not accept Both Null and Duplicate Values Check (<Condition>) This key is used for Checking Condition at Insert Time Default Set a content as default value Foreign Key or References This key used for Create Relationship Between one table to another Table Syntax for Constraint Keys: SQL> Create table <table name> (<Field name> <Data type> <Constraint Key>,..); Eg: SQL> Create table Student (Regno <Data type> <Constraint Key>,..); Syntax for References or Foreign Key:
ORACLE Complete Notes SQL> Create table <table name2> (<Table2 field name1> <data type>, foreign key (<Table1 field name1>) references <Table name1> (<Table1 field name1>),..); Eg: SQL> Create table Student2 (Rollno Number, foreign key (Rollno) references Student1 (Rollno), Name Varchar(20));
Class: 15 (Theory) Create and Manage Synonyms: A synonym is an alternative name for objects such as tables, views, sequences, stored procedures, and other database objects.
Class: 16 (Theory) Create and Manage Sequence: A sequence is an object in Oracle that is used to generate a number sequence. Syntax for Create Sequence: SQL> Create Sequence <Sequence Name> MinValue <n> MaxValue <n> Start with <n> Increment by <n>; Eg for Sequence Creation: SQL > Create Sequence S1 MinValue 1 MaxValue 100 Start with 10 Increment by 1; Syntax for insert sequence value to Table: SQL> insert into <Table name> values (<seq name>.nextval>); Eg: SQL> insert into Hcl (Sno, name, Salary) values (s1.nextval,&name, &salary);
30 Prepared by A.KARTHIKEYAN ( : 9790768919
ORACLE Complete Notes Syntax for display Current Sequence Value: SQL > Select <Seq Name>.currval from dual; Eg: SQL > Select S1.currval from dual; Syntax for Drop Sequence: SQL> Drop sequence <Sequence name>; Eg: SQL> Drop sequence S1; Class: 17 (Theory) PL - SQL: PL means Procedure Language and Structure Query Language PLSQL is refers to block structure language It contains program code, variable declaration, error handling, procedures and functions Structure of PL - SQL: SQL> Declare <Variable Declaration>; Begin <Main Coding>; Exception <Exception Coding> End;
Class: 18 (Theory) Conditions and Loops: 1) IF-THEN-ELSE Statement 2) Case Statement 3) GOTO Statement 4) Loop Statement 5) FOR Loop 6) CURSOR FOR Loop 7) While Loop 8) Repeat Until Loop 9) Exit Statement
Class: 19 (Theory) Exception Handling: Some abnormal an error can be occur to avoid this error we can use Exception concept.
Class: 23 (Theory) Cursor: A cursor is a mechanism by which you can assign a name to a "select statement" and manipulate the information within that SQL statement.
Class: 24 (Theory) Trigger: Stored procedure that is fired implicitly whenever the data is associated table are changed is called the trigger Parts of Trigger: 1) Trigger Statements: SQL statement that includes trigger is called trigger statement 2) Trigger Restriction: Trigger is fired only if the condition is true 3) Trigger Action: PL/SQL block executed when a trigger is fired is called trigger action Types of Trigger Fired: 1) Each Row Trigger fired each time the row is affected by trigger statements 2) Each Statement Trigger fired only once for execution of trigger in statements Types of Trigger Changes: 1) Before Trigger Trigger action executes before the triggering statement 2) After Trigger Trigger action executes after the triggering statement
38 Prepared by A.KARTHIKEYAN ( : 9790768919
ORACLE Complete Notes Syntax for Trigger Declaration: SQL> Create Trigger <Trigger name> <After/Before> <insert/update/Delete> on <table name> For <each row/each statement> Declare <Variable declaration>; Begin <Statements>; End; / O/P: Syntax for Drop Trigger: SQL>Drop Trigger <Trigger name>; O/P: SQL> Trigger Dropped Eg for Trigger Concept: SQL> Create trigger Trigger1 After insert on dept For each row Declare Begin Dbms_output.put_line(1 row created in dept table); End; / O/P: SQL> Trigger Created Checking Trigger concept: SQL> Insert into dept values(5,ramesh); O/P: SQL> 1 row created in dept table SQL> 1 row created Syntax for Drop Trigger:
39 Prepared by A.KARTHIKEYAN ( : 9790768919
ORACLE Complete Notes SQL>Drop Trigger Trigger1; O/P: SQL> Trigger Dropped
Syntax for Disable/Enable Trigger: SQL> Alter Trigger <Trigger name> Disable/Enable; Eg: SQL> Alter Trigger trigger1 Enable; Syntax for Disable/Enable all trigger on Tables: SQL> Alter Table <Table Name> Disable/Enable all Triggers; Eg: SQL> Alter Trigger trigger1 Disable all Triggers;
Adds specified text to the end of the current line in the buffer. Specifies where and how formatting will change in a report, or lists the current break definition. Places and formats a specified title at the bottom of each report page, or lists the current BTITLE definition. Changes text on the current line in the buffer. Resets or erases the current clause or setting for the specified option, such as BREAKS or COLUMNS.
41 ( : 9790768919
BREAK
BTITLE
Prepared by A.KARTHIKEYAN
DEFINE
DEL DESCRIBE
DISCONNECT Commits pending changes to the database and logs the current user off Oracle, but does not exit SQL*Plus. EDIT Invokes a host operating system text editor on the contents of the specified file or on the contents of the buffer. Executes a single PL/SQL statement. Terminates SQL*Plus and returns control to the operating system. Loads a host operating system file into the SQL buffer. Accesses the SQL*Plus help system. Executes a host operating system command without leaving SQL*Plus. Adds one or more new lines after the current line in the buffer.
EXECUTE EXIT
INPUT
PAUSE
REMARK RUN
SAVE
SET
SHOW
SHUTDOWN SPOOL
START
TTITLE
VARIABLE
Report Generation: Reports Builder is a visual tool to develop reports Can generate reports based on the data in the data base Provides extensive formatting facilities Use of PL/SQL in reports further enhances the capability Can integrate with other Oracle tools like Oracle Forms and Graphics Parts of Report: The basic requirements of a report are: Data Model Layout Model The Data Model is composed of objects that define the data to be included in the report The Layout Model is composed of objects that define positioning and appearance of data and other objects in the report Various Types of Reports: -Tabular -Form Like -Mailing Label
44 Prepared by A.KARTHIKEYAN ( : 9790768919
ORACLE Complete Notes -Form Letter -Group Left -Group Above -Matrix -Matrix with Group
SQL & PLSQL Commands Query: SQL Commands: 1. Select and display the names of all the tables owned by you. 2. Find out the column names and data types of some of these tables. 3. Select all rows and all columns from these tables. 4. Display empno, ename, deptno, mgr from emp. 5. Display mgr, deptno,empno, ename from emp. 6. Display the above columns again with the column tittles as Employee_no, Name, Manager, Department. 7. Display empno, name, salary deptno in ascending order of employee number. 8. Display empno, name, salary, deptno in descending order of deptno. 9. Display empno, name, salary, deptno in ascending order of salary. 10. Display empno, name, salary, deptno in ascending order of employee name. 11. Display empno, name, salary, deptno in descending order of employee number. 12. Display empno, name, salary, deptno in descending order of salary.
45 Prepared by A.KARTHIKEYAN ( : 9790768919
8. Write a query on emp and dept tables to display the names of the respective departments for every employee. In addition the query should also display the department nos in the dept table but not in emp table along with the name. (Hint: Use outer join.) SQL Expressions and Datatype Functions: 1. Display the employee number, name, salary, commission and salary+ commission for those employees who are salesmen. Name the calculated column as Gross. 2. Display the employee number, name, salary, commission and salary+ 10% of salary for those employees who are not salesmen. Name the calculated column as Revised_Pay. 3. Display the employee number, name, salary, commission for those employees whose salaries are greater than twice their commission.(Hint: use NVL function) 4. Display the employee number, name, salary, commission and salary multiplied by 1.1234 for those employees who are salesmen. (round off the calculated column to 2 digits after decimal). 5. Display the employee number, name, salary, and salary multiplied by 12.975 for those employees who are not salesmen. (truncate the calculated column to 2 digits after decimal). 6. Display the average salary for every department in the employee table. 7. Display the total salary, average salary, maximum salary, no of rows and
49 Prepared by A.KARTHIKEYAN ( : 9790768919
SUB Queries: 1. Display employee number, name, salary, department number from employee table for the department number of Jones. 2. Display employee number, name, salary, department number from employee table for department name accounting. (available in department table). 3. Display employee number, name, salary, department number from employee table for salary > maximum salary. 4. Repeat the above query for salary > minimum salary. 5. Modify the query 3 above to display the salaries > minimum salary for the respective departments. DDL Commands: 1. Create the following tables: Item_master(item code number 5, item_name varchar2 30 unit of measure varchar2 3, unit cost number 12,2) Stock(item code number 5, opening balance date date, opening quantity number 15,2, current stock number 15,2) Grn header(grn_number number 3, grn date date supplier_code varchar2 5, purchase_order_number number 5) Grn detail(grn_number number 3, grn_date date, item_code number 5, quantity number 15,2) Supplier_master(supplier_code varchar2 5, name varchar2 30, address_line1 varchar2 30, address_line2 varchar2 30,
51 Prepared by A.KARTHIKEYAN ( : 9790768919
After every insert , select and verify and then commit . 7. Modify the above values as follows: Item Code 2149 2150 2151 2152 Name printer monitor manuals cables Unit of Measure nos dozen boxes mtr Unit Cost 1900 6000 400 900
Select and verify the changes you have made and commit if they are OK. 8. Delete the following rows: - the row with item code =2152 - the row with item name as manuals - the rows with unit cost >500 9. Select and verify after every delete 10. Roll back 11. Delete all rows. Select and verify 13. Roll back. Select and verify 15. Commit VIEWS Commands: 1. Create a view containing all columns except salary from the employee table for department no 20. 2. Create a view for dept 20 with check option. 3. Update dept no 20 to 30 in the above view and observe the results.
53 Prepared by A.KARTHIKEYAN ( : 9790768919