Adding Oracle db cheatsheet

This commit is contained in:
Divya
2023-11-20 20:37:36 +05:30
parent 0f574aa958
commit dd2d37026e

View File

@@ -81,67 +81,67 @@ OTHER FUNCTIONS
***************************************************************************** *****************************************************************************
# Sort Rows # 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]; SELECT * FROM table_name ORDER BY column1 [ASC|DESC];
# Count Rows # 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; SELECT COUNT(*) FROM table_name;
# Limit Rows # 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; SELECT * FROM table_name WHERE ROWNUM <= n;
# Join Tables # 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; SELECT * FROM table1 JOIN table2 ON table1.column = table2.column;
# Group By # 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; SELECT column, COUNT(*) FROM table_name GROUP BY column;
# Aggregate Functions # 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; SELECT AVG(column), SUM(column), MIN(column), MAX(column) FROM table_name;
# Subquery # 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); SELECT column1 FROM table_name WHERE column2 = (SELECT column2 FROM another_table WHERE condition);
# Index # 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); CREATE INDEX index_name ON table_name(column1);
# Drop Index # Drop Index
Remove an existing index from a table. Remove an existing index from a table.
DROP INDEX index_name; DROP INDEX index_name;
# Constraints # 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); ALTER TABLE table_name ADD CONSTRAINT constraint_name PRIMARY KEY (column1);
# Sequence # 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; CREATE SEQUENCE sequence_name START WITH 1 INCREMENT BY 1;
# View # 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; CREATE VIEW view_name AS SELECT column1, column2 FROM table_name WHERE condition;
# Stored Procedure # 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 CREATE OR REPLACE PROCEDURE procedure_name (parameter1 datatype, parameter2 datatype, ...) AS
BEGIN BEGIN
@@ -150,7 +150,7 @@ END;
/ /
# Trigger # 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 CREATE OR REPLACE TRIGGER trigger_name
BEFORE INSERT ON table_name BEFORE INSERT ON table_name
@@ -161,7 +161,7 @@ END;
/ /
# Partitioning # 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 CREATE TABLE table_name
PARTITION BY RANGE (column_name) PARTITION BY RANGE (column_name)