Oracle Code Examples
SQL Code Examples
-
Creating
Tables
--create
table ddl statement.
Create
table customer
( cust_no integer not null,
cust_name varchar(10) not null );
-
With Primary Key
--Primary
is nothing but which identify the row in a table.
Create table customer
( cust_no number(4) not null primary
key,
cust_name varchar(10) not null);
-
With Check Constraint
--instead
of writing the logic in application program we can enforce a rule as a
check constraint at the data base level
Create table customer
(cust_no number(4) not null constraint
pk_customer primary key,
cust_name varchar(10) not null,
credit_limit number(10,2) check
(
credit_limit between 5,000 and 100000 ),
state_cd char(2) check (
state_cd in ('TX', 'CA', 'WA') ) );
-
Default
--if you
do not specify any value for a particular column then the default value
will be inserted in the column on which you define the default constraint
Create table customer
(cust_no number(4) not null constraint
pk_customer primary key,
cust_name varchar(10) not null,
credit_limit number(10,2) check
(
credit_limit between 5,000 and 100000 ),
state_cd char(2) check (
state_cd in ('TX', 'CA', 'WA') ),
crt_id char(10) default user
)
-
More than one column as Primary Key
--this
is just an example that we can make more than one column as a primary key.
Create table invoice_detail
(inv_no integer not null,
line_no integer not null,
item_no integer not null,
qty integer not null,
constraint pk_inv_detail primary
key ( invoice_no, line_no) );
-
Foreign Key
--a foreign
key is nothing but referential integrity constraint. You cannot have a
value in a foreign key column until you have that value as a primary key
column in another table.
Create table invoice_detail
(inv_no integer not null constraint
fk_inv_detail foreign key references invoice.inv_no,
ling_no integer not null,
item_no integer not null,
qty integer not null,
constraint pk_inv_detail primary
key ( invoice_no, line_no) );
-
Alter Table add column
--used
to add a column to the existing table structure. In general alter table
command used to chage the attributes of a column like increasing the column
length or changing the data type, enabling or disabling the constraints
defined on a column.
Alter table invoice_detail
add ( discount number(4,2)
);
-
Alter Table Modify column
--used
to change the attributes of an existing column like increasing the column
length or changing the data type.
Alter table invoice_detail modify
( discount number(6,2) ) ;
-
Enable a constraint
--to enable
a constraint on a table.
Alter table invoice_detail enable
fk_inv_detail;
-
Disable a constraint
--to disable
a constraint. usually you do this when you load the bulk data into the
table.
Alter table invoice_detail disable
fk_inv_detail;
-
Drop table with cascade
--when
you drop a table which has columns referred by another table then you will
get the error which says reference value exists in another table so you
cannot drop the table, at that time we can use this cascade so that it
actually deletes the rows in child table and then drops the table.
-
Drop a table
--once
you drop a table all the constraints and triggers will get automatically
dropped, if there are any stored procedure references this table those
will become invalid status.
Drop table invoice
-
Create Index
--index
is used to retrieve the data faster.
Create index ind_custname on customer.cust_name;
-
Create unique index
-- we cannot
have two primary keys on a table where as we can have as many unique indexes
on a table. ( some times we need to have unique values in a column or more
other than primary key to enforce the business rules. )
Create unique index ind_lgcy_cust_code
on customer.cust_old_code;
-
Creating a structure of one table
to another
--the
select statement will return 0 rows because the where condition is not
true
Create table customer_copy as select
* from customer where 1 = 2;
-
Creating a copy of a table with another
name
--usually
to create the copy of a table including the rows. ( just to make a copy
)
Create table customer_copy1 as select
* from customer;
-
Creating Sequence
--sequence
is used to generate the seq numbers. used in multi user environment where
there are many inserts
Create Sequence
cust_seq
start with 1
increment by 1
cache 50
no cycle;
-
Creating Sequence increment by 2
--We can
increment the seq number by any number
Create sequence
office_seq
start with 1000
increment by 2
cache 50
no cycle;
-
Creating Synonym
--This
is called private synonym. Only who has permission on the private synonym.
Create Synonym
employee
for
david.employee;
-
Creating Public Synonym
--Public
Synonym can be seen by all the users.
Create public synonym
department
for
david.department;
DML Statements
-
Select all columns
--just
to show how we can select all the columns from a table. We should never
use this type of select statement in the application development.
Select * from
course;
Select * from
students;
Select * from
customers; -- * represents all the columns from a select statement.
-
Select particular
columns
--this
is the way we should write select statements. Even though you are selecting
all the columns its better to specify all the column names respectively.
Select course_id,
course_nm
from course;
Select student_id,
student_nm
from students
where course_nm = 'JavaIII'
-
Concatinating
two strings in SQL
-- is handy
while we need to show some extra information or user friendly text.
--following
sql will give the result in readable format ie like Mike lives in Dallas.
Select student_nm
|| ' lives in ' || city from students;
Select course_nm
|| ' is taught by ' || instructor_name from course where course_id = 88;
-
Using Psedocolumns
--is a
column which does not exists in the table. rowid and rownum are not columns
but we can select from the table, that is called psedocolumns.
Select office_seq.NEXTVAL
from dual;
Select rowid, cust_no
from customer where state = 'TX';
Select rownum, cust_no
from customer where state = 'CA';
-
Using DECODE
--if then
else in the select statment. If state_cd iis TX then display Texas, else
if CA then California else if WA Washington and goes on like this.
Select
cust_no,
cust_name,
DECODE ( state_cd, 'TX', 'Texas',
, 'CA', 'California',
, 'WA', 'Washington')
From customer
Where credit_limit > 20,000;
-
Using NVL
--is used
to check if the column is null, if so send back the second argument as
the value
Select cust_no, cust_name, nvl (
credit_limit , 'No Credit for this customer ' )
From customer
Where state_cd = 'TX'
-
group by
--used
while calculation aggregate functions or group by functions. In the following
example we are trying to find the number of students by course, so we are
grouping the rows by course name and using count function to get the number
of rows in each group. all the aggregate functions will be based on the
grouped rows respectively.
Select course.course_name, count(*)
From course, student
Where course.course_id = student.course_id
Group by course_name;
Select course.course_name, start_date,
count(*)
From course, student
where course.course_id = student.course_id
group by course.course_name, start_date;
-
group by having
--is nothing
but a where clause on a grouped rows
Select course.course_name, city_nm,
count(*)
From course, student
where course.course_id = student.course_id
Group by course_name, city_nm
Having count(*) > 10;
-
Sysdate (Date Functions)
1. Select sysdate from dual;
2. Select to_char(sysdate,
'month-day-yyyy') from dual;
3. Select to_char(sysdate,
'dd-mm-yyyy:hh:mi:ss') from dual;
4. Select sysdate + 25 from
dual;
5. Select sysdate - 20 from
dual;
6. Select add_months ( sysdate,
4) from dual;
7. Select months_between ( sysdate,
to_date('10-JAN-1978', 'DD-MON-YYYY') );
-
Range Queries
--to get
the rows which falls in the small or wide range of values.
Select cust_no, cust_name, state_cd
From customer
Where cust_no between 100 and 250
-
Sub Query
--Inner
most query actually executes the select and returns the rows to the outer
select i.e the inner select results sets will become the filter clause
for the outer query.
Select cust_no, cust_name
From customer
Where invoice_no in (
select invoice_no
from invoice_detail
where state_cd = 'TX'
group by invoice_no
having count(*) > 5 )
-
Correlated Sub Query
-- In correlated
sub query the outer select executes first then for every row retireived
the inner select will get executed. So here we are trying to see in what
courses there are no students. So we are using not exits in the where clause,
if the inner query does not have any row which means there is no student
for the course the outer select will display the row.
Select course_nm, 'No Students for
this course'
from course
where not exists
( select count(*) from students where student.course_id = course.course_id
)
-
Outer Joins
--Retrieving
the matching and un matching rows.
Select course_nm, count(*)
from course, students
where course.course_id = student.course_id(+)
group by course_nm
-
Like statement.
--if you
know starting characters of a name, or partial name then we can use like
clause to retrieve the row from the table. ( If you call a doctor to get
an appointment if you just say starting letters of your first or last name
they can pull your record )
Select cust_nm from customer where
cust_name like 'ABC%'
-
AND, OR, IN, NOT IN
--just
some filtering rows
Select course_nm
from courses, locations
where courses.course_id = locations.course_id
AND locations.state_cd = 'TX'
Select student_id, student_name from
students
where state_cd = 'TX' OR state_cd
= 'OK';
Select student_id, student_name from
students
where state_cd IN ( 'TX', 'OK' )
Select student_id, student_name from
students
where state_cd NOT IN ( 'GA', 'NY'
)
-
Table Aliases
--using
table aliases. Here for course we call that as table alias a and for instructor
table we are calling it as b. Usually its very handy to make your sql more
readable and if the table names are longer then you dont need to type whole
table name, instead specify a short alias name. When you are writing self
join we must use the table alias.
Select a.course_id, b.instructor_name
from course a, instructors b
where a.instructor_id = b.instructor_id;
Self Join
Select a.employee_name, a.emp_salary,
b.manager_name
from employee a, employee b
where b.employee_number
= a.manager_number;
Distinct
Suppose
we want to display the manager names from the employee tables then we can
use distinct so that we wont get duplicate rows.
Select distinct manager_number from
emloyee.
-
Insert into Syntax1 (Single row insert)
--If you
know all the column values
Insert into course
values
( course_seq.NEXTVAL, 'Oracle',
user, sysdate )
-
Insert into Syntax2
--if you
know the partial values ( at least all not null columns )
Insert into course
( course_id, course_nm, crt_id,
crt_date )
values
( course_seq.NEXTVAL, 'SQL Server',
user, sysdate);
-
Insert into Select... (Multi row
insert)
--usually
its insert into a select statement syntax. If the select returns 0 rows
then the insert inserts 0 rows, if one or more it inserts the selected
rows into the table.
Insert into course
select * from course_old
-
Update table
--Changing
the value in a particular column in one or more rows
update course
set course_nm = 'Java'
where course_id = 8
-
Update with a sub query
--Sub query
returns the courses in Dallas and then change the course name for those
courses
update course
set course_nm = 'JavaII'
where course_id in ( select course_id
from locations where location = 'Dallas')
-
Delete a row
--Removing
one row from a table
Delete course where course_id =
23
-
Delete multiple rows
--Removing
one or more rows from a table
Delete course where course_nm =
'Oracle'
-
Truncate the table
--deleting
all the rows from a table. The delete trigger wont fire if you use this
statement.
Truncate table course
Data Control Language
Create an Oracle User
--once
you create an user the user can login to the database after granting resource
to that user. There are many types of grants you can assign to the users.
When we create the user we have to specify the password after that user
can chage the password at any time.
Create user JON identified by monday
Create a Role
--A role
is an oracle object where we can grant a set of permissions as a collection
to a particular job. Example for all customer representatives we can have
a role, when a person joins the company we can assign the role instead
of granting permissions to each table . It easy to maintenance.
Create Role CUSTINFO.
Grant select on customer, cust_address,
cust_hist....
Create a Role
--In the
previous example we created the role and granted set of permissions on
some objects. Now we will write a DCL to assign that role to the user.
You can assign any number of roles to the user
Grant CUSTINFO to JON
PL/SQL Code Examples
Example1
--just a feel of a pl/sql block. Adding 2
numbers and displaying the result
DECLARE
var_num1 number(4);
var_num2 number(4);
result number(6);
BEGIN
var_num1 := 15;
var_num2 := 30;
result := var_num1
+ var_num2;
dbms_output.put_line ( result
);
END;
Example2
--Displaying
numbers 1 to 50 using a Loop simple
DECLARE
var_rnunning_val number(2);
BEGIN
var_running_val := 0;
Loop
var_running := var_running_val + 1;
Dbms_output.put_line ( var_running_val );
If var_running_val > 50 then
Exit;
End If;
End Loop;
END;
Example 3
--getting
feel of PL/SQL writing sql inside the pl/sql block
DECLARE --where
you declare the variables used in this block.
var_custname
varchar(20);
var_custno
number(4);
BEGIN
var_custno
:= 1234; --Assigning a value to the variable.
--embedded select statement to get the customer name by passing the customer
number.
Select
cust_name into var_custname
From
customer where cust_no = var_custno;
--display
the customer name using dbms_ouput package
dbms_output.put_line
( var_custname )
EXCEPTION
WHEN
no_data_found then
-- Handle the no data found exception
raise_application_error ( -25000, 'No data found for customer no 1234');
END;
Example 4
--Writing a loop which calls the a procedure
so many times by passign different arguments. we take an example, a stored
procedure which takes the date as an argument and it summarizes the all
the transaction level data for the reports.
DECLARE
v_run_date DATE;
BEGIN
v_run_date := '01-JAN-01';
Loop
If v_run_Date >= '01-FEB-01' then
Exit;
End If;
summary_package.do_ticket_price_summary( v_run_Date );
v_run_date := v_Run_date + 1;
End Loop;
END;
-
RECORD datatypes
--declaring the record data type and assign
the values for the fields
DECLARE
TYPE courserec RECORD course%ROWTYPE;
course_layour courserec;
BEGIN
course_layout.course_id := 35;
course_layout.course_nm := 'Oracle';
END;
-
Cursor
--how to declare a cursor, open , fetch and
close cursor
DECLARE
CURSOR c_get_customers( state_id
char(2) ) IS
select customer_no from customer
where state = state_id;
l_cust_no customer.customer_no%type;
BEGIN
Open c_get_Customers('TX');
Loop;
Fetch c_get_Customers
into l_cust_no;
Exit when c_get_customers%notfound;
dbms_output.put_line
( l_cust_no);
End Loop;
Close c_get_customers;
END;
-
SavePoints
-
Commit and rollback
-
Exceptions (Pre defined Exception )
--In this example we are handling the no_Data_found
exception (one of the pre defined exception)
--when a select statement does not return
any row in a pl/sql block then oracle raises a pre defined exception called
no_data_found. If you dont handle the exception your program terminates
when that exception occurs.
declare
var_cust_no customer.customer_no%type;
var_cust_no_hold customer.customer_no%type;
var_cust_name_hold customer.customer_name%type;
begin
var_cust_no := &Customer_Number;
select
customer_no, customer_name into
var_cust_no, var_cust_name
from customer
where customer_no = var_cust_no;
dbms_output.put_line ( 'Customer Name is ' || var_cust_name
);
Exception
When no_data_found then
dbms_output.put_line ( 'Customer Number does
not exists in the database ');
End;
-
User Defined Exception
--here we are declaring too_many_students
variable of type exception
--You must
raise the exception in your pl/sql block processing when your business
rule fails.
declare
too_many_students exception;
var_kount number;
var_course_id number;
begin
var_course_id :=
&Course;
select count(*) into
var_kount from students where course_id = 50;
If var_kount > 20 then
raise
too_many_students;
End If;
Exception
When too_many_students then
dbms_output.put_line
( 'Time to create a new class schedule, Crossing the dead line for course
id ' || var_course_id );
End ;
-
raise_application_error
--if you want to terminate the process we can
raise an application error using Raise_application_error. When we use this
statement in triggers all the data manipulation gets rollback automatically,
the error text and number will appear to the called programs
declare
var_credit_limit
number(7, 2);
var_customer
customer.customer_no%type;
var_ret_value
number(2);
begin
var_customer
:= &Customer;
var_ret_value
:= check_customer_eligible_for_credit ( var_customer );
If var_ret_value
< 0 then
Raise_Application_Error ( -20001, 'Customer ' || var_customer ||
' is not eligible for the Credit ' );
End If;
End;
-
Stored Procedure
Its a pl/sql block with a name and can pass arguments
and more over you can save that as a database object in the database. The
advantage is when we create a stored procedure it also saves the execution
plan, so when we are calling this stored procedure again it uses the existing
plan so that it is not wasting any time to figure out the execution plan.
--in the
following stored procedure we update the credit limit for a customer by
passing the credit limit and customer number, if any error then pass the
sql code and sql error desc to the calling program
CREATE PROCEDURE update_customer_credit
( v_cust_no IN customer.customer_no%type,
v_amount IN customer.credit_limit%type,
v_sqlcode IN OUT number,
v_sqlerrtext IN OUT varchar(1000)
) is
BEGIN
update customer set
credit_limit = v_amount where customer_no = v_cust_no;
Exception
When others then
v_sqlcode := SQLERRCODE;
v_sqlerrtext := SQLERRTEXT;
End update_customer_credit;
-
Stored Function
--write a function which converts the local time
to GMT time. In this function we use a oracle date function named new_time
which takes 3 arguments, first argument is the date you want to convert,
second is from which time and lastly to which time you want to convert.
You must return a value from the function.
Create function convert_time_from_zone_to_zone
( var_local_time date,
var_from varchar(3),
var_destination varchar(3)
) return date is
var_gmt_value date;
BEGIN
var_gmt_value
:= new_time ( var_local_time, var_from ,var_destination );
return
var_gmt_value;
End convert_time_from_zone_to_zone;
-
Calling Function from a PL/SQL block
--Here in this example we are converting Eastern
Standard to Greenwich Mean Time. Just call the function by passing all
the arguments expected in any sql.
select convert_time_from_zone_to_zone
( sysdate, 'EST','GMT') from dual;
-
Global Variable (sort of)
-
PL/SQL Table
-
REF Cursor
-
Create Triggers
-
Row Level Trigger
-
Statement Level Trigger
-
Mutuating
You can create a temporary tables in your select statements. You can write
a select statement and call that as table a, create another select statement
call that as table b and join those two temp tables for your result.
Select
a.col1, a.col2, a.col3, b.col1, b.col2
from ( select col1, col2, col3 from table1 where col1 = 123 ) a,
( select col1, col2, col3
from table2 where col1 = 345 ) b
where a.col1 = b.col1;
In the above example the first select statement results is nothing but
a temporary table a and the next one is called b and then join those two
temp tables to get your selects.