This question already has answers here:
`show create table` equivalent in oracle sql
(6 answers)
Closed 5 years ago.
i am trying to show the syntax of a created table as described below
here is the sturucture of the table:
i need the complete syntax of table creation as:
CREATE TABLE STUDENT (R_NUMBER NUMBER(4) PRIMARY KEY NOT NULL,NAME VARCHAR2(20) NOT NULL,F_NAME VARCHAR2(20) NOT NULL, M_NAME VARCHAR2(20) NOT NULL, ADMN_NO REFERENCES FEE NUMBER(4);
Is there any data dictionary through which I can get this output?
Thanks in advance
I would try dbms_metadata.get_ddl using something like
SELECT dbms_metadata.get_ddl('TABLE', 'STUDENT', 'MYSCHEMA') -- Replace MYSCHEMA with your actual schema name
FROM dual
you can query data dictionary views user_tab_columns and user_cons_columns joined :
select c.column_name, c.data_type, c.nullable, s.constraint_name, c.data_length
from user_tab_columns c , user_cons_columns s
where c.table_name = 'AB'
and s.column_name(+) = c.column_name
and s.table_name(+) = c.table_name;
Related
Guys my question is I don't know the constraint names that I've added to a table. But I need to remove or disable all those foreign key constraints. But how?
SQL> desc orders;
Name Null? Type
----------------------------------------- -------- ----------------------------
ORDER_ID NOT NULL VARCHAR2(10)
PRODUCT_ID VARCHAR2(10)
DATE_OF_ORDER TIMESTAMP(6)
CUST_ID VARCHAR2(10)
QUANTITY NUMBER(38)
TOTAL_PRICE FLOAT(10)
DELIVERY_STATUS VARCHAR2(10)
The related foreign constraints might be determined by using user_constraints dictionary view, and you can disable the foreign key constraint of this table by the following code block
BEGIN
FOR c IN
(
SELECT *
FROM user_constraints c
WHERE c.constraint_type = 'R'
AND c.table_name = 'ORDERS')
LOOP
EXECUTE IMMEDIATE 'ALTER TABLE '||c.table_name||
' DISABLE CONSTRAINT '||c.constraint_name;
END LOOP;
END;
/
If you're using a tool like sql developer you can just right click the table name in the navigator and select constraint > disable all or drop
I have created a sample table with the first column defined as citext.
CREATE TABLE users (
nick CITEXT PRIMARY KEY,
pass TEXT NOT NULL
);
When I try to execute the following query to get the column names and their data types, the data type of the nick column is returned as USER-DEFINED.
select column_name, data_type from information_schema.columns
where table_name = 'users';
column_name | data_type
1 nick | USER-DEFINED
2 pass | text
Is there any way I can find out by querying any table in postgreSQL if the column nick is of type citext?
You need to use column udt_name instead of data_type:
create type compfoo AS (f1 int, f2 text);
create table compfoo_table (cp compfoo);
select udt_name from information_schema.columns where column_name = 'cp';
-- drop table compfoo_table;
-- drop type compfoo;
Documentation
A piece of legacy code I'm running that does some oracle SQL is violating a constraint called REF_REQUEST.
I look up this constraint by doing:
select * from all_constraints where constraint_name='REF_REQUEST'
This tells me that the constraint_type is 'R', and it gives me the table name of CORRESPONDENCE.
However, I still don't know... what value I'm missing from CORRESPONDENCE, or where I'm trying to insert that is causing the issue, and what column is relevant from each. How can I learn this information by querying the dB?
Alternatively, you know it's a referential integrity constraint based on your select from all_constraints (constraint_type = R), so you can just query all_cons_columns for your answer:
SELECT table_name, column_name
FROM all_cons_columns
WHERE constraint_name = 'REF_REQUEST'
ORDER by position;
Use get_ddl method in dbms_metadata package to get more details about the constraint
SELECT CAST(DBMS_METADATA.GET_DDL('CONSTRAINT','REF_REQUEST','CERTIFICATION') AS VARCHAR2(4000))
FROM DUAL
That should tell you what columns the constraint is acting on
One of the overloaded methods accepts the schema name, so you can pass it as a parameter.
DBMS_METADATA.GET_DDL (
object_type IN VARCHAR2,
name IN VARCHAR2,
schema IN VARCHAR2 DEFAULT NULL,
version IN VARCHAR2 DEFAULT 'COMPATIBLE',
model IN VARCHAR2 DEFAULT 'ORACLE',
transform IN VARCHAR2 DEFAULT 'DDL')
RETURN CLOB;
Further reading:
Oracle documentation on dbms_metadata.get_xxx subprograms
In Oracle 10g, how can I drop a unique constraint on a column without knowing the name of the constraint (e.g. a system generated name, which won't necessarily be the same across database instances)? Dropping and recreating the table isn't an option. Is it possible?
You can retrieve the constraint's name with:
SELECT CONSTRAINT_NAME
FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'xxx'
AND CONSTRAINT_TYPE = 'U'
You can for instance create a stored procedure that executes the previous sql, stores its result in a variable and uses this variable in ALTER TABLE DROP CONSTRAINT
EDIT: e.g.:
BEGIN
FOR r IN (
SELECT TABLE_NAME, CONSTRAINT_NAME
FROM USER_CONSTRAINTS WHERE TABLE_NAME = 'xxx'
AND CONSTRAINT_TYPE = 'U') LOOP
EXECUTE IMMEDIATE REPLACE(REPLACE(
'ALTER TABLE #TABLE# DROP CONSTRAINT #CON#'
,'#TABLE#',r.TABLE_NAME)
,'#CON#',r.CONSTRAINT_NAME);
END LOOP;
END;
Hello I am trying to display the constraints in one of my tables but for some reason I get the message no rows selected. Noted below is the table I have created.
Create table Teams (
TeamID varCHAR2(4) constraint Teams_TeamID_PK Primary Key,
TeamName VARCHAR2(40)
);
This is the code I am using to show my constraints.
SELECT constraint_name,
constraint_type,
search_condition
FROM USER_CONSTRAINTS
WHERE table_name = 'Teams';
I am a rookie so I want to make sure I understand what is wrong. I have tried to drop the table thinking that my constraints did not take - I did not, nor did I receive any errors when I created the table and I am referencing TeamID in another table. So when I try to drop the table I get an error message when is what I was hoping for.
Try this:
SELECT constraint_name,
constraint_type,
search_condition
FROM USER_CONSTRAINTS
WHERE table_name = 'TEAMS';
Unless double-quoted when created, all object names in Oracle are upper case.
I personally use:
SELECT * FROM all_constraints WHERE Table_Name = <TableName>;
Use the following code:
show create table table_name;
select dbms_mview.get_ddl('TABLE',USER,'TEAMS') from dual;
If you prefer the CamelCase names, your create table script should have been:
Create table "Teams" (
"TeamID" varCHAR2(4) constraint "Teams_TeamID_PK" Primary Key,
"TeamName" VARCHAR2(40)
);
Without double-quotes Oracle helpfully converts all identifiers to uppercase :)
Type the table name in upper case in where clause within the single quotes.
e.g. WHERE table_name = 'TEAMS';