How to set all column from null to not null in table in oracle - sql

I have 1 table having 40 columns. Out of 40 columns only 5 columns are NOT NULL and rest of the columns are set as NULL. How can i set all NULL column to NOT NULL in one time or in TOAD. Is there any possibility to do this except manually set as NOT NULL.

You can use the Alter Table command to do so. This way:
ALTER TABLE table_name
MODIFY (column_1 column_type NOT NULL,
column_2 column_type NOT NULL,
...
column_n column_type);
This will accomplish the changes in all columns at once. Also if your table already has data with null values on those columns, you will have to define default values as well. Here is a sample:
ALTER TABLE table_name
MODIFY (column_1 varchar2(100) DEFAULT 'some default' NOT NULL,
column_2 varchar2(75) DEFAULT 'Some Value' NOT NULL);

Alter table will work .
alter table [nameOfYourTable] modify [nameOfColumn] [dataType] not null

You could loop through USER_TAB_COLUMNS ( ALL_TAB_COLUMNS with owner = 'schema') and ALTER using EXECUTE IMMEDIATE.
You could first update the table containing existing NULLs with appropriate value before running this.
SET SERVEROUTPUT ON
BEGIN
FOR q IN (
SELECT 'ALTER TABLE ' || table_name || ' MODIFY ' || column_name || ' NOT NULL' AS query
FROM user_tab_columns
WHERE table_name = 'YOURTABLE'
AND NULLABLE = 'Y'
)
LOOP
DBMS_OUTPUT.PUT_LINE(q.query);
EXECUTE IMMEDIATE q.query;
END LOOP;
END;

Related

Create Primary Constraint from table columns

I have a table named PRIMARY_CONSTRAINTS into the DB such as it has the table name, the constraint name and the column name of the primary constraints which I want to add.
It is an SQL oracle DB.
I have tried something like this but it does not run
ALTER TABLE TABLE_NAME
ADD CONSTRAINT CONSTRAINT_NAME PRIMARY KEY (COLUMN_NAME)
WHERE TABLE_NAME, CONSTRAINT_NAME, COLUMN_NAME IN
( SELECT TABLE_NAME,
CONSTRAINT_NAME,
COLUMN_NAME
FROM PRIMARY_CONSTRAINTS );
How can I reference it so I can alter the tables?
Thanks
You can use the following dynamic block to achieve the same:
BEGIN
FOR Q IN (
SELECT
'ALTER TABLE '
|| TABLE_NAME
|| ' ADD CONSTRAINT '
|| CONSTRAINT_NAME
|| ' PRIMARY KEY ( '
|| COLUMN_NAME
|| ')' QRY
FROM
PRIMARY_CONSTRAINTS
) LOOP
EXECUTE IMMEDIATE Q.QRY;
END LOOP;
END;
/
I have created the small demo for you --> db<>fiddle demo
UPDATED ACCORDING TO COMMENT
If there are two entries for a single table then You can use the following block:
BEGIN
FOR Q IN (
SELECT
'ALTER TABLE '
|| TABLE_NAME
|| ' ADD CONSTRAINT '
|| MAX(CONSTRAINT_NAME)
|| ' PRIMARY KEY ( '
|| LISTAGG(COLUMN_NAME,',') WITHIN GROUP (ORDER BY NULL)
|| ')' QRY
FROM
PRIMARY_CONSTRAINTS
GROUP BY TABLE_NAME
) LOOP
EXECUTE IMMEDIATE Q.QRY;
END LOOP;
END;
/
db<>fiddle demo updated
With regard to your comment about multiple columns on the constraints, we could setup the data in PRIMARY_CONSTRAINTS table in the same way it would be used in the Alter Table Statement.
For Example : Suppose we have to use the following statement to create a constraint Alter table add constraint Constraint_Name primary key columnA, columnB;
Then the entry for this particular constraint in your PRIMARY_CONSTRAINTS table will have value columnA , columnB for COLUMN_NAME.
Hope this helps.
Regards
Akash

Oracle Pl/SQL alter table columns

I have created a table2 with SQL query: create table2 as select * from table1
Create table as select ... doesn't retain all column properties from table1 in table2.
Is it possible to generate an SQL file to ALTER table2 column properties (DATA_LENGTH,DATA_PRECISION,DATA_SCALE,DEFAULT_LENGTH,DATA_DEFAULT) with all column properties from table1?
Thanks!
--drop table table2;
create table table1 (
x number(10, 2) default 10,
y varchar2(200) default 'NA'
);
create table table2 as select * from table1;
Let us try for data_default:
create table search_user_tab_columns as
select table_name, column_name, to_lob(DATA_DEFAULT) dd
from user_tab_columns
where table_name = 'TABLE1';
As data_default is a long data in user_tab_columns we have to create an intermediate
search_user_tab_columns (dummy) :-(
select 'ALTER TABLE "' || table_name || '" MODIFY "' || column_name || '" DEFAULT ' || Dd || ';'
from search_user_tab_columns
where table_name = 'TABLE1';
Note:
SELECT DBMS_METADATA.GET_DDL('TABLE','<TABLE_NAME>','<SCHEMA_NAME>') from dual;
This is better way of doing this if creating a table from an existing table . But sometimes you may try this for specific purposes.
If you only need to get the properties.
SELECT DBMS_METADATA.GET_DDL('TABLE','<TABLE_NAME>','<SCHEMA_NAME>') from dual;
try this:
you can see all properties
select column_name as "Name"
, nullable as "Null?"
, concat(concat(concat(data_type,'('),data_length),')') as "Type"
from user_tab_columns
where table_name = 'MY_TABLE';

Dynamic query with :new value

I have a table called PKCHANGES that has a few columns, one of which is the primary_key column. What I want is to create a trigger on other tables, and upon an insert I grab some values and post them to the PKCHANGES table. All is fine except for when I try and post the primary key values. I want that in the column primary_key I input the primary key values comma delimited. So if TableX has 3 primary keys, in PKCHANGES (primary_key column) I post value1,value2,value3.
So far I am only managing to get the below as a result and not the actual values
":new.pkCol1:new.pkCol2:new.pkCol3"
My pl/sql block is:
DECLARE
mySql varchar2(5000);
myTable varchar2(10) := 'TableX';
BEGIN
mySql := 'CREATE OR REPLACE TRIGGER ' || 't_1' || ' AFTER INSERT ON ' || myTable || '
FOR EACH ROW
DECLARE
currentPK varchar2(200); --Contains the current primary key value in the loop
result varchar2(200); --Contains the appended string of primary key values
--Cursor that contains primaryKeys for table
CURSOR pks IS
SELECT cols.column_name FROM all_constraints cons, all_cons_columns cols
WHERE cons.constraint_type = ''P''
AND cons.constraint_name = cols.constraint_name
AND cons.table_name = ' || '''' || myTable || '''' || ';
BEGIN
--Loop through primary keys, get the value from the trigger, and append the string.
for current_pk IN pks LOOP
BEGIN
currentPK := '':new.'' || current_pk.column_name;
result:= result || currentPK;
END;
END LOOP;'
||
' --Insert the appended values into the primary_key column
INSERT INTO PKCHANGES(primary_key)' ||
'VALUES (result);'
|| ' END;';
dbms_output.put_line(mySql);
EXECUTE IMMEDIATE mySql;
END;
Any idea?
The primary key of the TableX need not be queried on each insert. It is stable and if once changed, you will change the trigger as well.
This allows you to pop up the logic out of the trigger.
In the first step concatenate the PK. I'd prefer LISTAGG as it handels elegant the delimiter. You get something like :new.COL1||','||:new.COL2||','||:new.COL3
Also make sure the table name is in correct case (I asume upper case; otherwise you need to quote the name).
In the next step generate the trigger, that will basicaly contain only the INSERT
DECLARE
mySql varchar2(5000);
myTable varchar2(10) := 'TableX';
result varchar2(200); -- Contains the concatenated string of primary key column names with delimiters,
-- e.g. ":new.COL1||','||:new.COL2||','||:new.COL3"
BEGIN
SELECT listagg(':new.'||cols.column_name,'||'',''||') within group (order by position) into result
FROM all_constraints cons, all_cons_columns cols
WHERE cons.constraint_type = 'P'
AND cons.constraint_name = cols.constraint_name
AND cons.table_name = upper(myTable);
mySql := 'CREATE OR REPLACE TRIGGER ' || 't_1' || ' AFTER INSERT ON ' || myTable || '
FOR EACH ROW
BEGIN
--Insert the appended values into the primary_key column
INSERT INTO PKCHANGES(primary_key)' ||
'VALUES ('||result||');'
|| ' END;';
dbms_output.put_line(mySql);
EXECUTE IMMEDIATE mySql;
END;
/
Test
create table TableX
(col1 number,
col2 number,
col3 number,
col4 number);
alter table TableX add (primary key (col1, col2, col3));
insert into TableX values (1,2,3,4);
select * from PKCHANGES;
PRIMARY_KEY
-----------
1,2,3

Alter multiple tables' columns length

So, we just found out that 254 tables in our Oracle DBMS have one column named "Foo" with the wrong length- Number(10) instead of Number(3).
That foo column is a part from the PK of the tables.
Those tables have other tables with forigen keys to it.
What I did is:
backed-up the table with a temp table.
Disabled the forigen keys to the table.
Disabled the PK with the foo column.
Nulled the foo column for all the rows.
Restored all the above
But now we found out it's not just couple of tables but 254 tables.
Is there an easy way, (or at least easier than this) to alter the columns length?
P.S. I have DBA permissions.
There's an easier way to generate the scripts that you want, use the system tables user_tables and user_constraints to dynamically generate the DDL. The downside is that this requires downtime. Also note that I use the truncate command rather than delete, which should be faster.
Assuming a simple table that looks like:
create table a (
foo number(10)
, bar number(10)
, constraint pk_a primary key (foo)
, constraint fk_a foreign key ( bar ) references a(foo )
);
This unlovely looking query
select cmd
from (
select table_name
, 1 as stage -- Just used to order by at the end.
, 'create table ' || table_name || '_backup as select * from '
|| table_name || ';' || chr(10) as cmd
-- chr(10) is LF
from user_tab_columns -- View of all columns
where column_name = 'FOO'
and data_precision = 10 -- Length of the number
union all
select table_name
, 3 as stage
, 'truncate table ' || table_name || ';' || chr(10) -- Remove all data
|| 'alter table ' || table_name
|| ' modify ( foo number(3));' || chr(10)
|| 'insert into ' || table_name || ' select * from '
|| table_name || '_backup;' || chr(10)
|| 'drop table ' || table_name || '_backup;' as cmd
from user_tab_columns
where column_name = 'FOO'
and data_precision = 10
union all
select ut.table_name
, 2 as stage
-- Disable the constraint
, 'alter table ' || uc.table_name || ' disable constraint '
|| uc.constraint_name || ';' || chr(10) as cmd
from user_constraints uc -- All named constraints
join user_tab_columns ut
on uc.table_name = ut.table_name
where ut.column_name = 'FOO'
and ut.data_precision = 10
and constraint_type = 'R' -- Foreign Key constraints (see link)
union all
select ut.table_name
, 4 as stage
, 'alter table ' || uc.table_name || ' enable constraint '
|| uc.constraint_name || ';' || chr(10) as cmd
from user_constraints uc
join user_tab_columns ut
on uc.table_name = ut.table_name
where ut.column_name = 'FOO'
and ut.data_precision = 10
and constraint_type = 'R'
)
order by stage
Will produce the following:
create table A_backup as select * from A; -- Create your backup
alter table A disable constraint FK_A; -- Disable FKs
truncate table A; -- Remove all data in the table
alter table A modify ( foo number(3)); -- Reduce the size of the column
insert into A select * from A_backup; -- Replace all the data
drop table A_backup; -- Drop the backup
alter table A enable constraint FK_A; -- Re-enable FKs
Due to the column stage, this won't be done table by table but stage by stage so that all the constraints will be disabled at the same time, which will avoid problems. If you're scared (I would be) then remove the drop of the _backup tables from the query; this means that whatever goes wrong you're safe.
If you're running this in SQL*Plus you also want to include whenever sqlerror exit so that if there's a problem, for instance no more tablespace, you don't truncate things that you haven't backed-up. It might almost be worth running it stage by stage so that you know that everything has completed correctly.
I would suggest testing this on a different user with a few tables to ensure that it does everything you need.
What we have done is:
CREATE TABLE <table_name_backup> as SELECT * <table_name>;
DELETE <table_name>;
ALTER TABLE <table_name> MODIFY (Foo NUMBER(3));
INSERT INTO <table_name> SELECT * FROM <table_name_backup>;
DROP <table_name_backup>;
For all the tables.
Your solution works but is a lot of work and implies downtime.
Since physically a NUMBER(3) is exactly like a NUMBER(10) with a stronger constraint you could add CHECK constraints and get the same logical restriction without downtime:
LOOP
ALTER TABLE <table_name> ADD CONSTRAINT <table_foo_chk> CHECK (foo < 1000);
END LOOP;

Catching an exception while altering a table in Oracle

I'm trying to write a command in Oracle that will wither ADD or MODIFY a column depending on whether or not it already exists. Basically something like:
BEGIN
ALTER TABLE MY_TABLE ADD ( COL_NAME VARCHAR2(100 );
EXCEPTION WHEN OTHERS THEN
ALTER TABLE MY_TABLE MODIFY ( COL_NAME VARCHAR2(100) );
END;
However, Oracle complains about having the ALTER command inside of BEGIN. Is there a way to achieve this using a single SQL command in Oracle?
Thanks!
In order to put DDL in a PL/SQL block, you would need to use dynamic SQL.
Personally, I'd check whether the column exists first and then issue the DDL. Something like
DECLARE
l_cnt INTEGER;
BEGIN
SELECT COUNT(*)
INTO l_cnt
FROM dba_tab_cols
WHERE table_name = 'MY_TABLE'
AND owner = <<owner of table>>
AND column_name = 'COL_NAME';
IF( l_cnt = 0 )
THEN
EXECUTE IMMEDIATE 'ALTER TABLE my_table ADD( col_name VARCHAR2(100) )';
ELSE
EXECUTE IMMEDIATE 'ALTER TABLE my_table MODIFY( col_name VARCHAR2(100) )';
END IF;
END;
If you don't have access to DBA_TAB_COLS, you could also use ALL_TAB_COLS or USER_TAB_COLS depending on what schema the table resides in and what privileges you have on the table.
I found a solution based on this post.
DECLARE v_column_exists number := 0;
BEGIN
SELECT COUNT(*) INTO v_column_exists
FROM ALL_TAB_COLUMNS
WHERE TABLE_NAME = 'MY_TABLE'
AND COLUMN_NAME = 'COL_NAME';
IF (v_column_exists = 0) THEN
EXECUTE IMMEDIATE 'ALTER TABLE MY_TABLE ADD ( COL_NAME VARCHAR2(200) )';
ELSE
EXECUTE IMMEDIATE 'ALTER TABLE MY_TABLE MODIFY ( COL_NAME VARCHAR2(200) )';
END IF;
END;