Lesson 10 | The DESCRIBE Command |
Objective |
Oracle DESCRIBE command to show table structure using SQL* Plus |
Oracle DESCRIBE Command using SQL*PLus
In "Oracle 19c", you can use the `DESCRIBE` command in SQL*Plus to view the structure of a table. This command provides details about the table's columns, data types, and constraints.
Syntax
DESC table_name;
or
DESCRIBE table_name;
Steps to Use `DESCRIBE` in SQL*Plus
-
Log in to SQL*Plus:
- Run the
DESCRIBE
Command:
Additional Notes
- Viewing a list of Columns
A DBA can use SQL*Plus to execute the DESCRIBE command to show the structure of a table. DESCRIBE
is usually used to view a list of columns in a database table or view. The following example shows how it would be used to list the column definitions for the DBA_USERS
view.
Use the DESCRIBE command to show the Structure of a table for DBA_USERS view
In Oracle 19c, the `DESCRIBE` command (or its shorthand `DESC`) is used to display the structure of a table, view, or other database objects. To show the structure of the `DBA_USERS` view, you can use the following command:
DESC DBA_USERS;
Explanation:
DESC
is a shorthand for DESCRIBE
.
DBA_USERS
is a predefined Oracle data dictionary view that contains information about all users in the database.
Output: The output will display the following columns for the `DBA_USERS` view:
- Column Name: The name of the column in the view.
- Null?: Indicates whether the column allows null values (
Y
for yes, N
for no).
- Type: The data type of the column (e.g.,
VARCHAR2
, NUMBER
, DATE
).
Example Output:
Name Null? Type
-------------- -------- ------------
USERNAME NOT NULL VARCHAR2(128)
USER_ID NOT NULL NUMBER
PASSWORD VARCHAR2(4000)
ACCOUNT_STATUS NOT NULL VARCHAR2(32)
Notes:
- Permissions: To query the
DBA_USERS
view, you must have the appropriate privileges, such as SELECT ANY DICTIONARY
or SELECT_CATALOG_ROLE
.
- Scope: The
DBA_USERS
view contains information about all users in the database. If you want information about the current user, you can use the USER_USERS
view instead.
If you don't have access to the `DBA_USERS` view, you can check your privileges or contact your database administrator.
Syntax for the Oracle DESCRIBE Command
DESC[RIBE] {[schema.]object[@db_link]}
Lists the column definitions for the specified table, view or synonym, or the specifications for the specified function or procedure.
- Terms
- schema: Represents the schema where the object resides. If you omit schema, SQL*Plus assumes you own object.
- object: Represents the table, view, type, procedure, function, package or synonym you wish to describe.
- @db_link: Consists of the database link name corresponding to the database where object exists. For more information on which privileges allow access to another table in a different schema, refer to the Oracle Database SQL Reference.
- Usage The description for tables, views, types and synonyms contains the following information:
- each column's name
- whether or not null values are allowed (NULL or NOT NULL) for each column
- datatype of columns, for example, CHAR, DATE, LONG, LONGRAW, NUMBER, RAW, ROWID, VARCHAR2 (VARCHAR), or XMLType
- precision of columns (and scale, if any, for a numeric column)
When you do a DESCRIBE, VARCHAR columns are returned with a type of VARCHAR2. The DESCRIBE command enables you to describe objects recursively to the depth level set in the SET DESCRIBE command. You can also display the line number and indentation of the attribute or column name when an object contains multiple object types. For more information, see the SET command. To control the width of the data displayed, use the SET LINESIZE command. Columns output for the DESCRIBE command are typically allocated a proportion of the linesize currently specified. Decreasing or increasing the linesize with the SET LINESIZE command usually makes each column proportionally smaller or larger. This may give unexpected text wrapping in your display. For more information, see the SET command.
The description for functions and procedures contains the following information:
- the type of PL/SQL object (function or procedure)
- the name of the function or procedure
- the type of value returned (for functions)
- the argument names, types, whether input or output, and default values, if any
- the ENCRYPT keyword to indicate whether or not data in a column is encrypted
SQL> DESCRIBE dba_users
The
DBA_USERS
view is a data dictionary view that returns information about the users who can log in to an Oracle database.
Viewing function, procedure, and package headers
The
DESCRIBE
command can also be used to view function, procedure, and package
headers. The following command for example, would tell you about all the entry points in the
UTL_FILE
package, which is used to read and write files from within stored procedures.
SQL> DESCRIBE utl_file
You can also retrieve the entire command that can be used to recreate the table:
select dbms_metadata.get_ddl('TABLE','< my table name>','<table owner>')
from dual;
Viewing more Specific Information
Describe Objects Recursively
The DESCRIBE command allows you to describe objects recursively to the depth level set in the SET DESCRIBE command. For example use the SET commands:
SET LINESIZE 80
SET DESCRIBE DEPTH 2
SET DESCRIBE INDENT ON
SET DESCRIBE LINE OFF
To display these settings use:
SHOW DESCRIBE
- Data Types
The description for functions and procedures contains the type of PL/SQL object (function or procedure) the name of the function or procedure, the type of value returned (for functions) the argument names, types, whether input or output, and default values, if any. DESC user.object_name will always identify a distinct database object because a user's database objects must have unique names.
i.e. you cannot create a FUNCTION with the same name as a TABLE in the same schema.
- Data Dictionary:
An alternative to the DESC command is selecting directly from the data dictionary:
DESC MY_TABLE
is equivalent to
SELECT
column_name "Name",
nullable "Null?",
concat(concat(concat(data_type,'('),data_length),')') "Type"
FROM user_tab_columns
WHERE table_name='TABLE_NAME_TO_DESCRIBE';
- Column Comments
To view column comments:
SELECT comments
FROM user_col_comments
WHERE table_name='MY_TABLE';
SELECT 'comment on column
'||table_name||'.'||column_name||'
is '''||comments||''';'
FROM user_col_comments
WHERE comments is not null;
Writing code and find yourself typing in a series of column names?
Why bother when it's all available in the data dictionary.
The script below will help out:
COL.SQL
List all the columns of a table.
select chr(9)||lower(column_name)||','
from USER_tab_columns
where table_Name = UPPER('&1')
So now if you want a list of the columns in the EMP table simply type:
@col emp
This will produce a list of columns:
empno,
ename,
job,
mgr,
hiredate,
sal,
comm,
deptno,
