SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Basics of Oracle
Priya Goyal
What is Oracle
• Oracle database is a relational database management system. It is
known as Oracle database, OracleDB or simply Oracle. It is produced
and marketed by Oracle Corporation.
• Oracle database is the first database designed for enterprise grid
computing. The enterprise grid computing provides the most flexible
and cost effective way to manage information and applications.
Oracle CREATE TABLE
• CREATE TABLE statement is used to create a new table in the database.
• Syntax:
CREATE TABLE table_name
(
column1 datatype [ NULL | NOT NULL ],
column2 datatype [ NULL | NOT NULL ],
...
column_n datatype [ NULL | NOT NULL ]
);
*Every column should either be defined as "NULL" or "NOT NULL". In the case, the value is left blank; it is treated as "NULL" as
default.
• Example
CREATE TABLE customers
(
customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50)
);
**In Oracle, total number of columns cannot be more than 32.
CREATE TABLE Example with primary key
Example 1:
CREATE TABLE customers
(
customer_id number(10) NOT NULL,
customer_name varchar2(50) NOT NULL,
city varchar2(50),
CONSTRAINT customers_pk PRIMARY KEY (customer_id, customer_name)
);
*In the example above there is only ONE PRIMARY KEY (customers_pk).
However, the VALUE of the primary key is made up of TWO COLUMNS
(customer_id + customer_name).
Example 2:
CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)
*A primary key is a single field or combination of fields that contains a unique record. It must be filled. None of
the field of primary key can contain a null value. A table can have only one primary key.
CREATE TABLE AS Statement
1)copying all columns of another table
CREATE TABLE new_table
AS (SELECT * FROM old_table);
CREATE TABLE newcustomers
AS (SELECT * FROM customers
WHERE customer_id < 5000);
2)copying selected columns of another table
CREATE TABLE new_table
AS (SELECT column_1, column2, ... column_n
FROM old_table);
CREATE TABLE newcustomers2
AS (SELECT customer_id, customer_name
FROM customers
WHERE customer_id < 5000);
3)copying selected columns from multiple tables
CREATE TABLE new_table
AS (SELECT column_1, column2, ... column_n
FROM old_table_1, old_table_2, ... old_table_n);
Let's take an example: Consider that you have already created two tables “Customer" and “Customers".The
table “Customer" has three columns customer_id, customer_name and customer_city. The second table
“Customers" has also three columns customers_id, customers_name and customers_city.
• In the following example, we will create a table name "newcustomer" form copying
columns from both tables.
Example:
CREATE TABLE newcustomer
AS (SELECT customer.customer_id, customer.customer_city, customers.customers_name
FROM customer, customers
WHERE customer.customer_id =customers.customers_id
AND customer.customer_id < 5000);
ALTER TABLE
• ALTER TABLE statement specifies how to add, modify, drop or delete
columns in a table. It is also used to rename a table.
ALTER TABLE table_name
ADD column_name column-definition;
• Consider that already existing table customers. Now, add a new column
customer_age into the table customers.
ALTER TABLE customers
ADD customer_age varchar2(50);
1)Add multiple columns in the existing
table
ALTER TABLE table_name
ADD (column_1 column-definition,
column_2 column-definition,
column_n column_definition);
Example
ALTER TABLE customers
ADD (customer_type varchar2(50),
customer_address varchar2(50));
2)Modify column of a table
ALTER TABLE table_name
MODIFY column_name column_type;
Example:
ALTER TABLE customers
MODIFY customer_name varchar2(100) not null;
3)Modify multiple columns of a
table
ALTER TABLE table_name
MODIFY (column_1 column_type,
column_2 column_type,
column_n column_type);
ALTER TABLE customers
MODIFY (customer_name varchar2(100) not null,
city varchar2(100));
4)Drop column of a table
ALTER TABLE table_name
DROP COLUMN column_name;
ALTER TABLE customers
DROP COLUMN customer_name;
5)rename column of a table
ALTER TABLE table_name
RENAME COLUMN old_name to new_name;
ALTER TABLE customers
RENAME COLUMN customer_name to cname;
6)rename table
ALTER TABLE table_name
RENAME TO new_table_name;
ALTER TABLE customers
RENAME TO retailers;
DROP TABLE Statement
DROP TABLE statement is used to remove or delete a table
from the Oracle database.
DROP [schema_name].TABLE table_name
[ CASCADE CONSTRAINTS ]
[ PURGE ];
Parameters
schema_name: It specifies the name of the schema that owns the table.
table_name: It specifies the name of the table which you want to remove from the
Oracle database.
CASCADE CONSTRAINTS: It is optional. If specified, it will drop all referential
integrity constraints as well.
PURGE: It is also optional. If specified, the table and its dependent objects are
placed in the recycle bin and can’t be recovered.
DROP TABLE customers;
DROP TABLE customers PURGE
This statement will drop the table called customers and issue a PURGE so that the
space associated with the customers table is released and the customers table is
not placed in recycle bin. So, it is not possible to recover that table if required.
SELECT Statement
• The Oracle SELECT statement is used to retrieve data from one or more than one tables, object tables, views,
object views etc.
SELECT expressions
FROM tables
WHERE conditions;
1)select all fields
SELECT *
FROM customers;
2)select specific fields
SELECT age, address, salary
FROM customers
WHERE age < 25
AND salary > '20000'
ORDER BY age ASC, salary DESC;
3)select fields from multiple tables (JOIN)
SELECT customers.name, courses.trainer
FROM courses
INNER JOIN customers
ON courses.course_id = course_id
ORDER BY name;
Insert Statement
1) Inserting a single record using the Values keyword):
INSERT INTO table
(column1, column2, ... column_n )
VALUES
(expression1, expression2, ... expression_n );
INSERT INTO suppliers
(supplier_id, supplier_name)
VALUES
(50, 'Flipkart');
2) Inserting multiple records using a SELECT statement):
INSERT INTO table
(column1, column2, ... column_n )
SELECT expression1, expression2, ... expression_n
FROM source_table
WHERE conditions;
INSERT INTO suppliers
(supplier_id, supplier_name)
SELECT age, address
FROM customers
WHERE age > 20;
You can even check the number of rows that you want to insert by following statement:
SELECT count(*)
FROM customers
WHERE age > 20;
INSERT statement is used to add a single record or multiple records into the table.
INSERT ALL statement
• INSERT ALL statement is used to insert the rows into one table or multiple tables by using
only one SQL command.
INSERT ALL
INTO table_name (column1, column2, column_n) VALUES (expr1, expr2, expr_n)
INTO table_name(column1, column2, column_n) VALUES (expr1, expr2, expr_n)
INTO table_name (column1, column2, column_n) VALUES (expr1, expr2, expr_n)
SELECT * FROM dual;
INSERT ALL
INTO suppliers (supplier_id, supplier_name) VALUES (20, 'Google')
INTO suppliers (supplier_id, supplier_name) VALUES (21, 'Microsoft')
INTO suppliers (supplier_id, supplier_name) VALUES (22, 'Apple')
SELECT * FROM dual;
UPDATE Statement
1) Update column
UPDATE table_name
SET column1 = expression1,
column2 = expression2,
...
column_n = expression_n
WHERE conditions;
UPDATE suppliers
SET supplier_name = 'Kingfisher',
supplier_name = 'Bata shoes'
WHERE supplier_id = 2;
2) Update Table by selecting rocords from another table
UPDATE table_name
SET column1 = (SELECT expression1
FROM table2
WHERE conditions)
WHERE conditions;
UPDATE customers
SET name = (SELECT supplier_name
FROM suppliers
WHERE suppliers.supplier_name = customers.name)
WHERE age < 25;
DELETE Statement
In Oracle, DELETE statement is used to remove or delete a single record or multiple records from a
table.
DELETE FROM table_name
WHERE conditions;
1) Delete on one condition
DELETE FROM customers
WHERE name = 'Sohan';
2) Delete On multiple conditions
DELETE FROM customers
WHERE last_name = 'Maurya'
AND customer_id > 2;
TRUNCATE TABLE
TRUNCATE TABLE statement is used to remove all records from a table. It works
same as DELETE statement but without specifying a WHERE clause. It is generally
used when you don’t have to worry about rolling back
TRUNCATE TABLE [schema_name.]table_name
1) schema_name: This parameter specifies the name of the schema that the table belongs to. It is
optional.
2) table_name: It specifies the table that you want to truncate.
TRUNCATE TABLE customers;
DELETE Table
Delete table is used to delete the table
DELETE TABLE customers;
TRUNCATE TABLE vs DELETE TABLE
Both the statements will remove the data from the "customers" table
but the main difference is that you can roll back the DELETE statement
whereas you can't roll back the TRUNCATE TABLE statement.
DISTINCT Clause
Oracle DISTINCT clause is used to remove the duplicate records from the result set. It is only used
with SELECT statement.
SELECT DISTINCT expressions
FROM tables
WHERE conditions;
SELECT DISTINCT name, age, salary
FROM customers
WHERE age >= '60';
FROM Clause
• FROM clause is a mandatory clause in SELECT expression. It specifies the tables from which data is
to be retrieved.
Syntax:
FROM table_name...
Expressions...
SELECT *
FROM customers
WHERE salary >= 20000
ORDER BY salary ASC;
FROM Clause Example: (with two tables)
Inner Join example:
Let's take two tables "suppliers" and "order1".
Suppliers:
Order1:
Execute the following query:
SELECT suppliers.supplier_id, suppliers.supplier_name, order1.order_number
FROM suppliers
INNER JOIN order1
ON suppliers.supplier_id = order1.supplier_id;
ORDER BY Clause
ORDER BY Clause is used to sort or re-arrange the records in the result set.
The ORDER BY clause is only used with SELECT statement.
SELECT expressions
FROM tables
WHERE conditions
ORDER BY expression [ ASC | DESC ];
ASC: optional parameter, used to sort records in ascending
order.
DESC: optional parameter, used to sort records in descending
order.
SELECT *
FROM supplier
ORDER BY last_name;
SELECT *
FROM supplier
ORDER BY last_name DESC;
GROUP BY Clause
GROUP BY clause is used with SELECT statement to collect data from multiple records and group the
results by one or more columns.
SELECT expression1, expression2, ... expression_n,
aggregate_function (aggregate_expression)
FROM tables
WHERE conditions
GROUP BY expression1, expression2, ... expression_n;
SELECT item, SUM(sale) AS "Total sales"
FROM salesdepartment
GROUP BY item;
HAVING Clause
• HAVING Clause is used with GROUP BY Clause to restrict the groups of returned rows where
condition is TRUE.
SELECT expression1, expression2, ... expression_n,
aggregate_function (aggregate_expression)
FROM tables
WHERE conditions
GROUP BY expression1, expression2, ... expression_n
HAVING having_condition;
• aggregate_function: It specifies the aggregate functions i.e. SUM, COUNT, MIN, MAX or AVG
functions.
• aggregate_expression: It specifies the column or expression on that the aggregate function is
based on.
• having_conditions: It specifies the conditions that are applied only to the aggregated results to
restrict the groups of returned rows.
1) With GROUP BY SUM function 2) With GROUP BY COUNT function
SELECT item, SUM(sale) AS "Total sales"
FROM salesdepartment
GROUP BY item
HAVING SUM(sale) < 1000; SELECT state, COUNT(*) AS "Number of customers"FROM customers
WHERE salary > 10000
GROUP BY state
HAVING COUNT(*) >= 2;
3) with GROUP BY MIN function 4) with GROUP BY MAX function
SELECT department,
MIN(salary) AS "Lowest salary"
FROM employees
GROUP BY department
HAVING MIN(salary) < 15000;
SELECT department,
MAX(salary) AS "Highest salary"
FROM employees
GROUP BY department
HAVING MAX(salary) > 30000;

Weitere ähnliche Inhalte

Was ist angesagt?

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)
Achmad Solichin
 

Was ist angesagt? (20)

PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts PL/SQL Introduction and Concepts
PL/SQL Introduction and Concepts
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Procedures
 
SQL JOINS
SQL JOINSSQL JOINS
SQL JOINS
 
PL/SQL Fundamentals I
PL/SQL Fundamentals IPL/SQL Fundamentals I
PL/SQL Fundamentals I
 
Sql commands
Sql commandsSql commands
Sql commands
 
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
Sql tutorialSql tutorial
Sql tutorial
 
DB2 Interview Questions - Part 1
DB2 Interview Questions - Part 1DB2 Interview Questions - Part 1
DB2 Interview Questions - Part 1
 
SQL
SQLSQL
SQL
 
Introduction to sql
Introduction to sqlIntroduction to sql
Introduction to sql
 
10 Creating Triggers
10 Creating Triggers10 Creating Triggers
10 Creating Triggers
 
SQL Overview
SQL OverviewSQL Overview
SQL Overview
 
Sql Constraints
Sql ConstraintsSql Constraints
Sql Constraints
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
 
SQL Tutorial - Basic Commands
SQL Tutorial - Basic CommandsSQL Tutorial - Basic Commands
SQL Tutorial - Basic Commands
 
Ms sql-server
Ms sql-serverMs sql-server
Ms sql-server
 
SQL - Structured query language introduction
SQL - Structured query language introductionSQL - Structured query language introduction
SQL - Structured query language introduction
 
Including Constraints -Oracle Data base
Including Constraints -Oracle Data base Including Constraints -Oracle Data base
Including Constraints -Oracle Data base
 
Sql ppt
Sql pptSql ppt
Sql ppt
 
1 - Introduction to PL/SQL
1 - Introduction to PL/SQL1 - Introduction to PL/SQL
1 - Introduction to PL/SQL
 

Andere mochten auch

Andere mochten auch (8)

Penentuan kadar ca dan mg serta turbiditas
Penentuan kadar ca dan mg serta turbiditasPenentuan kadar ca dan mg serta turbiditas
Penentuan kadar ca dan mg serta turbiditas
 
Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Jsp
JspJsp
Jsp
 
Oracle Database | Computer Science
Oracle Database | Computer ScienceOracle Database | Computer Science
Oracle Database | Computer Science
 
Introduction to Oracle Database
Introduction to Oracle DatabaseIntroduction to Oracle Database
Introduction to Oracle Database
 
Introduction to oracle database (basic concepts)
Introduction to oracle database (basic concepts)Introduction to oracle database (basic concepts)
Introduction to oracle database (basic concepts)
 
Dbms
DbmsDbms
Dbms
 
Relational Database Design - Lecture 4 - Introduction to Databases (1007156ANR)
Relational Database Design - Lecture 4 - Introduction to Databases (1007156ANR)Relational Database Design - Lecture 4 - Introduction to Databases (1007156ANR)
Relational Database Design - Lecture 4 - Introduction to Databases (1007156ANR)
 

Ähnlich wie Oraclesql

Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
Belle Wx
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql new
SANTOSH RATH
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
Tony Wong
 

Ähnlich wie Oraclesql (20)

Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
Query
QueryQuery
Query
 
Les10
Les10Les10
Les10
 
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
Unit-1 SQL fundamentals.docx SQL commands used to create table, insert values...
 
Les10 Creating And Managing Tables
Les10 Creating And Managing TablesLes10 Creating And Managing Tables
Les10 Creating And Managing Tables
 
SQL
SQLSQL
SQL
 
Sql
SqlSql
Sql
 
Introduction to sql new
Introduction to sql newIntroduction to sql new
Introduction to sql new
 
SQL
SQLSQL
SQL
 
MySQL Essential Training
MySQL Essential TrainingMySQL Essential Training
MySQL Essential Training
 
Lab
LabLab
Lab
 
Database Management System 1
Database Management System 1Database Management System 1
Database Management System 1
 
SQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdfSQL Beginners anishurrehman.cloud.pdf
SQL Beginners anishurrehman.cloud.pdf
 
SQL : Structured Query Language
SQL : Structured Query LanguageSQL : Structured Query Language
SQL : Structured Query Language
 
DBMS.pdf
DBMS.pdfDBMS.pdf
DBMS.pdf
 
Database Architecture and Basic Concepts
Database Architecture and Basic ConceptsDatabase Architecture and Basic Concepts
Database Architecture and Basic Concepts
 
ADBMS Unit-II c
ADBMS Unit-II cADBMS Unit-II c
ADBMS Unit-II c
 
Module 3
Module 3Module 3
Module 3
 
Lecture 3 sql {basics ddl commands}
Lecture 3 sql {basics  ddl commands}Lecture 3 sql {basics  ddl commands}
Lecture 3 sql {basics ddl commands}
 
1 introduction to my sql
1 introduction to my sql1 introduction to my sql
1 introduction to my sql
 

Kürzlich hochgeladen

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
rknatarajan
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
ankushspencer015
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
Tonystark477637
 

Kürzlich hochgeladen (20)

UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
(SHREYA) Chakan Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Esc...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur EscortsCall Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
Call Girls Service Nagpur Tanvi Call 7001035870 Meet With Nagpur Escorts
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 

Oraclesql

  • 2. What is Oracle • Oracle database is a relational database management system. It is known as Oracle database, OracleDB or simply Oracle. It is produced and marketed by Oracle Corporation. • Oracle database is the first database designed for enterprise grid computing. The enterprise grid computing provides the most flexible and cost effective way to manage information and applications.
  • 3. Oracle CREATE TABLE • CREATE TABLE statement is used to create a new table in the database. • Syntax: CREATE TABLE table_name ( column1 datatype [ NULL | NOT NULL ], column2 datatype [ NULL | NOT NULL ], ... column_n datatype [ NULL | NOT NULL ] ); *Every column should either be defined as "NULL" or "NOT NULL". In the case, the value is left blank; it is treated as "NULL" as default. • Example CREATE TABLE customers ( customer_id number(10) NOT NULL, customer_name varchar2(50) NOT NULL, city varchar2(50) ); **In Oracle, total number of columns cannot be more than 32.
  • 4. CREATE TABLE Example with primary key Example 1: CREATE TABLE customers ( customer_id number(10) NOT NULL, customer_name varchar2(50) NOT NULL, city varchar2(50), CONSTRAINT customers_pk PRIMARY KEY (customer_id, customer_name) ); *In the example above there is only ONE PRIMARY KEY (customers_pk). However, the VALUE of the primary key is made up of TWO COLUMNS (customer_id + customer_name). Example 2: CREATE TABLE Persons ( P_Id int NOT NULL PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255), Address varchar(255), City varchar(255) ) *A primary key is a single field or combination of fields that contains a unique record. It must be filled. None of the field of primary key can contain a null value. A table can have only one primary key.
  • 5. CREATE TABLE AS Statement 1)copying all columns of another table CREATE TABLE new_table AS (SELECT * FROM old_table); CREATE TABLE newcustomers AS (SELECT * FROM customers WHERE customer_id < 5000); 2)copying selected columns of another table CREATE TABLE new_table AS (SELECT column_1, column2, ... column_n FROM old_table); CREATE TABLE newcustomers2 AS (SELECT customer_id, customer_name FROM customers WHERE customer_id < 5000);
  • 6. 3)copying selected columns from multiple tables CREATE TABLE new_table AS (SELECT column_1, column2, ... column_n FROM old_table_1, old_table_2, ... old_table_n); Let's take an example: Consider that you have already created two tables “Customer" and “Customers".The table “Customer" has three columns customer_id, customer_name and customer_city. The second table “Customers" has also three columns customers_id, customers_name and customers_city. • In the following example, we will create a table name "newcustomer" form copying columns from both tables. Example: CREATE TABLE newcustomer AS (SELECT customer.customer_id, customer.customer_city, customers.customers_name FROM customer, customers WHERE customer.customer_id =customers.customers_id AND customer.customer_id < 5000);
  • 7. ALTER TABLE • ALTER TABLE statement specifies how to add, modify, drop or delete columns in a table. It is also used to rename a table. ALTER TABLE table_name ADD column_name column-definition; • Consider that already existing table customers. Now, add a new column customer_age into the table customers. ALTER TABLE customers ADD customer_age varchar2(50);
  • 8. 1)Add multiple columns in the existing table ALTER TABLE table_name ADD (column_1 column-definition, column_2 column-definition, column_n column_definition); Example ALTER TABLE customers ADD (customer_type varchar2(50), customer_address varchar2(50)); 2)Modify column of a table ALTER TABLE table_name MODIFY column_name column_type; Example: ALTER TABLE customers MODIFY customer_name varchar2(100) not null;
  • 9. 3)Modify multiple columns of a table ALTER TABLE table_name MODIFY (column_1 column_type, column_2 column_type, column_n column_type); ALTER TABLE customers MODIFY (customer_name varchar2(100) not null, city varchar2(100)); 4)Drop column of a table ALTER TABLE table_name DROP COLUMN column_name; ALTER TABLE customers DROP COLUMN customer_name;
  • 10. 5)rename column of a table ALTER TABLE table_name RENAME COLUMN old_name to new_name; ALTER TABLE customers RENAME COLUMN customer_name to cname; 6)rename table ALTER TABLE table_name RENAME TO new_table_name; ALTER TABLE customers RENAME TO retailers;
  • 11. DROP TABLE Statement DROP TABLE statement is used to remove or delete a table from the Oracle database. DROP [schema_name].TABLE table_name [ CASCADE CONSTRAINTS ] [ PURGE ]; Parameters schema_name: It specifies the name of the schema that owns the table. table_name: It specifies the name of the table which you want to remove from the Oracle database. CASCADE CONSTRAINTS: It is optional. If specified, it will drop all referential integrity constraints as well. PURGE: It is also optional. If specified, the table and its dependent objects are placed in the recycle bin and can’t be recovered. DROP TABLE customers; DROP TABLE customers PURGE This statement will drop the table called customers and issue a PURGE so that the space associated with the customers table is released and the customers table is not placed in recycle bin. So, it is not possible to recover that table if required.
  • 12. SELECT Statement • The Oracle SELECT statement is used to retrieve data from one or more than one tables, object tables, views, object views etc. SELECT expressions FROM tables WHERE conditions; 1)select all fields SELECT * FROM customers; 2)select specific fields SELECT age, address, salary FROM customers WHERE age < 25 AND salary > '20000' ORDER BY age ASC, salary DESC; 3)select fields from multiple tables (JOIN) SELECT customers.name, courses.trainer FROM courses INNER JOIN customers ON courses.course_id = course_id ORDER BY name;
  • 13. Insert Statement 1) Inserting a single record using the Values keyword): INSERT INTO table (column1, column2, ... column_n ) VALUES (expression1, expression2, ... expression_n ); INSERT INTO suppliers (supplier_id, supplier_name) VALUES (50, 'Flipkart'); 2) Inserting multiple records using a SELECT statement): INSERT INTO table (column1, column2, ... column_n ) SELECT expression1, expression2, ... expression_n FROM source_table WHERE conditions; INSERT INTO suppliers (supplier_id, supplier_name) SELECT age, address FROM customers WHERE age > 20; You can even check the number of rows that you want to insert by following statement: SELECT count(*) FROM customers WHERE age > 20; INSERT statement is used to add a single record or multiple records into the table.
  • 14. INSERT ALL statement • INSERT ALL statement is used to insert the rows into one table or multiple tables by using only one SQL command. INSERT ALL INTO table_name (column1, column2, column_n) VALUES (expr1, expr2, expr_n) INTO table_name(column1, column2, column_n) VALUES (expr1, expr2, expr_n) INTO table_name (column1, column2, column_n) VALUES (expr1, expr2, expr_n) SELECT * FROM dual; INSERT ALL INTO suppliers (supplier_id, supplier_name) VALUES (20, 'Google') INTO suppliers (supplier_id, supplier_name) VALUES (21, 'Microsoft') INTO suppliers (supplier_id, supplier_name) VALUES (22, 'Apple') SELECT * FROM dual;
  • 15. UPDATE Statement 1) Update column UPDATE table_name SET column1 = expression1, column2 = expression2, ... column_n = expression_n WHERE conditions; UPDATE suppliers SET supplier_name = 'Kingfisher', supplier_name = 'Bata shoes' WHERE supplier_id = 2; 2) Update Table by selecting rocords from another table UPDATE table_name SET column1 = (SELECT expression1 FROM table2 WHERE conditions) WHERE conditions; UPDATE customers SET name = (SELECT supplier_name FROM suppliers WHERE suppliers.supplier_name = customers.name) WHERE age < 25;
  • 16. DELETE Statement In Oracle, DELETE statement is used to remove or delete a single record or multiple records from a table. DELETE FROM table_name WHERE conditions; 1) Delete on one condition DELETE FROM customers WHERE name = 'Sohan'; 2) Delete On multiple conditions DELETE FROM customers WHERE last_name = 'Maurya' AND customer_id > 2;
  • 17. TRUNCATE TABLE TRUNCATE TABLE statement is used to remove all records from a table. It works same as DELETE statement but without specifying a WHERE clause. It is generally used when you don’t have to worry about rolling back TRUNCATE TABLE [schema_name.]table_name 1) schema_name: This parameter specifies the name of the schema that the table belongs to. It is optional. 2) table_name: It specifies the table that you want to truncate. TRUNCATE TABLE customers;
  • 18. DELETE Table Delete table is used to delete the table DELETE TABLE customers; TRUNCATE TABLE vs DELETE TABLE Both the statements will remove the data from the "customers" table but the main difference is that you can roll back the DELETE statement whereas you can't roll back the TRUNCATE TABLE statement.
  • 19. DISTINCT Clause Oracle DISTINCT clause is used to remove the duplicate records from the result set. It is only used with SELECT statement. SELECT DISTINCT expressions FROM tables WHERE conditions; SELECT DISTINCT name, age, salary FROM customers WHERE age >= '60';
  • 20. FROM Clause • FROM clause is a mandatory clause in SELECT expression. It specifies the tables from which data is to be retrieved. Syntax: FROM table_name... Expressions... SELECT * FROM customers WHERE salary >= 20000 ORDER BY salary ASC;
  • 21. FROM Clause Example: (with two tables) Inner Join example: Let's take two tables "suppliers" and "order1". Suppliers: Order1:
  • 22. Execute the following query: SELECT suppliers.supplier_id, suppliers.supplier_name, order1.order_number FROM suppliers INNER JOIN order1 ON suppliers.supplier_id = order1.supplier_id;
  • 23. ORDER BY Clause ORDER BY Clause is used to sort or re-arrange the records in the result set. The ORDER BY clause is only used with SELECT statement. SELECT expressions FROM tables WHERE conditions ORDER BY expression [ ASC | DESC ]; ASC: optional parameter, used to sort records in ascending order. DESC: optional parameter, used to sort records in descending order. SELECT * FROM supplier ORDER BY last_name; SELECT * FROM supplier ORDER BY last_name DESC;
  • 24. GROUP BY Clause GROUP BY clause is used with SELECT statement to collect data from multiple records and group the results by one or more columns. SELECT expression1, expression2, ... expression_n, aggregate_function (aggregate_expression) FROM tables WHERE conditions GROUP BY expression1, expression2, ... expression_n; SELECT item, SUM(sale) AS "Total sales" FROM salesdepartment GROUP BY item;
  • 25. HAVING Clause • HAVING Clause is used with GROUP BY Clause to restrict the groups of returned rows where condition is TRUE. SELECT expression1, expression2, ... expression_n, aggregate_function (aggregate_expression) FROM tables WHERE conditions GROUP BY expression1, expression2, ... expression_n HAVING having_condition; • aggregate_function: It specifies the aggregate functions i.e. SUM, COUNT, MIN, MAX or AVG functions. • aggregate_expression: It specifies the column or expression on that the aggregate function is based on. • having_conditions: It specifies the conditions that are applied only to the aggregated results to restrict the groups of returned rows.
  • 26. 1) With GROUP BY SUM function 2) With GROUP BY COUNT function SELECT item, SUM(sale) AS "Total sales" FROM salesdepartment GROUP BY item HAVING SUM(sale) < 1000; SELECT state, COUNT(*) AS "Number of customers"FROM customers WHERE salary > 10000 GROUP BY state HAVING COUNT(*) >= 2;
  • 27. 3) with GROUP BY MIN function 4) with GROUP BY MAX function SELECT department, MIN(salary) AS "Lowest salary" FROM employees GROUP BY department HAVING MIN(salary) < 15000; SELECT department, MAX(salary) AS "Highest salary" FROM employees GROUP BY department HAVING MAX(salary) > 30000;