We have a production environment with Oracle 12C. It has two instances and is using Oracle Data Guard to have a replica in another server.
Every time that we release a new version, we have to truncate some tables, which doesn’t have any index, and execute several scripts with inserts statements like this one:
ALTER TABLE T_I18N DISABLE ALL TRIGGERS;
TRUNCATE TABLE T_I18N;
INSERT INTO T_I18N VALUES ('sp', 'Table 1', '0', 'Tabla 1');
INSERT INTO T_I18N VALUES ('sp', 'Table 2', '0', 'Tabla 2');
INSERT INTO T_I18N VALUES ('sp', 'Table 3', '0', 'Tabla 3');
.
.
ALTER TABLE HIS_I18N ENABLE ALL TRIGGERS;
Previously, we also have disabled all the primary keys and foreign keys. To give more information, the table structure is the following one:
CREATE TABLE "T_I18N"
("T_CODE" VARCHAR2(5 BYTE),
"T_KEY" VARCHAR2(200 BYTE),
"T_C_ID" NUMBER(6,0),
"T_VALUE" VARCHAR2(500 BYTE),
CONSTRAINT "HIN_PK" PRIMARY KEY ("T_CODE", "T_KEY", "T_C_ID")
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 "TB1" ENABLE,
CONSTRAINT "HIN_CEN_FK" FOREIGN KEY ("T_C_ID")
REFERENCES "TB1"."T_CEN" ("T_C_ID") 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 "TB1";
The script with more Insert statements has about 20K and it takes about 12 minutes to be executed. In my opinion, it was taking too much time, so I took a different approach. I tried to drop the table, recreate it and then execute the script, and it took only 40 seconds.
I have been searching through Oracle website to understand why there are so much difference between drop/recreate and truncate, but unfortunately I couldn’t find any clue about it.
Thanks by advance. Any information or advice would be really appreciated.
Cheers!
Related
I worked with database tables... Paradoxically, I clicked somewhere else and now there are no these tables. More precisely, sql developer does not show them, unless you call select * from name_table and then only through f5. Otherwise, he does not show information. What should I do to have SQL developer output table data again?
If anyone has faced such a problem, please tell me!
Such a script should have been originally:
CREATE TABLE "SPISOK_DOLGNOSTY"
( "KOD_DOLGN" NUMBER(*,0),
"NAME_DOLGN" VARCHAR2(50 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 SPISOK_DOLGNOSTY
SET DEFINE OFF;
Insert into SPISOK_DOLGNOSTY (KOD_DOLGN,NAME_DOLGN) values (1,'Директор предприятия');
Insert into SPISOK_DOLGNOSTY (KOD_DOLGN,NAME_DOLGN) values (2,'Ведущий инженер');
Insert into SPISOK_DOLGNOSTY (KOD_DOLGN,NAME_DOLGN) values (5,'Доцент');
Insert into SPISOK_DOLGNOSTY (KOD_DOLGN,NAME_DOLGN) values (9,'Канцелярист');
Insert into SPISOK_DOLGNOSTY (KOD_DOLGN,NAME_DOLGN) values (12,'Начальник отдела кадров');
Insert into SPISOK_DOLGNOSTY (KOD_DOLGN,NAME_DOLGN) values (20,'Старший преподаватель');
Insert into SPISOK_DOLGNOSTY (KOD_DOLGN,NAME_DOLGN) values (21,'Техник первой категории');
Insert into SPISOK_DOLGNOSTY (KOD_DOLGN,NAME_DOLGN) values (118,'Сантехник');
Insert into SPISOK_DOLGNOSTY (KOD_DOLGN,NAME_DOLGN) values (120,'Кадровик');
--------------------------------------------------------
-- DDL for Index SYS_C007193
--------------------------------------------------------
CREATE UNIQUE INDEX "SYS_C007193" ON "SPISOK_DOLGNOSTY" ("KOD_DOLGN")
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 "USERS" ;
--------------------------------------------------------
-- DDL for Index SYS_C007112
--------------------------------------------------------
CREATE UNIQUE INDEX "SYS_C007112" ON "SPISOK_DOLGNOSTY" ("NAME_DOLGN")
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 "USERS" ;
--------------------------------------------------------
-- Constraints for Table SPISOK_DOLGNOSTY
--------------------------------------------------------
ALTER TABLE "SPISOK_DOLGNOSTY" ADD UNIQUE ("NAME_DOLGN")
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 "USERS" ENABLE;
ALTER TABLE "SPISOK_DOLGNOSTY" ADD PRIMARY KEY ("KOD_DOLGN")
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 "USERS" ENABLE;
ALTER TABLE "SPISOK_DOLGNOSTY" MODIFY ("NAME_DOLGN" NOT NULL ENABLE);
enter image description here
enter image description here
enter image description here
enter image description here
Your DDL, used on a Oracle 12cR2 database, running in a more recent version of SQL Developer (20.0), works just fine.
Try:
restarting the application
check the log panel for errors
UPGRADE your software, it's free and just a simple UnZip and go
I am testing a query with a table of 500 million records.
The field ID_COMPANY only has value 1 (500 millon records).
The SQL is defined as follows:
CREATE TABLE "BIDATA"."CSC_CUSTOMER_PREPAID_BALANCE"
( "ID_COMPANY" VARCHAR2(10 BYTE),
"SEQUENTIAL_MOV" NUMBER(10,0),
"ID_PAYMENT" NUMBER(10,0)
CONSTRAINT "PK_CSC_CUSTOMER_PREPAID_BAL" PRIMARY KEY ("ID_COMPANY", "SEQUENTIAL_MOV")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS NOLOGGING
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 "BIDATA_DATOS" ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 0 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 "BIDATA_DATOS" ;
CREATE INDEX clause for the ID_PAYMENT field for example
CREATE INDEX "BIDATA"."IDX_ID_PAYMENT" ON "BIDATA"."CSC_CUSTOMER_PREPAID_BALANCE" ("ID_PAYMENT")
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 "BIDATA_DATOS" ;
The problem that I have, that when launching a query as defined, it seems that it does not detect the index ID_COMPANY and it takes more than 15 minutes to show results, but if in the query in the WHERE clause I add ID_COMPANY = '1', it shows the results in less than 10 seconds.
Should I delete the index ID_COMPANY and define it as CREATE INDEX?
Why does that time difference occur?
SQL query takes> 10 minutes.
SELECT
COUNT(*)
FROM
CSC_CUSTOMER_ACCOUNT,
CSC_CUSTOMER_PREPAID_BALANCE
WHERE
AND ( CSC_CUSTOMER_ACCOUNT.ID_ACCOUNT(+)=CSC_CUSTOMER_PREPAID_BALANCE.ID_ACCOUNT AND CSC_CUSTOMER_ACCOUNT.ID_COMPANY(+)=CSC_CUSTOMER_PREPAID_BALANCE.ID_COMPANY )
AND ( CSC_CUSTOMER_PREPAID_BALANCE.ID_ACCOUNT (+)=CSC_RECHARGE_MOBILE.ID_ACCOUNT AND CSC_CUSTOMER_PREPAID_BALANCE.SEQUENTIAL_MOV(+)=CSC_RECHARGE_MOBILE.SEQUENTIAL_MOV )
SQL query takes < 10 seconds.
SELECT
COUNT(*)
FROM
CSC_CUSTOMER_ACCOUNT,
CSC_CUSTOMER_PREPAID_BALANCE
WHERE
AND ( CSC_CUSTOMER_ACCOUNT.ID_ACCOUNT(+)=CSC_CUSTOMER_PREPAID_BALANCE.ID_ACCOUNT AND CSC_CUSTOMER_ACCOUNT.ID_COMPANY(+)=CSC_CUSTOMER_PREPAID_BALANCE.ID_COMPANY )
AND ( CSC_CUSTOMER_PREPAID_BALANCE.ID_ACCOUNT (+)=CSC_RECHARGE_MOBILE.ID_ACCOUNT AND CSC_CUSTOMER_PREPAID_BALANCE.SEQUENTIAL_MOV(+)=CSC_RECHARGE_MOBILE.SEQUENTIAL_MOV )
AND CSC_CUSTOMER_PREPAID_BALANCE.ID_COMPANY = '1'
QUERY execution plan> 10 minutes
Execution plan Query > 10
Execution plan Query < 10 seconds
Thank you.
From what I can infer from your question, you seem to misinterpret the indexing rules for a table. Ideally you don't create an index for the primary key, rather it is the other way around. i.e. when you create a table with a primary key it(primary key) by default is considered as a clustered index by the sql agent and created/stored accordingly.
More on Indexing and Index types here
i have a daily job and this job is doing basically
LOAD Temp table
Exchange partition with source table
Rebuild local indexes
Rebuild global indexes
However, problem is that rebuilding indexes takes lots of time and this makes source table unavailable
during this period. source table is very critical table to support real time application.
Because of this case, web services which uses this table getting time out exceptions.
Do i have any alternative way rather than building this indexes on source table ?
Any help or discussion is very appreciated.
You can find the code snipped of the daily job, and the structure of source table (TABLEX) and temp table
(TABLEX_TEMP)
Daily Job:
` CREATE OR REPLACE PROCEDURE X.LOAD__TABLES_X IS
BEGIN
EXECUTE IMMEDIATE 'TRUNCATE TABLE TABLEX_TEMP REUSE STORAGE';
INSERT /*+ APPEND */ INTO TABLEX_TEMP(CUST_NO ,IDNO,SEX,NAME,SURNAME)
SELECT CUST_NO ,IDNO,SEX,NAME,SURNAME,PHONE
FROM T_X WHERE MAINT !='D';
COMMIT;
EXECUTE IMMEDIATE 'ALTER TABLE TABLEX EXCHANGE PARTITION DUMMY WITH TABLE TABLEX_TEMP WITHOUT VALIDATION';
EXECUTE IMMEDIATE 'ALTER TABLE TABLEX MODIFY PARTITION DUMMY REBUILD UNUSABLE LOCAL INDEXES';
EXECUTE IMMEDIATE 'ALTER INDEX PK_CUST_NO REBUILD NOCOMPRESS NOPARALLEL TABLESPACE TS_X_INDEX';
EXECUTE IMMEDIATE 'ALTER INDEX PK_CUST_NO_TMP REBUILD NOCOMPRESS NOPARALLEL TABLESPACE TS_X_INDEX';
EXECUTE IMMEDIATE 'ALTER INDEX IDX_TABLEX REBUILD NOCOMPRESS NOPARALLEL TABLESPACE TS_X_INDEX';
EXECUTE IMMEDIATE 'ALTER INDEX IDX_TABLEX_TMP REBUILD NOCOMPRESS NOPARALLEL TABLESPACE TS_X_INDEX';
EXECUTE IMMEDIATE 'TRUNCATE TABLE TABLEX_TEMP REUSE STORAGE';
COMMIT;
END LOAD_TABLES_X;`
Structure of Tables and indexes :
`
CREATE TABLE X.TABLEX_TEMP
(
CUST_NO NUMBER(9),
NAME VARCHAR2(54 BYTE),
SURNAME VARCHAR2(100 BYTE),
SEX VARCHAR (1 BYTE)
IDNO NUMBER(11)
)
TABLESPACE TS_X_DATAA
RESULT_CACHE (MODE DEFAULT)
PCTUSED 0
PCTFREE 0
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 8M
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
)
LOGGING
COMPRESS FOR QUERY HIGH
NOCACHE
NOPARALLEL
MONITORING;
CREATE INDEX X.IDX_TABLEX_TMP ON X.TABLEX_TEMP
(IDNO)
NOLOGGING
TABLESPACE TS_X_INDEX
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
)
NOPARALLEL;
CREATE UNIQUE INDEX X.PK_CUST_NO_TMP ON X.TABLEX_TEMP
(CUST_NO)
NOLOGGING
TABLESPACE TS_X_INDEX
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
)
NOPARALLEL;
ALTER TABLE X.TABLEX_TEMP ADD (
CONSTRAINT PK_CUST_NO_TMP
PRIMARY KEY
(CUST_NO)
USING INDEX X.PK_CUST_NO_TMP
ENABLE NOVALIDATE);
----------------------------------------------------------
CREATE TABLE X.TABLEX
(
CUST_NO NUMBER(9),
NAME VARCHAR2(54 BYTE),
SURNAME VARCHAR2(100 BYTE),
SEX VARCHAR (1 BYTE)
IDNO NUMBER(11)
)
COMPRESS FOR QUERY HIGH
TABLESPACE TS_X_DATA
RESULT_CACHE (MODE DEFAULT)
PCTUSED 0
PCTFREE 0
INITRANS 1
MAXTRANS 255
STORAGE (
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
)
PARTITION BY RANGE (CUST_NO)
(
PARTITION DUMMY VALUES LESS THAN (999999999)
LOGGING
COMPRESS FOR QUERY HIGH
TABLESPACE TS_X_DATA
PCTFREE 0
INITRANS 1
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
)
)
NOCACHE
NOPARALLEL
MONITORING;
CREATE INDEX X.IDX_TABLEX ON X.TABLEX
(IDNO)
NOLOGGING
TABLESPACE TS_X_INDEX
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
)
NOPARALLEL;
CREATE UNIQUE INDEX X.PK_CUST_NO ON X.TABLEX
(CUST_NO)
NOLOGGING
TABLESPACE TS_X_INDEX
PCTFREE 10
INITRANS 2
MAXTRANS 255
STORAGE (
INITIAL 64K
NEXT 1M
MINEXTENTS 1
MAXEXTENTS UNLIMITED
PCTINCREASE 0
BUFFER_POOL DEFAULT
FLASH_CACHE DEFAULT
CELL_FLASH_CACHE DEFAULT
)
NOPARALLEL;
ALTER TABLE X.TABLEX ADD (
CONSTRAINT PK_CUST_NO
PRIMARY KEY
(CUST_NO)
USING INDEX X.PK_CUST_NO
ENABLE NOVALIDATE);
`
Well, since a partition exchange modifies a substantial part of the data the index has to become unusable. However, you can avoid the index to become unusable by updating the indexes during the partition exchange.
In my experience it is is best to use a two-step approach:
Prior to the partition exchange you should build the same local indexes on the temporary table. Then you must append INCLUDING INDEXES to the ALTER TABLE command.
If you do have to use global indexes you can update them during the partition exchange by appending UPDATE GLOBAL INDEXES to the ALTER TABLE command. This will ensure that the global indexes are not unusable during the whole operation.
So you whole statement will become something like this:
ALTER TABLE TABLEX EXCHANGE PARTITION DUMMY WITH TABLE TABLEX_TEMP INCLUDING INDEXES WITHOUT VALIDATION UPDATE GLOBAL INDEXES;
You may want to look at the official Oracle documentation for details:
Updating Indexes Automatically
ALTER TABLE
This question already has answers here:
How to get Oracle create table statement in SQL*Plus
(3 answers)
Closed 1 year ago.
How can I generate a DDL script on an existing table in oracle? I am working on a project where I have to re-create some tables that are present in Oracle table into Hive.
If your SQL client doesn't support this, then you can use the dbms_metadata package to get the source for nearly everything in your database:
For a table use something like this:
select dbms_metadata.get_ddl('TABLE', 'YOUR_TABLE_NAME')
from dual;
You can also do this for all tables at once:
select dbms_metadata.get_ddl('TABLE', table_name)
from user_tables;
and spool the output into a SQL script.
More details are in the manual: http://docs.oracle.com/cd/E11882_01/appdev.112/e40758/d_metada.htm
Just expanding a bit on #a_horse_with_no_name's answer. Using DBMS_METADATA, you might have to take care of the format in SQL*Plus in order to get the output properly.
For example, I want to get the DDL for SCOTT.EMP table.
SQL> select dbms_metadata.get_ddl('TABLE', 'EMP')
2 from dual;
DBMS_METADATA.GET_DDL('TABLE','EMP')
--------------------------------------------------------------------------------
CREATE TABLE "SCOTT"."EMP"
( "EMPNO" NUMBER(4,0),
"ENAME" VARCHAR2(10),
SQL>
But, that is not what I expected.
So, setting up the format properly, would give me my desired output
SQL> set long 100000
SQL> set head off
SQL> set echo off
SQL> set pagesize 0
SQL> set verify off
SQL> set feedback off
SQL> select dbms_metadata.get_ddl('TABLE', 'EMP')
2 from dual;
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 DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS" 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 DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS"
SQL>
In MySql you can see the table definition (columns with their data types etc) with show create table table_name.
Is there a similar functionality for oracle sql?
If you are asking about SQL*Plus commands (show create table table_name doesn't appear to be a SQL statement), you can use the desc command
SQL> desc emp
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NOT NULL NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(7,2)
COMM NUMBER(7,2)
DEPTNO NUMBER(2)
If you really want a SQL statement, you can use the dbms_metadata package
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
Depending on the tool you are using, you may need to run set long 10000 first, that tells SQL*Plus to display the first 10,000 bytes of any LOB that is selected. If your DDL is longer, set a larger value.
Use DESC:
DESC mytable
Will show you the columns, but unfortunately the create statement is not available using standard oracle tools.
SQL> set long 1000
SQL> set pagesize 0
SQL> select DBMS_METADATA.GET_DDL('TABLE','TABLE NAME'[,'SCHEMA']) from DUAL
If you are using PL/SQL Developer; Right click on table, Select View, at the right bottom of the view window click on 'View SQL' button.
Just a note that table name is case_sensitive, you need to pass in table name as upper case.
DDL is working for me and more simple all you need is to write DDL (SCHEMA_OWNER).(TABLE_NAME) ...for example ddl HR.LOCATIONS;....HR is the schema and LOCATION is table name ...make sure you write both the SCHEMA name and table NAME in capital here the output will be
CREATE TABLE "HR"."LOCATIONS"
( "LOCATION_ID" NUMBER(4,0),
"STREET_ADDRESS" VARCHAR2(40),
"POSTAL_CODE" VARCHAR2(12),
"CITY" VARCHAR2(30) CONSTRAINT "LOC_CITY_NN" NOT NULL ENABLE,
"STATE_PROVINCE" VARCHAR2(25),
"COUNTRY_ID" CHAR(2),
CONSTRAINT "LOC_ID_PK" PRIMARY KEY ("LOCATION_ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 NOLOGGING 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 "EXAMPLE" ENABLE,
CONSTRAINT "LOC_C_ID_FK" FOREIGN KEY ("COUNTRY_ID")
REFERENCES "HR"."COUNTRIES" ("COUNTRY_ID") ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS NOLOGGING
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 "EXAMPLE" ;
COMMENT ON COLUMN "HR"."LOCATIONS"."LOCATION_ID" IS 'Primary key of
locations table';
COMMENT ON COLUMN "HR"."LOCATIONS"."STREET_ADDRESS" IS 'Street address
of an office, warehouse, or production site of a company.
Contains building number and street name';
COMMENT ON COLUMN "HR"."LOCATIONS"."POSTAL_CODE" IS 'Postal code of
the
location of an office, warehouse, or production site
of a company. ';
COMMENT ON COLUMN "HR"."LOCATIONS"."CITY" IS 'A not null column that
shows city where an office, warehouse, or
production site of a company is located. ';
COMMENT ON COLUMN "HR"."LOCATIONS"."STATE_PROVINCE" IS 'State or
Province where an office, warehouse, or production site of a
company is located.';
COMMENT ON COLUMN "HR"."LOCATIONS"."COUNTRY_ID" IS 'Country where an
office, warehouse, or production site of a company is
located. Foreign key to country_id column of the countries table.';
COMMENT ON TABLE "HR"."LOCATIONS" IS 'Locations table that contains
specific address of a specific office,
warehouse, and/or production site of a company. Does not store
addresses /
locations of customers. Contains 23 rows; references with the
departments and countries tables. ';
CREATE INDEX "HR"."LOC_CITY_IX" ON "HR"."LOCATIONS" ("CITY")
PCTFREE 10 INITRANS 2 MAXTRANS 255 NOLOGGING 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 "EXAMPLE" ;
CREATE INDEX "HR"."LOC_COUNTRY_IX" ON "HR"."LOCATIONS" ("COUNTRY_ID")
PCTFREE 10 INITRANS 2 MAXTRANS 255 NOLOGGING 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 "EXAMPLE" ;
CREATE INDEX "HR"."LOC_STATE_PROVINCE_IX" ON "HR"."LOCATIONS"
("STATE_PROVINCE")
PCTFREE 10 INITRANS 2 MAXTRANS 255 NOLOGGING 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 "EXAMPLE" ;