|
|
|
Re: NOT NULL Constraint Oracle SQL [message #386562 is a reply to message #386541] |
Mon, 16 February 2009 02:37 |
m_golam_hossain
Messages: 89 Registered: August 2008 Location: Uttara, Dhaka, Bangladesh
|
Member |
|
|
Mr. Afzaal,
Please follow the following example:
ALTER TABLE data.transdocdtl
MODIFY(
docid VARCHAR2(14) NOT NULL,
doctype VARCHAR2(14) NOT NULL,
transdt DATE NOT NULL);
And study on ALTER TABLE ..... ADD CONSTRAINT command.
Mohd. Golam Hossain
Uttara, Dhaka, Bangladesh.
[Updated on: Mon, 16 February 2009 02:41] Report message to a moderator
|
|
|
Re: NOT NULL Constraint Oracle SQL [message #386574 is a reply to message #386562] |
Mon, 16 February 2009 03:02 |
trivendra
Messages: 211 Registered: October 2007 Location: Phoenix
|
Senior Member |
|
|
If you really want to do this, try this
CREATE TABLE temp_drop ( a NUMBER NOT NULL);
SELECT constraint_name
FROM user_constraints
WHERE table_name = 'TEMP_DROP';
CONSTRAINT_NAME
------------------------------
SYS_C00158927
1 row selected.
ALTER TABLE temp_drop RENAME CONSTRAINT SYS_C00158927 TO new_name;
SELECT constraint_name
FROM user_constraints
WHERE table_name = 'TEMP_DROP';
CONSTRAINT_NAME
------------------------------
NEW_NAME
1 row selected.
Thanks
Trivendra
|
|
|
|
Re: NOT NULL Constraint Oracle SQL [message #386582 is a reply to message #386580] |
Mon, 16 February 2009 03:17 |
trivendra
Messages: 211 Registered: October 2007 Location: Phoenix
|
Senior Member |
|
|
By two ways
1. Create a alter script for ENABLE and DISABLE of constraint.
2. Create a block, select constraint name from USER_CONSTRAINTS view and use dynamic sql to ENABLE/DISABLE constraint.
Thanks
Trivendra
|
|
|
Re: NOT NULL Constraint Oracle SQL [message #386601 is a reply to message #386541] |
Mon, 16 February 2009 03:56 |
cookiemonster
Messages: 13961 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Alternatively you could dispense with adding and removing not null constraints and use a staging table instead.
If you do insist on loading your data this way you really don't need to know the constraint names, just the column names will do.
Read up on the alter table command.
|
|
|
|
|
|
Re: NOT NULL Constraint Oracle SQL [message #386616 is a reply to message #386541] |
Mon, 16 February 2009 04:31 |
sukhijank
Messages: 5 Registered: February 2009
|
Junior Member |
|
|
You can use:
CREATE TABLE TEST123 (A NUMBER NOT NULL, B NUMBER, C NUMBER);
ALTER TABLE TEST123 ADD CONSTRAINT B_NOT_NULL CHECK ("B" IS NOT NULL);
ALTER TABLE TEST123 DISABLE CONSTRAINT B_NOT_NULL;
|
|
|
Re: NOT NULL Constraint Oracle SQL [message #386619 is a reply to message #386616] |
Mon, 16 February 2009 04:37 |
trivendra
Messages: 211 Registered: October 2007 Location: Phoenix
|
Senior Member |
|
|
I dont know why you want to change the name of NOT NULL constraint. ENABLE and DISABLE can be eaisly done by given two methods in my above post. Just to clarify you ..
SQL> CREATE TABLE temp1 (a NUMBER NOT NULL, b NUMBER NOT NULL)
Table created.
SQL> CREATE TABLE temp2 (a NUMBER NOT NULL, b NUMBER NOT NULL)
Table created.
SQL> CREATE TABLE temp3 (a NUMBER NOT NULL, b NUMBER NOT NULL)
Table created.
SQL> -- Case 1
SQL> -- Create Manual Script For DISABLE
SQL> SELECT 'ALTER TABLE '
|| table_name
|| ' DISABLE CONSTRAINT '
|| constraint_name
|| ';'
FROM user_constraints
WHERE table_name IN ('TEMP1', 'TEMP2', 'TEMP3')
'ALTERTABLE'||TABLE_NAME||'DISABLECONSTRAINT'||CONSTRAINT_NAME||';'
--------------------------------------------------------------------------------
ALTER TABLE TEMP1 DISABLE CONSTRAINT SYS_C00158946;
ALTER TABLE TEMP1 DISABLE CONSTRAINT SYS_C00158947;
ALTER TABLE TEMP2 DISABLE CONSTRAINT SYS_C00158948;
ALTER TABLE TEMP2 DISABLE CONSTRAINT SYS_C00158949;
ALTER TABLE TEMP3 DISABLE CONSTRAINT SYS_C00158950;
ALTER TABLE TEMP3 DISABLE CONSTRAINT SYS_C00158951;
6 rows selected.
SQL> -- Create Manual Script For ENABLE
SQL> SELECT 'ALTER TABLE '
|| table_name
|| ' ENABLE CONSTRAINT '
|| constraint_name
|| ';'
FROM user_constraints
WHERE table_name IN ('TEMP1', 'TEMP2', 'TEMP3')
'ALTERTABLE'||TABLE_NAME||'ENABLECONSTRAINT'||CONSTRAINT_NAME||';'
--------------------------------------------------------------------------------
ALTER TABLE TEMP1 ENABLE CONSTRAINT SYS_C00158946;
ALTER TABLE TEMP1 ENABLE CONSTRAINT SYS_C00158947;
ALTER TABLE TEMP2 ENABLE CONSTRAINT SYS_C00158948;
ALTER TABLE TEMP2 ENABLE CONSTRAINT SYS_C00158949;
ALTER TABLE TEMP3 ENABLE CONSTRAINT SYS_C00158950;
ALTER TABLE TEMP3 ENABLE CONSTRAINT SYS_C00158951;
6 rows selected.
SQL> /* You can this scripts to ENABLE or DISABLE your table constraints*/
SQL> -- case 2
SQL> -------------------------------------------------------------------------------
SQL> -- BY BLOCK to DISABLE constraints
SQL> -------------------------------------------------------------------------------
SQL> DECLARE
l_fun VARCHAR2 (10) := 'DISABLE';
BEGIN
FOR i IN (SELECT table_name, constraint_name
FROM user_constraints
WHERE table_name IN ('TEMP1', 'TEMP2', 'TEMP3'))
LOOP
EXECUTE IMMEDIATE 'ALTER TABLE '
|| i.table_name
|| ' '
|| l_fun
|| ' '
|| 'CONSTRAINT '
|| i.constraint_name;
END LOOP;
END;
PL/SQL procedure successfully completed.
SQL> -- Check for DISABLE constarints
SQL> SELECT table_name, constraint_name, status
FROM user_constraints
WHERE table_name IN ('TEMP1', 'TEMP2', 'TEMP3')
TABLE_NAME CONSTRAINT_NAME STATUS
------------------------------ ------------------------------ --------
TEMP1 SYS_C00158946 DISABLED
TEMP1 SYS_C00158947 DISABLED
TEMP2 SYS_C00158948 DISABLED
TEMP2 SYS_C00158949 DISABLED
TEMP3 SYS_C00158950 DISABLED
TEMP3 SYS_C00158951 DISABLED
6 rows selected.
SQL> -------------------------------------------------------------------------------
SQL> -- BY BLOCK to DISABLE constraints
SQL> -------------------------------------------------------------------------------
SQL> DECLARE
l_fun VARCHAR2 (10) := 'ENABLE';
BEGIN
FOR i IN (SELECT table_name, constraint_name
FROM user_constraints
WHERE table_name IN ('TEMP1', 'TEMP2', 'TEMP3'))
LOOP
EXECUTE IMMEDIATE 'ALTER TABLE '
|| i.table_name
|| ' '
|| l_fun
|| ' '
|| 'CONSTRAINT '
|| i.constraint_name;
END LOOP;
END;
PL/SQL procedure successfully completed.
SQL> -- Check for DISABLE constarints
SQL> SELECT table_name, constraint_name, status
FROM user_constraints
WHERE table_name IN ('TEMP1', 'TEMP2', 'TEMP3')
TABLE_NAME CONSTRAINT_NAME STATUS
------------------------------ ------------------------------ --------
TEMP1 SYS_C00158946 ENABLED
TEMP1 SYS_C00158947 ENABLED
TEMP2 SYS_C00158948 ENABLED
TEMP2 SYS_C00158949 ENABLED
TEMP3 SYS_C00158950 ENABLED
TEMP3 SYS_C00158951 ENABLED
6 rows selected.
Thanks
Trivendra
|
|
|
|
Re: NOT NULL Constraint Oracle SQL [message #386621 is a reply to message #386603] |
Mon, 16 February 2009 04:38 |
JRowbottom
Messages: 5933 Registered: June 2006 Location: Sunny North Yorkshire, ho...
|
Senior Member |
|
|
Oracle only auto-names Not null constraints if you don't bother to name them yourself.
If you use syntax like this:create table test_102 (col_1 number constraint test_102_nn01 not null);
Then you can give your constraints names you control, and can then enable and disable them to your heart's content.
|
|
|
Re: NOT NULL Constraint Oracle SQL [message #386623 is a reply to message #386616] |
Mon, 16 February 2009 04:39 |
ThomasG
Messages: 3212 Registered: April 2005 Location: Heilbronn, Germany
|
Senior Member |
|
|
Maybe you should also tell us what you are actually trying to ACHIEVE with all this.
Two things that might be better than all this creating/dropping enabling/disabling, depending on what you are trying to do, might be :
1) Using a staging table where you clean up the data before you do the insert into the final table. (as already suggested)
2) Use a Deferred Constraint, which is checked after the transaction is finished with a commit.
|
|
|
|
|
Re: NOT NULL Constraint Oracle SQL [message #386692 is a reply to message #386613] |
Mon, 16 February 2009 12:34 |
joy_division
Messages: 4963 Registered: February 2005 Location: East Coast USA
|
Senior Member |
|
|
helloafzaal wrote on Mon, 16 February 2009 05:27 |
SQL> create table abc( a number);
Table created.
SQL> alter table abc add constraint abc_a_nn not null;
alter table abc add constraint abc_a_nn not null
ERROR at line 1:
ORA-00904: : invalid identifier
|
I think you probably what you were looking for with the responses after this question, but I just wanted to point out that this error is there because if you look at this ALTER command carefully, what would adding a NOT NULL constraint mean if there is no column associated with it?
|
|
|
|
Re: NOT NULL Constraint Oracle SQL [message #386795 is a reply to message #386773] |
Tue, 17 February 2009 00:49 |
|
Michel Cadot
Messages: 68722 Registered: March 2007 Location: Saint-Maur, France, https...
|
Senior Member Account Moderator |
|
|
@m_golam_hossain
This does not add anything to the topic and the provided documentation, just spoon feed OP.
As already said to you:
Don't put solution only hint or clue as requested in OraFAQ Forum Guide, "Responding to Posts" section:
Quote: | When responding to questions, if it is obviously a student with a homework assignment or someone else just learning, especially in the homework and newbies forums, it is usual to provide hints or clues, perhaps links to relevant portions of the documentation, or a similar example, to point them in the right direction so that they will research and experiment on their own and learn, and not provide complete solutions to problems. In cases where someone has a real-life complex work problem, or at least it may seem complex to them, it may be best to provide a complete demo and explanation.
|
Regards
Michel
|
|
|
Re: NOT NULL Constraint Oracle SQL [message #386805 is a reply to message #386795] |
Tue, 17 February 2009 01:05 |
Frank
Messages: 7901 Registered: March 2000
|
Senior Member |
|
|
Since this whole thread is full of solutions, I think it is not fair to blame m_golam_hossain for giving a "full solution".
However, his post does indeed not really add to the thread, since his suggestion was already in there.
|
|
|