Skip to content

Instantly share code, notes, and snippets.

@gladiopeace
Created February 4, 2018 21:30
Show Gist options
  • Save gladiopeace/d40a72a45962d23746bd860919bbbe96 to your computer and use it in GitHub Desktop.
Save gladiopeace/d40a72a45962d23746bd860919bbbe96 to your computer and use it in GitHub Desktop.
Sqlplus Quick Reference #database #oracle

SqlPlus quick reference

SqlPlus Profile: glogin.sql

glogin.sql or login.sql details:

-- pagesize repeats table headers after N rows
set pagesize 500

-- Increase output length
set linesize 150

-- Show output from stored procedures
set serveroutput ON

-- Do not wrap rows
set wrap OFF

SqlPlus Connect

sqlplus user/pass@host/SID_or_ServiceName
sqlplus user/pass@SID (localhost is default)

Local admin access, user must be part of ORA_DBA group on Windows; dba on Unix

sqlplus / as sysdba 

List objects

User tables

select * from tab;
select * from user_tables;

User functions

SELECT distinct name FROM User_Source WHERE TYPE = 'FUNCTION';

User procedures

SELECT * FROM User_Procedures;
SELECT DISTINCT name FROM User_Source WHERE TYPE = 'PROCEDURE';

Table structure

Describe <table_name>

SELECT COLUMN_NAME, DATA_TYPE FROM ALL_TAB_COLUMNS WHERE
TABLE_NAME='table_name';

Truncate all tables

BEGIN

FOR c IN (SELECT table_name, constraint_name FROM user_constraints
WHERE constraint_type = 'R')

LOOP

EXECUTE IMMEDIATE ('alter table ' || c.table_name || ' disable
constraint ' || c.constraint_name);

END LOOP;

FOR c IN (SELECT table_name FROM user_tables)

LOOP

EXECUTE IMMEDIATE ('truncate table ' || c.table_name);

END LOOP;

FOR c IN (SELECT table_name, constraint_name FROM user_constraints
WHERE constraint_type = 'R')

LOOP

EXECUTE IMMEDIATE ('alter table ' || c.table_name || ' enable
constraint ' || c.constraint_name);

END LOOP;

END;

/

SqlPlus pause on each page

set pause on

Sqlplus .sql Import

  • UTF8 sql files must be without BOM
  • Stored procedures and blocks must end with /
  • SET ECHO ON to display executed sql for logs
  • Script parameters
  • This way it will exit asap after script finishes: "@<path_to_sql_file> arg1 arg2" | sqlplus <conn_string>

Language Settings, Encoding

$ENV:NLS_LANG='.UTF8'

NLS lang FAQ: http://goo.gl/58oknU

Provera:

select Value from nls_database_parameters WHERE parameter = 'NLS_CHARACTERSET';

Length semantics

SHUTDOWN IMMEDIATE;
STARTUP MOUNT;
ALTER SYSTEM ENABLE RESTRICTED SESSION;
ALTER SYSTEM SET JOB_QUEUE_PROCESSES=0;
ALTER DATABASE OPEN; 
alter system set nls_length_semantics='char' scope=both;
SHUTDOWN IMMEDIATE;
STARTUP;

Provera:

select value from v$nls_parameters where parameter = 'NLS_LENGTH_SEMANTICS'
select Value from nls_database_parameters WHERE parameter = 'NLS_LENGTH_SEMANTICS';
SQL>  show parameter NLS_LENGTH_SEMANTICS
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
nls_length_semantics                 string      BYTE
SQL> ALTER SYSTEM SET NLS_LENGTH_SEMANTICS=CHAR scope=BOTH;
System altered.
 
SQL> show parameter NLS_LENGTH_SEMANTICS
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
nls_length_semantics                 string      CHAR

By default, the character data types CHAR and VARCHAR2 are specified in bytes, not characters. Hence, the specification CHAR(20) in a table definition allows 20 bytes for storing character data. This works well if the database character set uses a single-byte character encoding scheme because the number of characters is the same as the number of bytes. (Naša baza je multi-byte)

Note that if the NLS_LENGTH_SEMANTICS environment variable is not set on the client, then the client session defaults to the value for NLS_LENGTH_SEMANTICS on the database server. This enables all client sessions on the network to have the same NLS_LENGTH_SEMANTICS behavior. Setting the environment variable on an individual client enables the server initialization parameter to be overridden for that client.

The session-level value of NLS_LENGTH_SEMANTICS specifies the default length semantics to use for VARCHAR2 and CHAR table columns, user-defined object attributes, and PL/SQL variables in database objects created in the session. This default may be overridden by the explicit length semantics qualifiers BYTE and CHAR in column, attribute, and variable definitions.

The instance-level value of NLS_LENGTH_SEMANTICS provides a default for the session-level value if NLS_LENGTH_SEMANTICS it is not set explicitly by the database client through the NLS_LENGTH_SEMANTICS client environment variable (does not apply to JDBC Thin clients), or the ALTER SESSION SET NLS_LENGTH_SEMANTICS statement.

Why do you want to change it? NLS_LENGTH_SEMANTICS is only valid if you define a column without specifying the semantics (e.g. create table xxx (values varchar2(50)). It doesn't have an impact on any pre created tables, so it doesn't help to change from BYTE to CHAR! On the other hand: you can set it session specific (ALTER SESSION) or database wide (ALTER SYSTEM SET NLS_LENGTH_SEMANTICS=CHAR).

BUT AGAIN (due to the Oracle documentation): "Caution: Oracle strongly recommends that you do NOT set the NLS_LENGTH_SEMANTICS parameter to CHAR in the instance or server parameter file."

Slash vs Semicolon

The ; ends a SQL statement, whereas the / executes whatever is in the current "buffer". So when you use a ; and a / the statement is actually executed twice.

The / is mainly required in order to run statements that have embedded ; like a CREATE PROCEDURE statement

From my understanding, all the SQL statement don't need forward slash as they will run automatically at the end of semicolons, including DDL, DML, DCL and TCL statements.

For other PL/SQL blocks, including Procedures, Functions, Packages and Triggers, because they are multiple line programs, Oracle need a way to know when to run the block, so we have to write a forward slash at the end of each block to let Oracle run it.

http://goo.gl/HpchE0

Database operations

Listener

lsnrctl start | status | stop

Pluggable

-- Activate
alter session set container=pdbtest1;

-- Open
alter pluggable database PDBTEST1 open read write;
show con_name;
show con_id;
show pdbs;
select * from v$pdbs;
alter session set container=PDB1;

Information

-- Users
select * from all_users;
select * from dba_users;
alter user <name> identified by <pass>

-- Password expiration
select * from dba_profiles;
ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;

-- Enterprise Manager port
 select dbms_xdb_config.gethttpsport() from dual;

User



### Recover

RECOVER DATABASE UNTIL CANCEL;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment