SlideShare a Scribd company logo
1 of 53
Data Types
By: Prakhar Jha
 CHAR(size)
- used to store fixed length string.
-max size 255 character.
 VARCHAR2(size)
-used to store variable length string .
-it can store letter, number and
punctuation marks.
-max size 4000 character.
• VARCHAR(size)
-same as CHAR(size),but speed is
less.
-it is also used for storing string of
variable length.
• NUMBER(precision , scale)
-’precision’ total no of digits that can
be stored.
-’scale’ total no of digits after decimal.
e.g. : NUMBER(4) or NUMBER(4,2)
or NUMBER.
Actual
Data
Defined
As
Stored As
123456.78
9
NUMBER(
6,2)
1234.79
123456.78
9
NUMBER(
6)
123457
123456.78
9
NUMBER(
6,-2)
123500
123456.78
9
NUMBER 123456.78
9
 DATE
 LONG
-used to store variable length
text, of very large size
-max size 2GB.
Binary Types
• RAW(size)
-used to store binary digit of max
size 255 bytes.
• LONG RAW
-in this data type, it is possible to
store binary data upto 2GB
Object Type
• BLOB
(Binary Large Object)
-this is used for storing binary
data of very large size.
-data stored like images and
videos.
-max size 4GB
• CLOB
(Character Large Object)
-this data type is used to store
very large character object.
-data stored like text document.
-max size 4GB.
*In one table, more than one “lob”
type data type can be stored.
Commands
CREATE AND INSERT
(basic)
 CREATE TABLE <table_name>(<column_name>
<data_type>, <column_name> <data_type>,
<column_name> <data_type>);
 DESC <table_name>;
 INSERT INTO <table_name> VALUES (‘<value>’, ’<value>’ ,
’<value>’);
 INSERT INTO <table_name> VALUES (‘&<value>’,
‘&<value>’, ‘&<value>’);
SELECT and UPDATE and
DELETE
 SELECT * FROM <table_name>;
 SELECT <column_name>,<column_name> FROM
<table_name>;
 SELECT * FROM <table_name> WHERE <column_name> =
‘<value>’;
 UPDATE <table_name> SET <column_name> = ‘<value>’;
 UPDATE <table_name> SET <column_name>=‘<value>’
WHERE <condition>;
 DELETE FROM <table_name>;
 DELETE FROM <table_name> WHERE <condition>;
ALTER and DROP
 ALTER TABLE <table_name> ADD (<column_name>
<data_type>(size));
 ALTER TABLE <table_name> DROP COLUMN
<column_name>;
 ALTER TABLE <table_name> MODIFY(<column_name>
<data_type>(size));
 DROP TABLE <table_name>;
DISTINCT and ORDER BY
 SELECT DISTINCT <column_name> FROM <table_name>;
 SELECT DISTINCT * FROM <table_name>;
 SELECT * FROM <table_name> ORDER BY
<column_name>;
 SELECT * FROM <table_name> ORDER BY
<column_name> DESC;
CREATE and INSERT using
previous table.
 CREATE TABLE <table_name a> (<column_name a>, <column_name
a>,…) AS SELECT <column_name b>, <column_name b>,… FROM
<table_name b>;
 CREATE TABLE <table_name a> (<column_name a>, <column_name
a>,…) AS SELECT <column_name b>, <column_name b>,… FROM
<table_name b> WHERE <any_false_condition>;
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.
 INSERT INTO <table_name b> SELECT <column_name>,
<column_name>,… FROM <table_name a>;
 INSERT INTO <table_name b> SELECT <column_name a>,
<column_name a>,… FROM <table_name a> WHERE <condition>;
RENAME & VIEW TABLES
 RENAME <table_name a> TO <table_nameb>;
 SELECT * FROM TAB;
Data Constraints
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.
It is applied on a particular column.
• CREATE TABLE <table_name> (<column_1> <data_type>(size) PRIMARY KEY,
<column_2> <data_type>(size), <column_3> <data_type> (size));
Column 1 has PRIMARY KEY CONSTRAINT.
• CREATE TABLE <table_name> (<col_1> <data_type>(size), <col_2>
<data_type>(size), <col_3> <data_type>(size) , PRIMARY KEY (col_1,col_2,col_3));
col_1,col_2,col_3 together have PRIMARY KEY CONSTRAINT on them.
AUTO INCREMENT
-to set auto increment in oracle. We have to create SEQUENCE
• CREATE SEQUENCE <name>
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 10;
-now when we insert a value to a primary key column, we will use this
sequence object to insert our new values.
• INSERT INTO <table_name> (<column_name>, <column_name>,
<column_name>) values(<name>.nextval, <value>, <value>);
-<name>.nextval will return the next number in SEQUENCE.
FOREIGN KEY
<table_name a> - Detail Table or Foreign Table.
-when a table is created and FK is described in it, that table is known as Detail Table to
the table where this FK is PK to that table.
<table_name b> - Master Table or Primary Table.
-the actual table whose PK is being described in other table as FK.
• CREATE TABLE <table_name a> (
<col_1> <data_type>(size) PRIMARY KEY,
<col_2> <data_type>(size) REFERENCES <table_name b> [(<column_name>)],
<col_3> <data_type>(size) ) ;
-oracle automatically associates <table_name b>’s PK to <col_2> and described
<col_2> as FK, if we don’t mention <column_name> after “REFERENCE
<table_name>” .
 CREATE TABLE <table_name a>(
<col_1> <data_type>(size) PRIMARY KEY,
<col_2> <data_type>(size)
CONSTRAINT <constraint_name> FOREIGN KEY(col_2)
REFERENCES <table_name b>(<column_name>)
);
 [CONSTRAINT [symbol]] FOREIGN KEY [index_name]
(index_col_name, ...) REFERENCES tbl_name
(index_col_name,...) [ON DELETE reference_option] [ON UPDATE
reference_option]
-reference_option: RESTRICT | CASCADE | SET NULL | NO
ACTION
 CREATE TABLE <table_name a> (<col_1> <data_type>(size) PRIMARY KEY,
<col_2> <data_type>(size), <col_3> <data_type>(size), FOREIGN KEY(col_2)
REFERENCES <table_name b>(<column_name>) );
 CREATE TABLE <table_name a> (<col_1> <data_type>(size) PRIMARY KEY,
<col_2> <data_type>(size), <col_3> <data_type>(size), FOREIGN KEY(col_2)
REFERENCES <table_name b>(<column_name>) ) ON DELETE CASCADE;
- “ON DELETE CASCADE” will delete the associated record from <table_name
a> if any record is deleted in the Master Table or <table_name b>.
 CREATE TABLE <table_name a> (<col_1> <data_type>(size) PRIMARY KEY,
<col_2> <data_type>(size), <col_3> <data_type>(size), FOREIGN KEY(col_2)
REFERENCES <table_name b>(<column_name>) ) ON DELETE SET NULL;
- “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.
• CREATE TABLE <table_name> (
<col 1> <data_type> UNIQUE ,
<col 2> <data_type>,
<col 3> <data_type> );
• CREATE TABLE <table_name> (
<col 1> <data_type> ,
<col 2> <data_type>,
<col 3> <data_type> ,
UNIQUE(col 1));
Business Rule Constraint i.e.
CHECK Constraint
 CREATE TABLE <table_name> (
<col 1> <data_type>(size),
<col 2> <data_type>(size),
<col 3> <data_type>(size) CHECK(<col 3> == 2000),
CHECK(<col 1> LIKE ‘E%’)
);
-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>);
 ALTER TABLE <table_name> ADD PRIMARY
KEY(<col_name>);
 ALTER TABLE <table_name> DROP PRIMARY KEY;
NOT NULL and DEFAULT
 CREATE TABLE <table_name>(
<col 1> <data_type>(size) PRIMARY KEY,
<col 2> <data_type>(size) NOT NULL
);
-the insert query wont execute, if there is no data for <col 2>.
 CREATE TABLE <table_name>(
<col 1> <data_type>(size) PRIMARY KEY,
<col 2> <data_type>(size) DEFAULT <value>
);
-if value is string, wrap it in single quotes.
-if number no need to wrap it.
USER_CONTRAINTS table
 DESC USER_CONSTRAINTS;
 SELECT <column_name> FROM
USER_CONSTRAINTS WHERE
TABLE_NAME =‘<table_name>’ ;
Data Manipulation In SQL
Operators
Arithmetic Comparison
 ‘+’ : Addition
 ‘-’ : Subtraction
 ‘*’ : Multiplication
 ‘/’ : Division
 ‘<’ : Is less than
 ‘>’ : Is greater than
 ‘<=’ : Is less than or equal to
 ‘>=’ : Is greater than or equal
to
 ‘=’ : Is equal to
 ‘!=’ : is NOT equal to
Logical Operators
 AND : Logical AND Operator.
 OR : Logical OR Operator.
 NOT : Logical NOT 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
LIKE Predicate IN and NOT IN Predicate
 SELECT * FROM <table_name>
WHERE <col_name> LIKE
‘E%’;
 SELECT * FROM <table_name>
WHERE <col_name> LIKE
‘%E’;
Assume <col_name> = ‘name’
- when the above query is executed
the output will show all the records
whose ‘name’ are starting with the
letter ‘E’ or ending with letter ‘E’ in
the second query.
 SELECT * FROM <table_name>
WHERE <col_name> IN (‘<value>’);
- the braces after IN can hold multiple
value each of which is separated by
comma.
- the output will contain all records(rows)
where the values of <col_name>
matches to <value>.
 SELECT * FROM <table_name>
WHERE <col_name> NOT IN
(‘<value>’);
-the exact opposite of above query will
happen in this query.
Renaming Columns and Tables
Column Alias Table Alias
 SELECT last_name AS FAMILY,
first_name FROM persons;
-output
FAMILY FIRST_NAME
Jha prakhar
Soni rohit
Yadav ankit
Patel sachin
 SELECT * FROM
<table_name> “<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
Group Function and Scalar
Functions
-function that works on a set of values are
known as Group Functions or Aggregate
Functions, when we apply any function on
a table's column, it generate output
according to all the values that is selected
by the executed query.
-function that works on single value are
known as scalar functions, they are also
known as ‘single row function’ .
Group Functions
 SELECT AVG(marks) AS “Average Marks” FROM STUDENTS ;
 SELECT COUNT(*) AS “No Of Rows” FROM STUDENTS ;
 SELECT COUNT(name) AS “No Of Rows” FROM STUDENTS ;
 SELECT MAX(marks) AS “Maximum Marks” FROM STUDENTS;
 SELECT MIN(marks) AS “Minimum Marks” FROM STUDENTS;
 SELECT SUM(marks) AS “Sum Of Marks” FROM STUDENTS:
- in above example ‘marks’ & ‘name’ are column in table ‘STUDENTS’ .
- ‘ * ’ used above can be replaced by column names as well.
Scalar Function
Numeric Function
 SELECT ABS(-24) FROM DUAL; output: 24
 SELECT POWER(5,2) FROM DUAL; output: 5*5 = 25
 SELECT ROUND(16.966, 1) FROM DUAL; output: 17.0
 SELECT SQRT(25) FROM DUAL; output: 5
 SELECT EXP(2) FROM DUAL; output: 7.3890561
- this function calculates the value of ‘e’ to the power ‘argument’ where ‘ e = 2.71828183 ’ .
 SELECT GREATEST(12, 20, 40) FROM DUAL; output: 40
 SELECT LEAST(12, 20, 40) FORM DUAL; output: 12;
 SELECT MOD(12,5) FROM DUAL; output: 12 divided by 5 and the answer is 2
 SELECT CEIL(82.2) FROM DUAL; output: 83
 SELECT FLOOR(82.2) FROM DUAL; output: 82
String Function
 SELECT LOWER(‘PRAKHAR’) FROM DUAL; output: prakhar
 SELECT UPPER(‘prakhar’) FROM DUAL; output: PRAKHAR
 SELECT INITCAP(‘pRAKhar’) FROM DUAL; output: Prakhar
 SELECT SUBSTR(‘prakhar’,2,5) FORM DUAL; output: rakha
- in above query, oracle will start reading from 2nd position up till ‘a’ which is at 5th position from there.
 SELECT ASCII(‘A’) FROM DUAL; output: 65
 SELECT INSTR(‘prakhar’, ‘a’) FROM DUAL; output: 3
 SELECT INSTR(‘prakhar’, ‘a’, 1, 2) FROM DUAL; output: 6
-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.
 SELECT TRANSLATE(‘prakhar’, ‘ar’ , ‘bs’) FROM DUAL; output: psbkhbs
 SELECT LENGTH(‘cow’) “Length Of String” FROM DUAL; output: 3
 SELECT LTRIM(‘prakhar’ , ‘pr’ ) FORM DUAL; output: akhar
-if no argument is passed i.e. in this case ‘pr’ , by default it will remove the white spaces from the starting.
 SELECT RTRIM(‘prakhar’, ‘khar’) FROM DUAL; output: pra
 SELECT TRIM(‘ prakhar ’) AS “TRIM” FROM DUAL; output: prakhar
 SELECT TRIM(LEADING ‘X’ FROM ‘XXXprakharXXX’ ) FROM DUAL;
output: prakharXXX
 SELECT TRIM(TRAILING ‘X’ FROM ‘XXXprakharXXX’ ) FROM DUAL;
output: XXXprakhar
 SELECT TRIM(BOTH ‘X’ FROM ‘XXXprakharXXX’ ) FROM DUAL; output:
prakhar
 SELECT LPAD(‘prakhar’ , 10, ‘*’) FROM DUAL; output: ***prakhar
 SELECT RPAD(‘prakhar’ , 10, ‘*’) FROM DUAL; output: prakhar***
Conversion Function
 SELECT 2 + TO_NUMBER(‘2’) FROM DUAL; output: 4
 SELECT TO_CHAR(12345, ’00,0000’) FROM DUAL; output: 01,2345
- argument passed in the above query ‘12345’ is the input and ’00,0000’ this is the
format we expect in our output.
 SELECT TO_CHAR(SYSDATE, ‘MONTH DD YYYY’) AS “Date” FROM DUAL; output:
March 24 2015
 SELECT TO_CHAR(SYSDATE, ‘MM DD YYYY’) AS “Date” FROM DUAL; output: 03
24 2015
- 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.
 SELECT TO_DATE(SYSDATE, ‘DD MM YYYY’) “TO DATE” FROM DUAL; output: 24-
03-2015
Date Function
- 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
 SELECT NEXT_DAY(’24-MAR-15’, ‘monday’) FROM DUAL; output: 30-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.
 SELECT MONTHS_BETWEEN( ‘06-JAN-16’ , ’06-APR-15’) FROM DUAL; output: 1
- the above function requires two arguments which are date, and this function will find out the months between
these two dates.
 SELECT TO_CHAR(SYSDATE, ‘DDTH/MM/YY’) FROM DUAL; output: example - 24TH/MAR/15
 SELECT TO_CHAR(SYSDATE, ‘DDSP/MM/YY’) FROM DUAL; output: example – twenty-four/MAR/15
 SELECT TO_DATE(’24/03/2015’, ‘DD/MM/YYYY’) FROM DUAL; output: 24-MAR-15
- 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
Tcs 12000
Tcs 20000
Hp 40000
Hp 30000
TABLE NAME = COMPANY
 SELECT COMPANY, SUM(amount) FROM
COMPANY WHERE COMPANY = ‘Hp’
GROUP BY COMPANY;
- 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 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.
 SELECT COMPANY, SUM(amount) FROM
COMPANY GROUP BY COMPANY HAVING
SUM(amount)>4000;
- We use ‘HAVING’ clause in a situation
where we need further filtration on a result.
- In the above query the ‘group by’ clause will
generate a result and the ‘having’ clause will
provide further filtration on it.
output : Hp 70000
Joins, Sub-queries, and Views
Example table to understand joins
employees Table (j.1) orders Table (j.2)
emp_id (pk) emp_name
E01 Pandey sunil
E02 Dubey ajay
E03 Dubey anand
E04 Singh ashwin
prod_id
(pk)
prod_name emp_id
(fk)
234 Printer E01
657 Table E03
865 Chair E03
Inner Join
Inner join is used for handling complex data. When we need to
extract the data from multiple tables then ‘inner join’ keyword is
used.
 SELECT e.emp_name, o.prod_name FROM orders o INNER
JOIN employees e ON e.emp_id = o.emp_id;
-output:
1- pandey sunil printer
2- dubey anand table
3- dubey anand chair
- the above query can also be formed this way.
 SELECT employees.emp_name, orders.prod_name FROM
orders INNER JOIN employees ON employees.emp_id =
orders.emp_id;
Right Join
This join works in a way that, the output of the query with right join will contain all the rows
that are present in the table whose name is written on the right side of this keyword.
 SELECT e.emp_name, o.prod_name FROM orders o RIGHT JOIN employees e ON
e.emp_id = o.emp_id;
-output:
1- pandey sunil printer
2- dubey anand table
3- dubey anand chair
4- singh ashwin
5- dubey ajay
- the above query can also be formed this way.
 SELECT employees.emp_name, orders.prod_name FROM orders INNER JOIN
employees ON employees.emp_id = orders.emp_id;
Left Join
This join works in a way that, the output of the query with left join will contain all the rows
that are present in the table whose name is written on the left side of this keyword.
 SELECT e.emp_name, o.prod_name FROM orders o LEFT JOIN employees e ON
e.emp_id = o.emp_id;
-output:
1- pandey sunil printer
2- dubey anand table
3- dubey anand chair
- the above query can also be formed this way.
 SELECT employees.emp_name, orders.prod_name FROM orders LEFT JOIN
employees ON employees.emp_id = orders.emp_id;
Self Join table( j.3 )
Id Name Type
1 Teacher 1
2 Student 1 0
3 Student 2 0
- Let this table name be “persons”
- In self join we treat one single
table as two different table.
- All the columns in this table and
all the example tables have data
type VARCHAR2(10).
 SELECT teacher.name “Teacher
Name”, student.name “Student”
FROM persons teacher, person
student where student.type=‘0’
and teacher.type=‘1’;
-output:
Teacher Name Student Name
1 Teacher Student 1
2 Teacher Student 2
Cross Join
Sr_No Course_Name
1 BCA
2 BSC
Table: courses
Sr_no Sub_Name
1 oracle
2 java
Table: subjects
 SELECT c.course_name,
s.subject_name FROM
courses c CROSS JOIN
subjects s;
 SELECT
courses.course_name,
subjects.subject_name
FROM courses CROSS JOIN
subjects;
Output:
1. BSC oracle
2. BSC java
3. BCA oracle
4. BSC java
Sub-Queries
- We will use table ‘j.1’ and table ‘j.2’ to
create a example showing how to use sub
queries.
 SELECT * FROM orders WHERE emp_id
IN (SELECT emp_id FROM employees
WHERE emp_name=‘Pandey sunil’);
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;
UNION query output UNION ALL query output
Emp_id
E01
E02
E03
E04
Emp_id
E01
E02
E03
E04
E01
E03
E03
INTERSECT
 SELECT emp_id FROM
employees INTERSECT
SELECT emp_id FROM
orders;
- the only records which
are common in both the
table will be show as the
output of the above
query.
output:
right side.
Emp_id
E01
E03
MINUS
 SELECT emp_id FROM
employees MINUS SELECT
emp_id FROM orders;
- the output of the above
query will contain all the
emp_id’s which are present
in the employees table but
not in the orders table.
- if we swap places of both
the table name in the above
query the output will have
no records to display,
because order table have
emp_id (1, 3) where as in
employees (1,2,3,4).
Emp_id
E02
e04
VIEWS
 They are used for security purposes, so that each column of a
table can only be seen by those who are authorized to see it.
 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 don’t 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.
 Views are of 2 types READ-ONLY and UPDATABLE.
CREATE , SELECT and INSERT
 CREATE VIEW <view_name> AS SELECT <col_name>
“<alias_name>”, <col_name 1> “<alias_name 1>” ,….FROM
<table_name>;
 SELECT <alias_name>, <alias_name1>, … FROM
<view_name>;
 INSERT INTO <view_name> values (‘<value>’, ‘<value>’);
 UPDATE <view_name> SET <alias_name> = ‘<value>’ WHERE
<alias_name?> = ‘<value>’;
 DELETE FROM <view_name> WHERE
<alias_name?>=‘<value>’;
 DROP VIEW <view_name>;
Limitations (updatable view)
- Aggregate functions cannot be used.
- DISTINCT, HAVING, GROUP BY clause cannot be used.
- Sub queries cannot be used.
- UNION, INTERSECT and MINUS cannot be used.
- Constant, Strings and Expressions.
If one view is defined by another the other view must be
updatable.

More Related Content

What's hot (20)

Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)Les01 (retrieving data using the sql select statement)
Les01 (retrieving data using the sql select statement)
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Introduction to-sql
Introduction to-sqlIntroduction to-sql
Introduction to-sql
 
Sql commands
Sql commandsSql commands
Sql commands
 
SQL Constraints
SQL ConstraintsSQL Constraints
SQL Constraints
 
Sql delete, truncate, drop statements
Sql delete, truncate, drop statementsSql delete, truncate, drop statements
Sql delete, truncate, drop statements
 
Basic sql Commands
Basic sql CommandsBasic sql Commands
Basic sql Commands
 
Structured query language(sql)ppt
Structured query language(sql)pptStructured query language(sql)ppt
Structured query language(sql)ppt
 
PostgreSQL
PostgreSQLPostgreSQL
PostgreSQL
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
PL/SQL TRIGGERS
PL/SQL TRIGGERSPL/SQL TRIGGERS
PL/SQL TRIGGERS
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
SQL Functions and Operators
SQL Functions and OperatorsSQL Functions and Operators
SQL Functions and Operators
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Procedures
 
Sql select
Sql select Sql select
Sql select
 
MySQL for beginners
MySQL for beginnersMySQL for beginners
MySQL for beginners
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
Javascript arrays
Javascript arraysJavascript arrays
Javascript arrays
 
Oracle Database Trigger
Oracle Database TriggerOracle Database Trigger
Oracle Database Trigger
 

Viewers also liked

Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQLGary Myers
 
Basic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database AdministratorsBasic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database AdministratorsSveta Smirnova
 
Oracle Database Management Basic 1
Oracle Database Management Basic 1Oracle Database Management Basic 1
Oracle Database Management Basic 1Chien Chung Shen
 
Oracle architecture with details-yogiji creations
Oracle architecture with details-yogiji creationsOracle architecture with details-yogiji creations
Oracle architecture with details-yogiji creationsYogiji Creations
 
Introduction to Oracle Database
Introduction to Oracle DatabaseIntroduction to Oracle Database
Introduction to Oracle Databasepuja_dhar
 
Oracle Basics and Architecture
Oracle Basics and ArchitectureOracle Basics and Architecture
Oracle Basics and ArchitectureSidney Chen
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questionsPyadav010186
 
Best Practices - PHP and the Oracle Database
Best Practices - PHP and the Oracle DatabaseBest Practices - PHP and the Oracle Database
Best Practices - PHP and the Oracle DatabaseChristopher Jones
 
Oracle Database Overview
Oracle Database OverviewOracle Database Overview
Oracle Database Overviewhonglee71
 
Basic oracle for developer&beginner
Basic oracle for developer&beginnerBasic oracle for developer&beginner
Basic oracle for developer&beginnermaclean liu
 
Oracle architecture ppt
Oracle architecture pptOracle architecture ppt
Oracle architecture pptDeepak Shetty
 

Viewers also liked (15)

Sql Objects And PL/SQL
Sql Objects And PL/SQLSql Objects And PL/SQL
Sql Objects And PL/SQL
 
Basic Oracle Usage v1
Basic Oracle Usage v1Basic Oracle Usage v1
Basic Oracle Usage v1
 
Basic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database AdministratorsBasic MySQL Troubleshooting for Oracle Database Administrators
Basic MySQL Troubleshooting for Oracle Database Administrators
 
Basic of Oracle Application
Basic of Oracle ApplicationBasic of Oracle Application
Basic of Oracle Application
 
Oracle Database Management Basic 1
Oracle Database Management Basic 1Oracle Database Management Basic 1
Oracle Database Management Basic 1
 
Oracle architecture with details-yogiji creations
Oracle architecture with details-yogiji creationsOracle architecture with details-yogiji creations
Oracle architecture with details-yogiji creations
 
Introduction to Oracle Database
Introduction to Oracle DatabaseIntroduction to Oracle Database
Introduction to Oracle Database
 
Oracle Essentials Oracle Database 11g
Oracle Essentials   Oracle Database 11gOracle Essentials   Oracle Database 11g
Oracle Essentials Oracle Database 11g
 
Oracle Basics and Architecture
Oracle Basics and ArchitectureOracle Basics and Architecture
Oracle Basics and Architecture
 
Sql queries interview questions
Sql queries interview questionsSql queries interview questions
Sql queries interview questions
 
Oracle: Basic SQL
Oracle: Basic SQLOracle: Basic SQL
Oracle: Basic SQL
 
Best Practices - PHP and the Oracle Database
Best Practices - PHP and the Oracle DatabaseBest Practices - PHP and the Oracle Database
Best Practices - PHP and the Oracle Database
 
Oracle Database Overview
Oracle Database OverviewOracle Database Overview
Oracle Database Overview
 
Basic oracle for developer&beginner
Basic oracle for developer&beginnerBasic oracle for developer&beginner
Basic oracle for developer&beginner
 
Oracle architecture ppt
Oracle architecture pptOracle architecture ppt
Oracle architecture ppt
 

Similar to Oracle basic queries (20)

DDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using OracleDDL(Data defination Language ) Using Oracle
DDL(Data defination Language ) Using Oracle
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
 
Sql
SqlSql
Sql
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
Les10
Les10Les10
Les10
 
MySql slides (ppt)
MySql slides (ppt)MySql slides (ppt)
MySql slides (ppt)
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
MySQL Database System Hiep Dinh
MySQL Database System Hiep DinhMySQL Database System Hiep Dinh
MySQL Database System Hiep Dinh
 
mysqlHiep.ppt
mysqlHiep.pptmysqlHiep.ppt
mysqlHiep.ppt
 
SQL
SQLSQL
SQL
 
SQL report
SQL reportSQL report
SQL report
 
Select To Order By
Select  To  Order BySelect  To  Order By
Select To Order By
 
SQL
SQLSQL
SQL
 
Query
QueryQuery
Query
 
e computer notes - Creating and managing tables
e computer notes -  Creating and managing tablese computer notes -  Creating and managing tables
e computer notes - Creating and managing tables
 
Sql
SqlSql
Sql
 
Oracle Material.pdf
Oracle Material.pdfOracle Material.pdf
Oracle Material.pdf
 
Lab
LabLab
Lab
 
Mysql
MysqlMysql
Mysql
 
Module 3
Module 3Module 3
Module 3
 

Recently uploaded

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 

Recently uploaded (20)

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 

Oracle basic queries

  • 2.  CHAR(size) - used to store fixed length string. -max size 255 character.  VARCHAR2(size) -used to store variable length string . -it can store letter, number and punctuation marks. -max size 4000 character. • VARCHAR(size) -same as CHAR(size),but speed is less. -it is also used for storing string of variable length. • NUMBER(precision , scale) -’precision’ total no of digits that can be stored. -’scale’ total no of digits after decimal. e.g. : NUMBER(4) or NUMBER(4,2) or NUMBER. Actual Data Defined As Stored As 123456.78 9 NUMBER( 6,2) 1234.79 123456.78 9 NUMBER( 6) 123457 123456.78 9 NUMBER( 6,-2) 123500 123456.78 9 NUMBER 123456.78 9
  • 3.  DATE  LONG -used to store variable length text, of very large size -max size 2GB. Binary Types • RAW(size) -used to store binary digit of max size 255 bytes. • LONG RAW -in this data type, it is possible to store binary data upto 2GB Object Type • BLOB (Binary Large Object) -this is used for storing binary data of very large size. -data stored like images and videos. -max size 4GB • CLOB (Character Large Object) -this data type is used to store very large character object. -data stored like text document. -max size 4GB. *In one table, more than one “lob” type data type can be stored.
  • 5. CREATE AND INSERT (basic)  CREATE TABLE <table_name>(<column_name> <data_type>, <column_name> <data_type>, <column_name> <data_type>);  DESC <table_name>;  INSERT INTO <table_name> VALUES (‘<value>’, ’<value>’ , ’<value>’);  INSERT INTO <table_name> VALUES (‘&<value>’, ‘&<value>’, ‘&<value>’);
  • 6. SELECT and UPDATE and DELETE  SELECT * FROM <table_name>;  SELECT <column_name>,<column_name> FROM <table_name>;  SELECT * FROM <table_name> WHERE <column_name> = ‘<value>’;  UPDATE <table_name> SET <column_name> = ‘<value>’;  UPDATE <table_name> SET <column_name>=‘<value>’ WHERE <condition>;  DELETE FROM <table_name>;  DELETE FROM <table_name> WHERE <condition>;
  • 7. ALTER and DROP  ALTER TABLE <table_name> ADD (<column_name> <data_type>(size));  ALTER TABLE <table_name> DROP COLUMN <column_name>;  ALTER TABLE <table_name> MODIFY(<column_name> <data_type>(size));  DROP TABLE <table_name>;
  • 8. DISTINCT and ORDER BY  SELECT DISTINCT <column_name> FROM <table_name>;  SELECT DISTINCT * FROM <table_name>;  SELECT * FROM <table_name> ORDER BY <column_name>;  SELECT * FROM <table_name> ORDER BY <column_name> DESC;
  • 9. CREATE and INSERT using previous table.  CREATE TABLE <table_name a> (<column_name a>, <column_name a>,…) AS SELECT <column_name b>, <column_name b>,… FROM <table_name b>;  CREATE TABLE <table_name a> (<column_name a>, <column_name a>,…) AS SELECT <column_name b>, <column_name b>,… FROM <table_name b> WHERE <any_false_condition>; 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.  INSERT INTO <table_name b> SELECT <column_name>, <column_name>,… FROM <table_name a>;  INSERT INTO <table_name b> SELECT <column_name a>, <column_name a>,… FROM <table_name a> WHERE <condition>;
  • 10. RENAME & VIEW TABLES  RENAME <table_name a> TO <table_nameb>;  SELECT * FROM TAB;
  • 12. 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. It is applied on a particular column. • CREATE TABLE <table_name> (<column_1> <data_type>(size) PRIMARY KEY, <column_2> <data_type>(size), <column_3> <data_type> (size)); Column 1 has PRIMARY KEY CONSTRAINT. • CREATE TABLE <table_name> (<col_1> <data_type>(size), <col_2> <data_type>(size), <col_3> <data_type>(size) , PRIMARY KEY (col_1,col_2,col_3)); col_1,col_2,col_3 together have PRIMARY KEY CONSTRAINT on them.
  • 13. AUTO INCREMENT -to set auto increment in oracle. We have to create SEQUENCE • CREATE SEQUENCE <name> MINVALUE 1 START WITH 1 INCREMENT BY 1 CACHE 10; -now when we insert a value to a primary key column, we will use this sequence object to insert our new values. • INSERT INTO <table_name> (<column_name>, <column_name>, <column_name>) values(<name>.nextval, <value>, <value>); -<name>.nextval will return the next number in SEQUENCE.
  • 14. FOREIGN KEY <table_name a> - Detail Table or Foreign Table. -when a table is created and FK is described in it, that table is known as Detail Table to the table where this FK is PK to that table. <table_name b> - Master Table or Primary Table. -the actual table whose PK is being described in other table as FK. • CREATE TABLE <table_name a> ( <col_1> <data_type>(size) PRIMARY KEY, <col_2> <data_type>(size) REFERENCES <table_name b> [(<column_name>)], <col_3> <data_type>(size) ) ; -oracle automatically associates <table_name b>’s PK to <col_2> and described <col_2> as FK, if we don’t mention <column_name> after “REFERENCE <table_name>” .  CREATE TABLE <table_name a>( <col_1> <data_type>(size) PRIMARY KEY, <col_2> <data_type>(size) CONSTRAINT <constraint_name> FOREIGN KEY(col_2) REFERENCES <table_name b>(<column_name>) );
  • 15.  [CONSTRAINT [symbol]] FOREIGN KEY [index_name] (index_col_name, ...) REFERENCES tbl_name (index_col_name,...) [ON DELETE reference_option] [ON UPDATE reference_option] -reference_option: RESTRICT | CASCADE | SET NULL | NO ACTION
  • 16.  CREATE TABLE <table_name a> (<col_1> <data_type>(size) PRIMARY KEY, <col_2> <data_type>(size), <col_3> <data_type>(size), FOREIGN KEY(col_2) REFERENCES <table_name b>(<column_name>) );  CREATE TABLE <table_name a> (<col_1> <data_type>(size) PRIMARY KEY, <col_2> <data_type>(size), <col_3> <data_type>(size), FOREIGN KEY(col_2) REFERENCES <table_name b>(<column_name>) ) ON DELETE CASCADE; - “ON DELETE CASCADE” will delete the associated record from <table_name a> if any record is deleted in the Master Table or <table_name b>.  CREATE TABLE <table_name a> (<col_1> <data_type>(size) PRIMARY KEY, <col_2> <data_type>(size), <col_3> <data_type>(size), FOREIGN KEY(col_2) REFERENCES <table_name b>(<column_name>) ) ON DELETE SET NULL; - “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.
  • 17. 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. • CREATE TABLE <table_name> ( <col 1> <data_type> UNIQUE , <col 2> <data_type>, <col 3> <data_type> ); • CREATE TABLE <table_name> ( <col 1> <data_type> , <col 2> <data_type>, <col 3> <data_type> , UNIQUE(col 1));
  • 18. Business Rule Constraint i.e. CHECK Constraint  CREATE TABLE <table_name> ( <col 1> <data_type>(size), <col 2> <data_type>(size), <col 3> <data_type>(size) CHECK(<col 3> == 2000), CHECK(<col 1> LIKE ‘E%’) ); -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>)
  • 19. Defining Integrity Constraints by ALTER command  ALTER TABLE <table_name a> ADD FOREIGN KEY(<col_name>) REFERENCES <table_name b>(<col_name>);  ALTER TABLE <table_name> ADD PRIMARY KEY(<col_name>);  ALTER TABLE <table_name> DROP PRIMARY KEY;
  • 20. NOT NULL and DEFAULT  CREATE TABLE <table_name>( <col 1> <data_type>(size) PRIMARY KEY, <col 2> <data_type>(size) NOT NULL ); -the insert query wont execute, if there is no data for <col 2>.  CREATE TABLE <table_name>( <col 1> <data_type>(size) PRIMARY KEY, <col 2> <data_type>(size) DEFAULT <value> ); -if value is string, wrap it in single quotes. -if number no need to wrap it.
  • 21. USER_CONTRAINTS table  DESC USER_CONSTRAINTS;  SELECT <column_name> FROM USER_CONSTRAINTS WHERE TABLE_NAME =‘<table_name>’ ;
  • 23. Operators Arithmetic Comparison  ‘+’ : Addition  ‘-’ : Subtraction  ‘*’ : Multiplication  ‘/’ : Division  ‘<’ : Is less than  ‘>’ : Is greater than  ‘<=’ : Is less than or equal to  ‘>=’ : Is greater than or equal to  ‘=’ : Is equal to  ‘!=’ : is NOT equal to
  • 24. Logical Operators  AND : Logical AND Operator.  OR : Logical OR Operator.  NOT : Logical NOT 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)
  • 25. Using AND and OR together  SELECT * FROM EMPLOYEE WHERE salary > 25000 AND ( designation= ‘mngr’ OR designation= ‘sec’ );
  • 26. 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.
  • 27. Pattern Matching LIKE Predicate IN and NOT IN Predicate  SELECT * FROM <table_name> WHERE <col_name> LIKE ‘E%’;  SELECT * FROM <table_name> WHERE <col_name> LIKE ‘%E’; Assume <col_name> = ‘name’ - when the above query is executed the output will show all the records whose ‘name’ are starting with the letter ‘E’ or ending with letter ‘E’ in the second query.  SELECT * FROM <table_name> WHERE <col_name> IN (‘<value>’); - the braces after IN can hold multiple value each of which is separated by comma. - the output will contain all records(rows) where the values of <col_name> matches to <value>.  SELECT * FROM <table_name> WHERE <col_name> NOT IN (‘<value>’); -the exact opposite of above query will happen in this query.
  • 28. Renaming Columns and Tables Column Alias Table Alias  SELECT last_name AS FAMILY, first_name FROM persons; -output FAMILY FIRST_NAME Jha prakhar Soni rohit Yadav ankit Patel sachin  SELECT * FROM <table_name> “<table _alias>” ;
  • 29. 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
  • 31. Group Function and Scalar Functions -function that works on a set of values are known as Group Functions or Aggregate Functions, when we apply any function on a table's column, it generate output according to all the values that is selected by the executed query. -function that works on single value are known as scalar functions, they are also known as ‘single row function’ .
  • 32. Group Functions  SELECT AVG(marks) AS “Average Marks” FROM STUDENTS ;  SELECT COUNT(*) AS “No Of Rows” FROM STUDENTS ;  SELECT COUNT(name) AS “No Of Rows” FROM STUDENTS ;  SELECT MAX(marks) AS “Maximum Marks” FROM STUDENTS;  SELECT MIN(marks) AS “Minimum Marks” FROM STUDENTS;  SELECT SUM(marks) AS “Sum Of Marks” FROM STUDENTS: - in above example ‘marks’ & ‘name’ are column in table ‘STUDENTS’ . - ‘ * ’ used above can be replaced by column names as well.
  • 33. Scalar Function Numeric Function  SELECT ABS(-24) FROM DUAL; output: 24  SELECT POWER(5,2) FROM DUAL; output: 5*5 = 25  SELECT ROUND(16.966, 1) FROM DUAL; output: 17.0  SELECT SQRT(25) FROM DUAL; output: 5  SELECT EXP(2) FROM DUAL; output: 7.3890561 - this function calculates the value of ‘e’ to the power ‘argument’ where ‘ e = 2.71828183 ’ .  SELECT GREATEST(12, 20, 40) FROM DUAL; output: 40  SELECT LEAST(12, 20, 40) FORM DUAL; output: 12;  SELECT MOD(12,5) FROM DUAL; output: 12 divided by 5 and the answer is 2  SELECT CEIL(82.2) FROM DUAL; output: 83  SELECT FLOOR(82.2) FROM DUAL; output: 82
  • 34. String Function  SELECT LOWER(‘PRAKHAR’) FROM DUAL; output: prakhar  SELECT UPPER(‘prakhar’) FROM DUAL; output: PRAKHAR  SELECT INITCAP(‘pRAKhar’) FROM DUAL; output: Prakhar  SELECT SUBSTR(‘prakhar’,2,5) FORM DUAL; output: rakha - in above query, oracle will start reading from 2nd position up till ‘a’ which is at 5th position from there.  SELECT ASCII(‘A’) FROM DUAL; output: 65  SELECT INSTR(‘prakhar’, ‘a’) FROM DUAL; output: 3  SELECT INSTR(‘prakhar’, ‘a’, 1, 2) FROM DUAL; output: 6 -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.  SELECT TRANSLATE(‘prakhar’, ‘ar’ , ‘bs’) FROM DUAL; output: psbkhbs  SELECT LENGTH(‘cow’) “Length Of String” FROM DUAL; output: 3  SELECT LTRIM(‘prakhar’ , ‘pr’ ) FORM DUAL; output: akhar -if no argument is passed i.e. in this case ‘pr’ , by default it will remove the white spaces from the starting.  SELECT RTRIM(‘prakhar’, ‘khar’) FROM DUAL; output: pra
  • 35.  SELECT TRIM(‘ prakhar ’) AS “TRIM” FROM DUAL; output: prakhar  SELECT TRIM(LEADING ‘X’ FROM ‘XXXprakharXXX’ ) FROM DUAL; output: prakharXXX  SELECT TRIM(TRAILING ‘X’ FROM ‘XXXprakharXXX’ ) FROM DUAL; output: XXXprakhar  SELECT TRIM(BOTH ‘X’ FROM ‘XXXprakharXXX’ ) FROM DUAL; output: prakhar  SELECT LPAD(‘prakhar’ , 10, ‘*’) FROM DUAL; output: ***prakhar  SELECT RPAD(‘prakhar’ , 10, ‘*’) FROM DUAL; output: prakhar***
  • 36. Conversion Function  SELECT 2 + TO_NUMBER(‘2’) FROM DUAL; output: 4  SELECT TO_CHAR(12345, ’00,0000’) FROM DUAL; output: 01,2345 - argument passed in the above query ‘12345’ is the input and ’00,0000’ this is the format we expect in our output.  SELECT TO_CHAR(SYSDATE, ‘MONTH DD YYYY’) AS “Date” FROM DUAL; output: March 24 2015  SELECT TO_CHAR(SYSDATE, ‘MM DD YYYY’) AS “Date” FROM DUAL; output: 03 24 2015 - 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.  SELECT TO_DATE(SYSDATE, ‘DD MM YYYY’) “TO DATE” FROM DUAL; output: 24- 03-2015
  • 37. Date Function - 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  SELECT NEXT_DAY(’24-MAR-15’, ‘monday’) FROM DUAL; output: 30-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.  SELECT MONTHS_BETWEEN( ‘06-JAN-16’ , ’06-APR-15’) FROM DUAL; output: 1 - the above function requires two arguments which are date, and this function will find out the months between these two dates.  SELECT TO_CHAR(SYSDATE, ‘DDTH/MM/YY’) FROM DUAL; output: example - 24TH/MAR/15  SELECT TO_CHAR(SYSDATE, ‘DDSP/MM/YY’) FROM DUAL; output: example – twenty-four/MAR/15  SELECT TO_DATE(’24/03/2015’, ‘DD/MM/YYYY’) FROM DUAL; output: 24-MAR-15 - 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.
  • 38. This is COMPANY table and Grouping functions are applied on it COMPANY AMOUNT Tcs 12000 Tcs 20000 Hp 40000 Hp 30000 TABLE NAME = COMPANY  SELECT COMPANY, SUM(amount) FROM COMPANY WHERE COMPANY = ‘Hp’ GROUP BY COMPANY; - 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 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.  SELECT COMPANY, SUM(amount) FROM COMPANY GROUP BY COMPANY HAVING SUM(amount)>4000; - We use ‘HAVING’ clause in a situation where we need further filtration on a result. - In the above query the ‘group by’ clause will generate a result and the ‘having’ clause will provide further filtration on it. output : Hp 70000
  • 40. Example table to understand joins employees Table (j.1) orders Table (j.2) emp_id (pk) emp_name E01 Pandey sunil E02 Dubey ajay E03 Dubey anand E04 Singh ashwin prod_id (pk) prod_name emp_id (fk) 234 Printer E01 657 Table E03 865 Chair E03
  • 41. Inner Join Inner join is used for handling complex data. When we need to extract the data from multiple tables then ‘inner join’ keyword is used.  SELECT e.emp_name, o.prod_name FROM orders o INNER JOIN employees e ON e.emp_id = o.emp_id; -output: 1- pandey sunil printer 2- dubey anand table 3- dubey anand chair - the above query can also be formed this way.  SELECT employees.emp_name, orders.prod_name FROM orders INNER JOIN employees ON employees.emp_id = orders.emp_id;
  • 42. Right Join This join works in a way that, the output of the query with right join will contain all the rows that are present in the table whose name is written on the right side of this keyword.  SELECT e.emp_name, o.prod_name FROM orders o RIGHT JOIN employees e ON e.emp_id = o.emp_id; -output: 1- pandey sunil printer 2- dubey anand table 3- dubey anand chair 4- singh ashwin 5- dubey ajay - the above query can also be formed this way.  SELECT employees.emp_name, orders.prod_name FROM orders INNER JOIN employees ON employees.emp_id = orders.emp_id;
  • 43. Left Join This join works in a way that, the output of the query with left join will contain all the rows that are present in the table whose name is written on the left side of this keyword.  SELECT e.emp_name, o.prod_name FROM orders o LEFT JOIN employees e ON e.emp_id = o.emp_id; -output: 1- pandey sunil printer 2- dubey anand table 3- dubey anand chair - the above query can also be formed this way.  SELECT employees.emp_name, orders.prod_name FROM orders LEFT JOIN employees ON employees.emp_id = orders.emp_id;
  • 44. Self Join table( j.3 ) Id Name Type 1 Teacher 1 2 Student 1 0 3 Student 2 0 - Let this table name be “persons” - In self join we treat one single table as two different table. - All the columns in this table and all the example tables have data type VARCHAR2(10).  SELECT teacher.name “Teacher Name”, student.name “Student” FROM persons teacher, person student where student.type=‘0’ and teacher.type=‘1’; -output: Teacher Name Student Name 1 Teacher Student 1 2 Teacher Student 2
  • 45. Cross Join Sr_No Course_Name 1 BCA 2 BSC Table: courses Sr_no Sub_Name 1 oracle 2 java Table: subjects  SELECT c.course_name, s.subject_name FROM courses c CROSS JOIN subjects s;  SELECT courses.course_name, subjects.subject_name FROM courses CROSS JOIN subjects; Output: 1. BSC oracle 2. BSC java 3. BCA oracle 4. BSC java
  • 46. Sub-Queries - We will use table ‘j.1’ and table ‘j.2’ to create a example showing how to use sub queries.  SELECT * FROM orders WHERE emp_id IN (SELECT emp_id FROM employees WHERE emp_name=‘Pandey sunil’); output: 234 printer E01
  • 47. 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.
  • 48. SELECT emp_id FROM employees UNION SELECT emp_id FROM orders; SELECT emp_id FROM employees UNION ALL SELECT emp_id FROM orders; UNION query output UNION ALL query output Emp_id E01 E02 E03 E04 Emp_id E01 E02 E03 E04 E01 E03 E03
  • 49. INTERSECT  SELECT emp_id FROM employees INTERSECT SELECT emp_id FROM orders; - the only records which are common in both the table will be show as the output of the above query. output: right side. Emp_id E01 E03
  • 50. MINUS  SELECT emp_id FROM employees MINUS SELECT emp_id FROM orders; - the output of the above query will contain all the emp_id’s which are present in the employees table but not in the orders table. - if we swap places of both the table name in the above query the output will have no records to display, because order table have emp_id (1, 3) where as in employees (1,2,3,4). Emp_id E02 e04
  • 51. VIEWS  They are used for security purposes, so that each column of a table can only be seen by those who are authorized to see it.  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 don’t 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.  Views are of 2 types READ-ONLY and UPDATABLE.
  • 52. CREATE , SELECT and INSERT  CREATE VIEW <view_name> AS SELECT <col_name> “<alias_name>”, <col_name 1> “<alias_name 1>” ,….FROM <table_name>;  SELECT <alias_name>, <alias_name1>, … FROM <view_name>;  INSERT INTO <view_name> values (‘<value>’, ‘<value>’);  UPDATE <view_name> SET <alias_name> = ‘<value>’ WHERE <alias_name?> = ‘<value>’;  DELETE FROM <view_name> WHERE <alias_name?>=‘<value>’;  DROP VIEW <view_name>;
  • 53. Limitations (updatable view) - Aggregate functions cannot be used. - DISTINCT, HAVING, GROUP BY clause cannot be used. - Sub queries cannot be used. - UNION, INTERSECT and MINUS cannot be used. - Constant, Strings and Expressions. If one view is defined by another the other view must be updatable.