From b93b658ee4d45f8bc5e164753e758982ddcced49 Mon Sep 17 00:00:00 2001 From: Divya Date: Mon, 20 Nov 2023 20:27:09 +0530 Subject: [PATCH 1/3] Adding Oracle db cheatsheet --- databases/oracledb.sh | 172 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 databases/oracledb.sh diff --git a/databases/oracledb.sh b/databases/oracledb.sh new file mode 100644 index 00000000..1551a242 --- /dev/null +++ b/databases/oracledb.sh @@ -0,0 +1,172 @@ +***************************************************************************** +BASICS +***************************************************************************** + +# Connect to Oracle Database +Establish a connection to the Oracle Database using SQL*Plus. Replace username, password, hostname, port, and service_name with the appropriate values. + +sqlplus username/password@hostname:port/service_name + + +# Show All Schemas +Retrieve a list of all schemas in the Oracle Database. + +SELECT username FROM all_users; + +# Show Current Schema + Display the current schema (user) in use. + +SHOW USER; + +# Create Schema (User) + Create a new schema (user) in the Oracle Database. + +CREATE USER username IDENTIFIED BY password; + +# Switch Schema + Change the current schema for the session. + +ALTER SESSION SET CURRENT_SCHEMA = username; + +# Drop Schema (User) + Remove a schema (user) from the Oracle Database, including all owned objects. + +DROP USER username CASCADE; + +# Show Tables in Current Schema + Retrieve a list of tables in the current schema. + +SELECT table_name FROM user_tables; + +***************************************************************************** +CRUD +***************************************************************************** + +# Create Table + Define and create a new table in the Oracle Database. + +CREATE TABLE table_name ( + column1 datatype, + column2 datatype, + ... +); + +# Insert Row + Add a new row with specified values into a table. + +INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...); + +# Select All Rows + Retrieve all rows from a table. + +SELECT * FROM table_name; + +# Select Specific Columns + Retrieve specific columns from a table. + +SELECT column1, column2 FROM table_name; + +# Update Row + Modify data in a table by updating a specific row. + +UPDATE table_name SET column1 = value1 WHERE condition; + +# Delete Row + Remove specific rows from a table based on a condition. + +DELETE FROM table_name WHERE condition; + +***************************************************************************** +OTHER FUNCTIONS +***************************************************************************** + +# Sort Rows + Retrieve rows from a table sorted in ascending or descending order based on a specified column. + +SELECT * FROM table_name ORDER BY column1 [ASC|DESC]; + +# Count Rows + Calculate the total number of rows in a table. + +SELECT COUNT(*) FROM table_name; + +# Limit Rows + Restrict the number of rows retrieved from a table. + +SELECT * FROM table_name WHERE ROWNUM <= n; + +# Join Tables + Combine rows from two or more tables based on a related column. + +SELECT * FROM table1 JOIN table2 ON table1.column = table2.column; + +# Group By + Group rows based on the values in a specified column and perform an aggregate function. + +SELECT column, COUNT(*) FROM table_name GROUP BY column; + +# Aggregate Functions + Perform aggregate calculations on a column, such as average, sum, minimum, and maximum. + +SELECT AVG(column), SUM(column), MIN(column), MAX(column) FROM table_name; + +# Subquery + Use the result of a subquery to filter data in the main query. + +SELECT column1 FROM table_name WHERE column2 = (SELECT column2 FROM another_table WHERE condition); + +# Index + Create an index on a column to improve query performance. + +CREATE INDEX index_name ON table_name(column1); + +# Drop Index + Remove an existing index from a table. + +DROP INDEX index_name; + +# Constraints + Enforce data integrity by adding a primary key constraint to a table. + +ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (column1); + +# Sequence + Create a sequence that generates unique values for a column. + +CREATE SEQUENCE sequence_name START WITH 1 INCREMENT BY 1; + +# View + Create a virtual table based on the result of a SELECT query. + +CREATE VIEW view_name AS SELECT column1, column2 FROM table_name WHERE condition; + +# Stored Procedure + Define and store a reusable set of SQL statements in the database. + +CREATE OR REPLACE PROCEDURE procedure_name (parameter1 datatype, parameter2 datatype, ...) AS +BEGIN + -- procedure body +END; +/ + +# Trigger + Associate a set of actions with a specific table event, such as before inserting a new row. + +CREATE OR REPLACE TRIGGER trigger_name +BEFORE INSERT ON table_name +FOR EACH ROW +BEGIN + -- trigger body +END; +/ + +# Partitioning + Divide a large table into smaller, more manageable pieces for improved performance and maintenance. + +CREATE TABLE table_name +PARTITION BY RANGE (column_name) +( + PARTITION part1 VALUES LESS THAN (value1), + PARTITION part2 VALUES LESS THAN (value2), + ... +); From 0f574aa95833bd61fc7b22cd895674310aacf4f1 Mon Sep 17 00:00:00 2001 From: Divya Date: Mon, 20 Nov 2023 20:30:26 +0530 Subject: [PATCH 2/3] Updating oracle db --- databases/oracledb.sh | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/databases/oracledb.sh b/databases/oracledb.sh index 1551a242..61dc8ab3 100644 --- a/databases/oracledb.sh +++ b/databases/oracledb.sh @@ -14,27 +14,27 @@ Retrieve a list of all schemas in the Oracle Database. SELECT username FROM all_users; # Show Current Schema - Display the current schema (user) in use. +Display the current schema (user) in use. SHOW USER; # Create Schema (User) - Create a new schema (user) in the Oracle Database. +Create a new schema (user) in the Oracle Database. CREATE USER username IDENTIFIED BY password; # Switch Schema - Change the current schema for the session. +Change the current schema for the session. ALTER SESSION SET CURRENT_SCHEMA = username; # Drop Schema (User) - Remove a schema (user) from the Oracle Database, including all owned objects. +Remove a schema (user) from the Oracle Database, including all owned objects. DROP USER username CASCADE; # Show Tables in Current Schema - Retrieve a list of tables in the current schema. +Retrieve a list of tables in the current schema. SELECT table_name FROM user_tables; @@ -43,7 +43,7 @@ CRUD ***************************************************************************** # Create Table - Define and create a new table in the Oracle Database. +Define and create a new table in the Oracle Database. CREATE TABLE table_name ( column1 datatype, @@ -52,27 +52,27 @@ CREATE TABLE table_name ( ); # Insert Row - Add a new row with specified values into a table. +Add a new row with specified values into a table. INSERT INTO table_name (column1, column2, ...) VALUES (value1, value2, ...); # Select All Rows - Retrieve all rows from a table. +Retrieve all rows from a table. SELECT * FROM table_name; # Select Specific Columns - Retrieve specific columns from a table. +Retrieve specific columns from a table. SELECT column1, column2 FROM table_name; # Update Row - Modify data in a table by updating a specific row. +Modify data in a table by updating a specific row. UPDATE table_name SET column1 = value1 WHERE condition; # Delete Row - Remove specific rows from a table based on a condition. +Remove specific rows from a table based on a condition. DELETE FROM table_name WHERE condition; From dd2d37026e0350319c3bc2c40ebc864d6dbe662d Mon Sep 17 00:00:00 2001 From: Divya Date: Mon, 20 Nov 2023 20:37:36 +0530 Subject: [PATCH 3/3] Adding Oracle db cheatsheet --- databases/oracledb.sh | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/databases/oracledb.sh b/databases/oracledb.sh index 61dc8ab3..be4de06e 100644 --- a/databases/oracledb.sh +++ b/databases/oracledb.sh @@ -81,67 +81,67 @@ OTHER FUNCTIONS ***************************************************************************** # Sort Rows - Retrieve rows from a table sorted in ascending or descending order based on a specified column. +Retrieve rows from a table sorted in ascending or descending order based on a specified column. SELECT * FROM table_name ORDER BY column1 [ASC|DESC]; # Count Rows - Calculate the total number of rows in a table. +Calculate the total number of rows in a table. SELECT COUNT(*) FROM table_name; # Limit Rows - Restrict the number of rows retrieved from a table. +Restrict the number of rows retrieved from a table. SELECT * FROM table_name WHERE ROWNUM <= n; # Join Tables - Combine rows from two or more tables based on a related column. +Combine rows from two or more tables based on a related column. SELECT * FROM table1 JOIN table2 ON table1.column = table2.column; # Group By - Group rows based on the values in a specified column and perform an aggregate function. +Group rows based on the values in a specified column and perform an aggregate function. SELECT column, COUNT(*) FROM table_name GROUP BY column; # Aggregate Functions - Perform aggregate calculations on a column, such as average, sum, minimum, and maximum. +Perform aggregate calculations on a column, such as average, sum, minimum, and maximum. SELECT AVG(column), SUM(column), MIN(column), MAX(column) FROM table_name; # Subquery - Use the result of a subquery to filter data in the main query. +Use the result of a subquery to filter data in the main query. SELECT column1 FROM table_name WHERE column2 = (SELECT column2 FROM another_table WHERE condition); # Index - Create an index on a column to improve query performance. +Create an index on a column to improve query performance. CREATE INDEX index_name ON table_name(column1); # Drop Index - Remove an existing index from a table. +Remove an existing index from a table. DROP INDEX index_name; # Constraints - Enforce data integrity by adding a primary key constraint to a table. +Enforce data integrity by adding a primary key constraint to a table. ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (column1); # Sequence - Create a sequence that generates unique values for a column. +Create a sequence that generates unique values for a column. CREATE SEQUENCE sequence_name START WITH 1 INCREMENT BY 1; # View - Create a virtual table based on the result of a SELECT query. +Create a virtual table based on the result of a SELECT query. CREATE VIEW view_name AS SELECT column1, column2 FROM table_name WHERE condition; # Stored Procedure - Define and store a reusable set of SQL statements in the database. +Define and store a reusable set of SQL statements in the database. CREATE OR REPLACE PROCEDURE procedure_name (parameter1 datatype, parameter2 datatype, ...) AS BEGIN @@ -150,7 +150,7 @@ END; / # Trigger - Associate a set of actions with a specific table event, such as before inserting a new row. +Associate a set of actions with a specific table event, such as before inserting a new row. CREATE OR REPLACE TRIGGER trigger_name BEFORE INSERT ON table_name @@ -161,7 +161,7 @@ END; / # Partitioning - Divide a large table into smaller, more manageable pieces for improved performance and maintenance. +Divide a large table into smaller, more manageable pieces for improved performance and maintenance. CREATE TABLE table_name PARTITION BY RANGE (column_name)