Advance SQL with Security Management
- Get link
- X
- Other Apps
1. Index in Oracle
An Index is a database object that improves the speed of data retrieval. It acts like a pointer to data in a table, allowing queries to be executed more efficiently.
- Purpose: Enhances query performance by reducing the amount of data scanned.
- Types of Indexes:
- B-Tree Index: Default index type, best for high-cardinality columns.
- Bitmap Index: Best for low-cardinality columns (e.g., gender).
- Unique Index: Ensures all values in the column(s) are unique.
Example:
Creating an index on the EMPLOYEE
table for the LAST_NAME
column:
CREATE INDEX idx_lastname ON EMPLOYEE(LAST_NAME);
2. View in Oracle
A View is a virtual table based on a query that pulls data from one or more tables. It does not store data but provides a way to simplify complex queries.
- Purpose: Provides data security, abstraction, and simplifies queries.
- Types of Views:
- Simple View: Based on a single table without group functions.
- Complex View: Based on multiple tables with group functions or joins.
Example:
Creating a view to display employee details:
CREATE VIEW emp_view AS
SELECT EMPLOYEE_ID, FIRST_NAME, LAST_NAME FROM EMPLOYEE;
3. Sequence in Oracle
A Sequence is a database object used to generate unique numerical values automatically, often used for primary key generation.
- Purpose: Ensures unique values without manual incrementing.
- Features: Can be configured with increments, start values, and cycles.
Example:
Creating a sequence starting at 1000, incrementing by 1:
CREATE SEQUENCE emp_seq
START WITH 1000
INCREMENT BY 1;
Using the sequence:
INSERT INTO EMPLOYEE(EMPLOYEE_ID, NAME) VALUES (emp_seq.NEXTVAL, 'John Doe');
4. Cluster in Oracle
A Cluster is a storage mechanism where tables that are frequently joined are physically stored together, optimizing retrieval.
- Purpose: Improves performance for join operations on clustered tables.
Example:
Creating a cluster:
CREATE CLUSTER emp_dept_cluster (DEPT_ID NUMBER(5));
Creating tables within the cluster:
CREATE TABLE EMPLOYEE (EMP_ID NUMBER, NAME VARCHAR2(50), DEPT_ID NUMBER)
CLUSTER emp_dept_cluster (DEPT_ID);
5. Granting and Revoking Permissions in Oracle
Granting Permissions allows users to perform specified actions on database objects. Revoking Permissions removes previously granted permissions.
- Common Privileges:
SELECT
,INSERT
,UPDATE
,DELETE
.
Granting Example:
Granting SELECT
on the EMPLOYEE
table:
GRANT SELECT ON EMPLOYEE TO user1;
Revoking Example:
Revoking the SELECT
privilege:
REVOKE SELECT ON EMPLOYEE FROM user1;
6. Dump Function in Oracle
The DUMP function returns a string that represents the internal data storage of a value.
- Purpose: Useful for debugging data type issues or understanding how Oracle stores data.
Example:
SELECT DUMP('Oracle') FROM DUAL;
Output example: Typ=96 Len=6: 79,114,97,99,108,101
7. Creating a User in Oracle & Changing Password
Creating a New User:
A new user can be created using the CREATE USER
command.
CREATE USER new_user IDENTIFIED BY password123;
Granting Roles to the User:
GRANT CONNECT, RESOURCE TO new_user;
Changing the Password:
ALTER USER new_user IDENTIFIED BY new_password456;
8. Setting Environment Using SET Command
The SET
command in Oracle SQL*Plus is used to configure various environment settings, like line size, page size, or feedback.
Examples:
- Setting Line Size:
SET LINESIZE 100;
- Disabling Command Echo:
SET ECHO OFF;
9. Spooling in Oracle
Spooling allows the output of SQL commands to be written to a file.
- Purpose: Useful for saving query results for later review or documentation.
Example:
Starting the spool:
SPOOL output.txt;
SELECT * FROM EMPLOYEE;
SPOOL OFF;
This creates a file output.txt
containing the query result.
Summary of Advantages
- Index: Enhances query performance and speeds up searches.
- View: Provides data abstraction and security, simplifies queries.
- Sequence: Ensures unique key generation automatically.
- Cluster: Optimizes join operations for related tables.
- Grant/Revoke: Controls user access to database objects securely.
- Dump Function: Helps in debugging data storage issues.
- User Management: Simplifies database administration.
- SET Command: Customizes SQL*Plus session behavior.
- Spooling: Facilitates saving query results for external use.
- Get link
- X
- Other Apps
Comments
Post a Comment