Skip to main content

Welcome to DBA Master – Database Tips, Tricks, and Tutorials

Welcome to DBA Master ! This blog is dedicated to all things related to database administration , SQL optimization , and performance tuning . Whether you're a beginner or a seasoned DBA, you'll find practical guides, troubleshooting tips, and real-world tutorials to help you work smarter with data. What to Expect: SQL performance tuning tips Indexing strategies Backup and recovery best practices High availability and replication techniques Database creation, configuration, and setup Monitoring queries and scripts for proactive performance management Migration guides across different database platforms Security essentials and best practices Recommended tools for DBAs Real-world error fixes and how to solve them Stay tuned — exciting content is coming soon. Feel free to bookmark and share: www.dbamaster.com ! Thanks for visiting!

SQL Loader and External Table

SQL Loader:

  • Oracle SQL Loader is a utility provided by Oracle Corporation for loading data from external files into Oracle Database tables.
  • It is a command-line tool that uses control files to define the format of the data file and the target database table.
  • It is a powerful and efficient tool designed to handle large volumes of data loading in a fast.
  • SQL Loader is particularly useful when you need to import data from flat files, such as CSV (Comma-Separated Values) or other delimited text files, into Oracle Database tables.

 

Log File: The log file provides a detailed record of the SQL Loader session, offering information on the progress, statistics, and any errors encountered during the loading process.

Bad File: The bad file is used to store records that SQL Loader determines cannot be loaded into the target table due to formatting errors, data type mismatches, or other issues. These records are considered "bad" and are not loaded into the database.
Discard File: Similar to the bad file, the discard file stores records that were not loaded into the database. However, records in the discard file are typically rejected due to conditions specified in the control file, such as SKIP or WHEN clauses.
Modes:
• Insert(Load Empty Table)
• Append (Load Existing Table)
• Replace (Delete records and then load, user must have DELETE permission)

Data File: filename: employee.txt
John Doe, 28, New York, Software Engineer, 80000
Alice Smith, 35, Los Angeles, Data Scientist, 90000
Bob Johnson, 42, Chicago, Project Manager, 75000
Emily Davis, 30, San Francisco, UX Designer, 85000
Michael Williams, 25, Houston, Sales Representative, 70000
Olivia Brown, 31, Seattle, Marketing Specialist, 82000
Daniel Lee, 38, Miami, Financial Analyst, 95000
Sophia Miller, 27, Boston, Graphic Designer, 78000
Matthew Anderson, 33, Dallas, HR Manager, 88000
Emma Wilson, 29, Atlanta, Operations Coordinator, 76000

Control File: emp.ctl
load data
infile '/home/oracle/employee.txt'
insert into table employee
fields terminated by "," optionally enclosed by " "
(name, age, city, occupation, salary)

load data
infile '/home/oracle/employee.txt'
append into table employee
fields terminated by ","
(name, age, city, occupation, salary)

load data
infile '/home/oracle/employee.txt'
infile '/home/oracle/employee_new.txt'
append into table employee
fields terminated by ","
(name, age, city, occupation, salary)

LOAD DATA
INFILE '/home/oracle/employee.txt'
REPLACE INTO TABLE Employee
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
TRAILING NULLCOLS
(name, age, city, occupation, salary)

Create a table:

CREATE TABLE Employee (
name VARCHAR2(100),
age NUMBER,
city VARCHAR2(50),
occupation VARCHAR2(100),
salary NUMBER
);

Run SQl Loader Command:
sqlldr scott/tiger control=emp.ctl

Use Parameter File: pmfile.par
control=email.ctl
log=email.log
bad=email.bad
data=email.dat
direct=true

Run SQl Loader Command:
sqlldr user_name/password parfile=pmfile.par

External Table:

External table is a database object that allows you to access and query data stored outside the Oracle database, such as in flat files or other external sources. External tables are read-only, and it does not describe any data that is stored in the database.

create a directory in the file system and put your CSV file inside:
mkdir -p /db/testdir
CSV File: emp.csv
John Doe, 28, New York, Software Engineer, 80000
Alice Smith, 35, Los Angeles, Data Scientist, 90000
Bob Johnson, 42, Chicago, Project Manager, 75000
Emily Davis, 30, San Francisco, UX Designer, 85000
Michael Williams, 25, Houston, Sales Representative, 70000
Olivia Brown, 31, Seattle, Marketing Specialist, 82000
Daniel Lee, 38, Miami, Financial Analyst, 95000
Sophia Miller, 27, Boston, Graphic Designer, 78000
Matthew Anderson, 33, Dallas, HR Manager, 88000
Emma Wilson, 29, Atlanta, Operations Coordinator, 76000

Create Directory (extdir) and give permission:

CREATE OR REPLACE DIRECTORY extdir AS '/db/testdir';

GRANT READ,WRITE ON DIRECTORY extdir TO user_name;

check external directory List:

select * from dba_directories where directory_name='extdir';

external table command: method 1

CREATE TABLE ext_table (
name VARCHAR2(100),
age NUMBER,
city VARCHAR2(50),
occupation VARCHAR2(100),
salary NUMBER
)
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY extdir
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
)
LOCATION (‘emp.csv’)
)
REJECT LIMIT UNLIMITED;

external table command: method 2:

CREATE TABLE ext_table (
name VARCHAR2(100),
age NUMBER,
city VARCHAR2(50),
occupation VARCHAR2(100),
salary NUMBER
)
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY extdir
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY ','
(name, age, city, occupation, salary)
)
LOCATION ('emp.csv')
)
REJECT LIMIT UNLIMITED;


Comments

Popular posts from this blog

Oracle Database 19C Performance Tunning - PART 1

Advantages: 1. Improved Query Performance •    Optimized SQL execution plans lead to faster query response times. •    Reduces unnecessary full table scans and improves indexing strategies. •    Parallel execution tuning speeds up large data processing tasks. 2. Better Resource Utilization •    Efficient use of CPU, memory, disk I/O, and network resources. •    Reduces contention on Redo Logs, Undo Tablespaces, and Buffer Cache. •    Helps in load balancing across multiple instances in RAC (Real Application Clusters). 3. Increased System Scalability •    Ensures that the database can handle a growing number of users and transactions. •    Proper tuning allows scaling without degrading performance. •    Optimized parallel processing ensures better performance on multi-core servers. 4. Lower Infrastructure Costs •    Reduces the need for add...

Oracle RMAN Backup And Restore

RMAN: (Oracle 8) RMAN (Recovery Manager) is a utility provided by Oracle Database to perform backup, restore, and recovery operations. It is a command line tool. Features of RMAN in Oracle 19c Comprehensive Backup Capabilities: Full and incremental backups. Block-level backups for efficient data storage. Archived redo log backups. Fast Recovery Area (FRA) integration for centralized backup storage. Efficient Recovery Options: Point-in-time recovery (PITR). Complete and incomplete recovery. Flashback database capabilities for quick undo of changes. Multitenant Database Support: RMAN fully supports container databases (CDBs) and pluggable databases (PDBs). Provides flexibility to back up and recover individual PDBs or entire CDBs. Automatic Space Management: Manages disk space in the FRA. Automatically deletes obsolete backups and archived logs. Data Deduplication and Compression: Backup optimization through block-level deduplication. Built-in compression algorithms to reduce storage req...

Oracle 19c Database Software Installation in OEL8

 Pre-requisites for OS level:            Set the static IP Address     Disable the Firewall (systemctl stop firewalld & systemctl disable firewalld)     set SELINUX=permissive on /etc/selinux/config  ##Need to restart the server use init 6 Oracle Installation Pre-requisites Methods     Automatic Setup     Manual Setup      Automatic requisites Setup: (avoid step 1 to step 5): dnf install -y oracle-database-preinstall-19c Install the dependencies: curl -o oracle-database-preinstall-19c-1.0-2.el8.x86_64.rpm https://yum.oracle.com/repo/OracleLinux/OL8/appstream/x86_64/getPackage/oracle-database-preinstall-19c-1.0-2.el8.x86_64.rpm dnf -y localinstall oracle-database-preinstall-19c-1.0-2.el8.x86_64.rpm Manual Setup: step 1: Add the karenl parameters and values vi /etc/sysctl.conf     fs.file-max = 6815744 kernel.sem = 250 32000 100 128 kernel....