Equivalent of .schema in Oracle - oracle

I have read access on a table in a database and I wanted to get the relational schema. In Sqlite3, I know you could just do .schema . However, I was wondering what the equivalent is in OracleDB. It seems hard to find because they've repurposed the word schema.

as I understood from this SQLite link
.schema ?TABLE? Show the CREATE statements
If TABLE specified, only show tables matching
LIKE pattern TABLE.
to get DDL/create statements for Oracle objects you need use package DBMS_METADATA
Example: Fetch the DDL for all Complete Tables in the Current Schema,
Filter Out Nested Tables and Overflow Segments
--create test table
create table my_test_table(col1 varchar(50));
--get DDL
SELECT table_name, DBMS_METADATA.GET_DDL('TABLE',u.table_name)
FROM USER_ALL_TABLES u
WHERE u.nested='NO'
AND (u.iot_type is null or u.iot_type='IOT');
--query result
TABLE_NAME DBMS_METADATA.GET_DDL('TABLE',
1 MY_TEST_TABLE <CLOB>
--DDL for MY_TEST_TABLE will be
CREATE TABLE "TESTUSER"."MY_TEST_TABLE"
( "COL1" VARCHAR2(50)
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS"

Related

Can I export all my table definitions in a database in Oracle SQL Developer

So I created a database in Oracle SQL Developer and wrote up all the table definitions by hand using Notepad ++ and just did a bulk create tables statement.
Silly me forgot to save this file so now I have all the definitions on SQL developer but none locally.
I have made some major changes to the design however there are some definitions that I can still use that are on there.
Is there a way I can export all the tables in a database to a file? I know I can grab the definitions individually however the time constraint would be massive. This way I don't have to go about manually writing it all again. Any searches that I conduct looking for an answer are just giving answers on how to select all tables in a database and not how to export table definitions.
Just to note, I had a look in my temp data for Notepad ++ however the file is not there anymore.
Tools, Database Export
Uncheck 'data'
Pick your output options, you want one file for everything or one file for each object.
Then pick your schema and objects - if you pick 'nothing' it will dump out the entire schema by default.
Sure, you can - using the DBMS_METADATA package and its GET_DDL function.
For example - in which I'm doing it in SCOTT's schema for two tables only - it would be like this:
SQL> set pagesize 0
SQL> set linesize 200
SQL> set long 20000
SQL> set longchunksize 20000
SQL> set feedback off
SQL> set verify off
SQL> set trimspool on
SQL>
SQL> select dbms_metadata.get_ddl ('TABLE', table_name, 'SCOTT')
2 from user_tables
3 where table_name in ('EMP', 'DEPT');
CREATE TABLE "SCOTT"."DEPT"
( "DEPTNO" NUMBER(2,0),
"DNAME" VARCHAR2(14),
"LOC" VARCHAR2(13)
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS"
CREATE TABLE "SCOTT"."EMP"
( "EMPNO" NUMBER(4,0) NOT NULL ENABLE,
"ENAME" VARCHAR2(10),
"JOB" VARCHAR2(9),
"MGR" NUMBER(4,0),
"HIREDATE" DATE,
"SAL" NUMBER(7,2),
"COMM" NUMBER(7,2),
"DEPTNO" NUMBER(2,0)
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS"
SQL>
You've noticed some SET commands at the beginning - I ran it in SQL*Plus, but they work in SQL Developer as well.

Showing full response on query on sqlcl query

I'm trying to use sqlcl on a remote server to retrieve the table definition of the tables in given oracle database.
Since I have to use the cli there, I can't simply use SQLDeveloper and get the table definitions out of there (can't connect to DB from outside of the server).
The idea is to use
select dbms_metadata.get_ddl('TABLE', table_name)
from user_tables where table_name = 'RESULTS';
in order to get the table DDL for the table I need.
However, when I run this query in the sqlcl tool, the output looks like this:
CREATE TABLE "AP29_QUDB"."RESULTS"
( "LOCATION_RESULT_UID" RAW(16) NOT NU
That's it, no more information.
Does someone know how I get the full result of that query to be displayed?
(Or alternatively simply pipe it to a file for me to copy it out of there)
Thank you very much
- Tim
Looks like sqlcl copied the LONG setting from SQL*Plus. By default it only shows the first 80 characters of a CLOB. That size can be changed with set long [some large number]'.
SQL> select dbms_metadata.get_ddl('TABLE', 'DUAL', 'SYS') from dual;
DBMS_METADATA.GET_DDL('TABLE','DUAL','SYS')
--------------------------------------------------------------------------------
CREATE TABLE "SYS"."DUAL" SHARING=METADATA
( "DUMMY" VARCHAR2(1)
) PCT
SQL> set long 10000000
SQL> /
DBMS_METADATA.GET_DDL('TABLE','DUAL','SYS')
--------------------------------------------------------------------------------
CREATE TABLE "SYS"."DUAL" SHARING=METADATA
( "DUMMY" VARCHAR2(1)
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
STORAGE(INITIAL 16384 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "SYSTEM"
SQL>

How can I execute 'describe table_name' command from a PL/SQL procedure?

I have the following procedure in Oracle:
create procedure clone_tables
(current_table_name varchar2, cloned_table_name varchar2);
I have to clone a table, but I receive only it's name.
In this case, I think I have to get it's structure, so describe table_name command would be enough.
Now, execute immediate or dbms_sql.execute() use only SQL statements.
Is there another way I can do that?
If you need to build a clone of a table, you can use:
create or replace procedure clone_tables (current_table_name varchar2,
cloned_table_name varchar2
) as
begin
execute immediate
'create table ' || cloned_table_name ||
' as select * from ' || current_table_name
' where 1 = 0 ' ; /* to avoid copying records */
end;
/
This will build a table with exactly the same columns of the starting one, with no need for scanning all the columns. This way you will not copy the records of the starting table; if you want to copy records, simply remove the WHERE condition.
As correctly said by Alex Poole, this will only create the clone table, but will not create any trigger, index, foreign key, ... existing on the cloned table.
Query USER_TAB_COLUMNS to get a list of columns and their types.
Since you mention DESCRIBE (...), you must be using SQL*Plus - or a graphical program that understands SQL*Plus commands, like Toad or SQL Developer. Unfortunately, you CAN'T execute the DESCRIBE command in SQL or in PL/SQL, because DESCRIBE is a SQL*Plus command, it is NOT a SQL or PL/SQL command.
If you do use SQL Developer or Toad, they have a feature where you bring up a table and it gives you the SQL (not PL/SQL - that is not needed, plain and very fast SQL is all that's needed) to re-create the tables, INCLUDING constraints and comments. Below I am reproducing the output of using this feature in SQL Developer, on a practice SQL table. This only creates the table structure, not its data; you will still have to copy the data over, for example with
INSERT INTO (new_table) (SELECT * FROM old_table)
The advantage over Alexsej's solution is that the data type will be copied exactly; in Aleksej's solution the columns will not necessarily be EXACTLY the same - [for example, in the old table you may have a VARCHAR2(300) column; the width, 300, will not be copied with his method, and instead the width of the actual data present in the table will be used.] Edit: As Alex Poole pointed out in a comment, what I said here (in square brackets) is INCORRECT, cloning a table with Aleksej's solution WILL preserve column widths and such. (Also, his method will not copy the constraints, like NOT NULL and UNIQUE.)
The method I am recommending still doesn't re-create triggers, but it does re-create constraints and indexes.
Here is an example of what SQL Developer can do for you, with NO EFFORT on your part:
CREATE TABLE "INTRO"."COURSES"
( "CODE" VARCHAR2(6 BYTE) NOT NULL ENABLE,
"DESCRIPTION" VARCHAR2(30 BYTE) NOT NULL ENABLE,
"CATEGORY" CHAR(3 BYTE) NOT NULL ENABLE,
"DURATION" NUMBER(2,0) NOT NULL ENABLE,
CONSTRAINT "COURSES_PK" PRIMARY KEY ("CODE")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "SYSTEM" ENABLE,
CONSTRAINT "COURSES_CAT_CHK" CHECK (CATEGORY in ('GEN','BLD','DSG')) ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "SYSTEM" ;
COMMENT ON COLUMN "INTRO"."COURSES"."CODE" IS 'Unique course code';
COMMENT ON COLUMN "INTRO"."COURSES"."DESCRIPTION" IS 'Course description (title)';
COMMENT ON COLUMN "INTRO"."COURSES"."CATEGORY" IS 'Course category (GEN, BLD or DSG)';
COMMENT ON COLUMN "INTRO"."COURSES"."DURATION" IS 'Course duration (in days)';
Good luck!

How to generate script lists in Oracle

IN SQL Server there is a tasks feature and then you can 'generate scripts' which is useful for things like views and stored procedures, or more.
In Oracle, can I generate such a copy of Views? How is this done? I am using PL SQL.
I am trying to update my Oracle database on my laptop. REcently many new views and stored procedures were added to the database I am working on at job. I also work from laptop for development.
Rather than backup the entire database, if I can just copy these views and sp's would be far easier.
It sounds like you are probably looking for the DBMS_METADATA package and the GET_xxx functions within that package. This is what tools like SQL Developer and other GUIs will call to generate the DDL for a particular object.
So, for example, if you wanted to get the DDL for the EMP table in the SCOTT schema in SQL (or PL/SQL), you can
SQL> ed
Wrote file afiedt.buf
1 select dbms_metadata.get_ddl( 'TABLE', 'EMP', 'SCOTT' )
2* from dual
SQL> /
DBMS_METADATA.GET_DDL('TABLE','EMP','SCOTT')
--------------------------------------------------------------------------------
CREATE TABLE "SCOTT"."EMP"
( "EMPNO" NUMBER(4,0),
"ENAME" VARCHAR2(10),
"JOB" VARCHAR2(9),
"MGR" NUMBER(4,0),
"HIREDATE" DATE,
"SAL" NUMBER(7,2),
"COMM" NUMBER(7,2),
"DEPTNO" NUMBER(2,0),
CONSTRAINT "PK_EMP" PRIMARY KEY ("EMPNO")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DE
FAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS"
ALTER INDEX "SCOTT"."PK_EMP" UNUSABLE ENABLE,
CONSTRAINT "FK_DEPTNO" FOREIGN KEY ("DEPTNO")
REFERENCES "SCOTT"."DEPT" ("DEPTNO") ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DE
FAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS"
CACHE

is it possible to get Oracle SQL Developer to escape output when exporting as inserts?

Is it possible, and if so, how, to tell Oracle SQL Developer to escape single quotes whenn exporting a table to inserts? It seems like an obvious thing to want to do, escape the character being used to quote fields when it occurs within the field, but I cannot find the option to do it.
Update: version 2.1.1.64, export method is. Context menu>export data>insert...
I just tried it using SQL Developer 3.0.04, and it appears that the single quotes in data are escaped by default:
--------------------------------------------------------
-- File created - Wednesday-June-01-2011
--------------------------------------------------------
--------------------------------------------------------
-- DDL for Table T3
--------------------------------------------------------
CREATE TABLE "THEUSER"."T3"
("C1" VARCHAR2(20 BYTE),
"C2" VARCHAR2(20 BYTE)
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS" ;
REM INSERTING into THEUSER.T3
Insert into THEUSER.T3 (C1,C2) values ('C1','C''2');
I just had one row in the table, consisting of the values C1 and C'2.

Resources