Sqlplus Oracle Basic
Sqlplus Oracle Basic
Sqlplus Oracle Basic
SQL PLUS
CHAR(size) Actual Defined Stored As
- used to store fixed length string. Data As
-max size 255 character.
123456.78 NUMBER( 1234.79
9 6,2)
VARCHAR2(size)
-used to store variable length string . 123456.78 NUMBER( 123457
-it can store letter, number and 9 6)
punctuation marks.
-max size 4000 character.
123456.78 NUMBER( 123500
VARCHAR(size) 9 6,-2)
-same as CHAR(size),but speed is less.
-it is also used for storing string of variable
123456.78 NUMBER 123456.78
length.
9 9
NUMBER(precision , scale)
-precision total no of digits that can be
stored.
-scale total no of digits after decimal.
LONG BLOB
-used to store variable length text, (Binary Large Object)
of very large size -this is used for storing binary data
-max size 2GB. of very large size.
-data stored like images and videos.
Binary Types -max size 4GB
CLOB
RAW(size)
(Character Large Object)
-used to store binary digit of max
-this data type is used to store very
size 255 bytes.
large character object.
LONG RAW
-data stored like text document.
-in this data type, it is possible to
-max size 4GB.
store binary data upto 2GB
SQL PLUS
CREATE AND INSERT
(basic)
CREATE TABLE <table_name>(<column_name>
<data_type>, <column_name> <data_type>,
<column_name> <data_type>);
DESC <table_name>;
The above query will create a new table same as <table_name b> but with no
records. This is because the condition can never be fulfilled.
SQL PLUS
PRIMARY KEY and COMPOSITE
KEY
To identify each row uniquely in a table in RDBMS, the concept of primary key is developed.
If it is not possible to identify data uniquely based on single column, PRIMARY KEY is
applied on multiple column and that thing is known as COMPOSITE KEY.
-now when we insert a value to a primary key column, we will use this
sequence object to insert our new values.
-oracle automatically associates <table_name b>s PK to <col_2> and described <col_2> as FK, if
we dont mention <column_name> after REFERENCE <table_name> .
- ON DELETE SET NULL will set NULL in <table_name a> at the places
where the record is associated to the Master Table or <table_name b>, if the
record is deleted from master table.
UNIQUE KEY
- UK constraint is similar to PK.
-if we apply UK constraint on any column that column can store NULL values in
them, this thing is not possible with PK contraint.
-a column with UK constraint on it can have multiple NULL value, but when there
is actual value stored, no two values can be similar.
-in the above query the CHECK constraint is applied on <col 1> & <col 3>
- in <col 1>, it will check if the value which is being inserted, starts from E or
not.
- in <col 3>, it will check if the value which is being inserted, equals to 2000
or not.
Syntax :
CHECK(<logical_expression>)
Defining Integrity Constraints by
ALTER command
ALTER TABLE <table_name a> ADD FOREIGN
KEY(<col_name>) REFERENCES <table_name
b>(<col_name>);
-the insert query wont execute, if there is no data for <col 2>.
SQL PLUS
Operators
< : Is less than
* : Multiplication = : Is equal to
OR : Logical OR Operator.
examples:
AND
SELECT * FROM EMPLOYEES WHERE
SALARY > 10000 AND SALARY < 25000;
OR
SELECT * FROM SUBJECTS WHERE
NAME = java AND NAME < oracle ;
NOT
SELECT * FROM EMPLOYEE WHERE
NOT(SALARY > 25000)
Using AND and OR
together
SELECT * FROM EMPLOYEE
WHERE
salary > 25000 AND ( designation= mngr OR designation=
sec );
Range Searching
SELECT * FROM <table_name>
WHERE <col_name> BETWEEN <value 1> AND <value
2> ;
- in the output, the tuple(row) with <value 1> and <value 2> will
be included.
Pattern Matching
SELECT * FROM <table_name> WHERE
SELECT * FROM <table_name> <col_name> IN (<value>);
WHERE <col_name> LIKE
E%; - the braces after IN can hold multiple value
each of which is separated by comma.
SELECT * FROM <table_name> - the output will contain all records(rows)
WHERE <col_name> LIKE where the values of <col_name> matches
%E; to <value>.
-output
FAMILY FIRST_NAME
Jha prakhar
Soni rohit
Yadav ankit
Patel sachin SELECT * FROM
<table_name> <table
_alias> ;
Column Alias Table Alias
DUAL Table in Oracle
-we use SELECT command to retrieve output from a table, and it is
compulsory to write a table name with it. But some time when we need
to perform any Arithmetic Calculation or use some Function, at that
point of time we cannot use any table name because it will return
unexpected results, and using SELECT without table name is
incomplete.
For this purpose we use DUAL table.
DESC DUAL;
SELECT * FROM DUAL;
SELECT 2+2 FROM DUAL; output : 4
SELECT 2*3 FROM DUAL; output: 6
SYSDATE:
SELECT SYSDATE FROM DUAL; output: current date of your system
Oracle Functions
SQL PLUS
Group Function and Scalar
Functions
- this function calculates the value of e to the power argument where e = 2.71828183 .
- in above query, oracle will start reading from 2nd position up till a which is at 5th position from there.
-the above query will search for a , it will start searching from the first occurrence of a and it will search for the a at second position.
-if no argument is passed i.e. in this case pr , by default it will remove the white spaces from the starting.
- argument passed in the above query 12345 is the input and 00,0000 this is the format we
expect in our output.
- in the above 2 queries the argument which we have passed the first one represents input
date, and second one is for the format which we expect.
- in oracle there are some function defined, with the help of them we can perform various tasks upon the values whose type is
DATE.
SELECT ADD_MONTHS(SYSDATE, 6) Add Month FROM DUAL; input: 24-MAR-15 output: 24-SEP-15
- the argument passed in the above question, the first one represents the input date and the second one represents no. of months to
be added to that current date.
SELECT LAST_DAY(SYSDATE) Last Day FROM DUAL; input (SYSDATE): 24-MAR-15 output: 31-MAR-15
- the above function needs two arguments first one is the date and second one is day.
- this function helps us to find the date which will occur on the inputted day, and the date will be after 24-MAR-15 i.e. the inputted
date in above case.
- the above function requires two arguments which are date, and this function will find out the months between these two dates.
- in the above query the first argument is the string date which we want to cast in date formant, the second argument is to tell the
function that in which format we are sending the date.
This is COMPANY table and
Grouping functions are applied on it
COMPANY AMOUNT TABLE NAME = COMPANY
Tcs 20000 - In the above query the table name and the first
column name are both COMPANY
- The output of the query will be executed in a
Hp 40000 manner such that all the rows with the company
name Hp will collapse and become one and all
the data in the amount column against Hp will be
added together.
Hp 30000
SELECT COMPANY, SUM(amount) FROM
COMPANY GROUP BY COMPANY HAVING
SUM(amount)>4000;
output : Hp 70000
Joins, Sub-queries, and Views
SQL PLUS
Example table to understand joins
Id Name Type
Let this table name be persons
1 Teacher 1 In self join we treat one single
2 Student 1 0 table as two different table.
All the columns in this table and
3 Student 2 0 all the example tables have data
type VARCHAR2(10).
-output:
Teacher Name Student Name
1 Teacher Student 1
2 Teacher Student 2
Cross Join
Sr_No Course_Name SELECT c.course_name,
1 BCA s.subject_name FROM courses
2 BSC c CROSS JOIN subjects s;
Table: courses
SELECT
courses.course_name,
subjects.subject_name FROM
Sr_no Sub_Name courses CROSS JOIN subjects;
1 oracle
2 java Output:
output:
234 printer E01
UNION and UNION ALL
UNION helps us to take out records from multiple tables and join them.
The records that are present in the output, there can be multiple
similar records but since we are using UNION only single set of the
total records will be displayed or we can say there will be no similar
records in the output.
Just like UNION, UNION ALL is used the basic difference between
them is that, when we use UNION no duplicate record is displayed.
Well in the case of UNION ALL, all the outputs which are generated by
the individual query are displayed. We will see repetition of records
while using UNION ALL clause.
Using table (j.1) and (j.2) we will query on these table to understand
union and union all more effectively.
SELECT emp_id FROM employees UNION SELECT emp_id FROM orders;
SELECT emp_id FROM employees UNION ALL SELECT emp_id FROM orders;
Emp_id Emp_id
E01 E01
E02 E02
E03 E03
E04 E04
E01
E03
E03
output:
right side.
MINUS
SELECT emp_id FROM Emp_id
employees MINUS SELECT E02
emp_id FROM orders;
e04
- the output of the above query
will contain all the emp_ids
which are present in the
employees table but not in the
orders table.
If for each user we define part-of a particular table so that he can see
only those columns which they are allowed to, the redundancy will
increase and this sort of approach is inefficient, but have the capability
to provide security.
View dont have data stored in them. They read the table and also read
there own implementation so as to know, what are all the columns the
need to show to the user.
We can use views as if they are tables, we can perform CRUD queries.