Displaying the constraints in a table - sql

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';

Related

SQL DROP TABLE FUNCTION

Hello i've created a table if in a VM provided by my university and inserted the following:
create table first_table (record_id int primary key,
first_name varchar(20) not null,
last_name varchar(20) not null);
However, instead of the first_name and last_name I actually inserted my name and am looking to drop the table to recreate it.
" I typed in DROP TABLE [IF EXISTS] first_table "
and then it simply doesn't do anything. Any idea why.
In the Postgresql documentation, when you see something inside square brackets such as [IF EXISTS] it means that "IF EXISTS" is optional. You shouldn't type the square brackets if you put that in.
In this case, though, you know that the table exists so just leave the "IF EXISTS" part off:
drop table first_table;
Given that the problem is that you typed your first and last names instead of the desired column name (first_name and last_name) you can simply rename the columns using
alter table first_table
rename leo as first_name
alter table first_table
rename whatever_your_last_name_is as last_name
Try this:
DROP TABLE first_table;
In PSQL you should be able to do so using the command
DROP TABLE first_table;

Can I alter the constraints of one table by using the constraints of another table?

I had to drop a table and remake it using an archive. In the process, I lost the table's constraints--things like the primary key--triggers, indices, and more. I have, however, the same table on a different DB, which has all the appropriate constraints.
I have tried adding the constraints, triggers, and indices manually, but there are just too many.
I was wondering if I could do something like:
alter table t73
modify col_n....col_n+1
using (select constraints from t73#otherdb)
No, that won't work.
What you could do is to use some GUI (like TOAD or SQL Developer), find table t73, have a look at its Script which contains all commands (CREATE TABLE, CREATE INDEX, CREATE CONSTRAINT, ...) and copy/paste the ones you need and execute them in your current database.
That would be quick.
If you want to do it right (you know, pretending you know what you're doing, just like I do), then see DBMS_METADATA.GET_DDL and extract those commands from the database.
The final result should be the same.
this is below example how you can use dbms_metadata.get_ddl oracle package
create table EX_EMPLOYEe ( id number(5) null, name varchar2(100))
/
alter table ex_Employee add constraint PK_EX_EMPLOYEE primary key (id)
/
alter table ex_Employee add constraint FK_EX_EMPLOYEE foreign key (id)
references ex_Employee1 (id)
/
create table EX_EMPLOYEe1 ( id number(5) null, name varchar2(100))
/
alter table ex_Employee1 add constraint PK_EX_EMPLOYEE1 primary key (id)
alter table SYS_PARAM_KEY_LABEL
add constraint FK1_SYS_PARAM_KEY_LABEL foreign key (KEY_GROUP_ID)
references SYS_PARAM_KEY_GROUP (KEY_GROUP_ID);
/
CREATE INDEX IDX_EX_EMPLOYEe on ex_employee(name)
/
Create or replace PROCEDURE P_EX_EMPLOYEe as
begin
select id from ex_employee where rownum=1;
end;
/
CREATE OR REPLACE TRIGGER TRG_EX_EMPLOYEe AFTER DELETE ON EX_EMPLOYEe
FOR EACH ROW
BEGIN
DELETE FROM ex_employee1 WHERE id = :OLD.ID;
END;
/
select to_char( dbms_metadata.get_ddl('CONSTRAINT', c.constraint_name)) from user_constraints c where table_name='EX_EMPLOYEE'
and c.constraint_type='P'
union
select to_char( dbms_metadata.get_ddl('REF_CONSTRAINT', c.constraint_name)) from user_constraints c where table_name='EX_EMPLOYEE'
and c.constraint_type='R'
union
select to_char( dbms_metadata.get_ddl('INDEX', c.index_name)) from user_indexes c where table_name='EX_EMPLOYEE'
union
select to_char( dbms_metadata.get_ddl('PROCEDURE', d.name)) from user_dependencies d where d.referenced_name='EX_EMPLOYEE'
and d.type='PROCEDURE'
union
select to_char( dbms_metadata.get_ddl('TRIGGER', d.name)) from user_dependencies d where d.referenced_name='EX_EMPLOYEE'
and d.type='TRIGGER'

H2 equivalent of Postgres `SERIAL` or `BIGSERIAL` column?

In Postgres, defining a column with SERIAL/BIGSERIAL has a triple effect as discussed here:
Define a int/bigint column.
Create a sequence object to generate auto-incrementing numbers.
Set the default of the column to call nextval() on the sequence.
Is there a similar shortcut command in H2 to get this related set of behavior?
If not, what would the long version of the SQL be?
Where does the sequence live? How can you adjust its value or reset it?
If you create a column as auto_increment (or identity) H2 creates a sequence in the background. The name of that sequence can be obtained by looking at information_schema.columns:
create table foo
(
id integer auto_increment,
other_column varchar(20)
);
If you then run:
select column_name, column_default
from information_schema.columns
where table_name = 'FOO'
and table_schema = 'PUBLIC';
You'll get something like this:
COLUMN_NAME | COLUMN_DEFAULT
-------------+-----------------------------------------------------------------------------
ID | (NEXT VALUE FOR PUBLIC.SYSTEM_SEQUENCE_C1C36118_ED1C_44D6_B573_6C00C5923EAC)
OTHER_COLUMN |
You can alter that sequence without problems:
alter sequence SYSTEM_SEQUENCE_C1C36118_ED1C_44D6_B573_6C00C5923EAC
restart with 42;
This is essentially identical to Postgres' serial data type
If not, what would the long version of the SQL be?
create sequence foo_id_seq;
create table foo
(
id integer default foo_id_seq.nextval,
other_column varchar(20)
);
The big difference between this and a Postgres serial is that H2 does not know that the sequence "belongs" to the column. You need to drop it manually when the table is dropped.
foo_id_seq.nextval will actually be converted to (NEXT VALUE FOR PUBLIC.FOO_ID_SEQ) when the table is created (and it will be stored like that in information_schema.columns.

change the size of datatype in sql

I have created a table with column id as varchar2(20). Now I want to modify it and change size to 13 i.e column id varchar2(13). How to achieve it? Thanks in advance
p.s.: I don`t have any data in my table.
You may try this for Oracle:-
alter table tablename modify
(
column_name varchar2(13)
);
Also If you dont have any data in the table then you can also drop the table and then create the table with the columname as varchar2(13)
Just run this query for MySQL:
ALTER TABLE tablename CHANGE column `id` varchar2(13);
See here for more details. If you have additional constraints on this column, specify those as well.

Learn more about the nature of a constraint?

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