date Not null , error ora_01758 - oracle

why I am getting this error ?
In the table DDL I only have 2 columns , id (number) and name (varchar)
ALTER TABLE mytable ADD SUSPEND date NOT NULL
ORA-01758: table must be empty to add mandatory (NOT NULL) column
ORA-06512: at line 7

ORA-01758: table must be empty to add mandatory (NOT NULL) column ORA-06512: at line 7
And is your table empty? I think not.
There's probably a way around this involving adding the column as nullable, then populating every row with a non-NULL value, the altering the column to be not null.
Alternatively, since the problem is that these current rows will be given NULL as a default value, and the column is not allowed to be NULL, you can also get around it with a default value. From the Oracle docs:
However, a column with a NOT NULL constraint can be added to an existing table if you give a default value; otherwise, an exception is thrown when the ALTER TABLE statement is executed.
Here is a fiddle, how you could do it

Would a date in the future be acceptable as a temporary default? If so, this would work:
ALTER TABLE MYTABLE ADD (SUSPEND_DATE DATE DEFAULT(TO_DATE('21000101', 'YYYYMMDD'))
CONSTRAINT SUSPEND_DATE_NOT_NULL NOT NULL);

If table already contain the records then table won't allowes to add "Not null" column.
If you need same then set default value for the column or truncate the table then try.

Related

Modify column value Oracle

Hello I have made a Trigger it compiles
CREATE OR REPLACE TRIGGER livraisonfinie
BEFORE UPDATE ON Expedition
FOR EACH ROW
DECLARE
BEGIN
IF :NEW.date_livraison <> TO_DATE('3000/01/01 00:00:00', 'yyyy/mm/dd hh24:mi:ss')
THEN
INSERT INTO Commande (etat) VALUES ('livree');
DELETE FROM cmdalivrer CMD WHERE :NEW.numero_commande=CMD.id_cmd WHERE :NEW.numero_commande= Commande.numero_commande;;
END IF ;
END;
/
When I try to update a value of expedition table I get this error
ORA-01400: cannot insert NULL into ("HAMZA"."COMMANDE"."NUMERO_COMMANDE")
ORA-06512: at "HAMZA.LIVRAISONFINIE", line 10
ORA-04088: error during execution of trigger 'HAMZA.LIVRAISONFINIE'
And I have used this update request but not sure if I have to use INSERT INTO or UPDATE and how corretly even after reading many manuals.
Here's the table
Expedition(Id_Expedition ,#id_chauffeur,#Immatriculation, #Id_Itineraire,Date_Deb_Expedition , Date_Livraison)
Commande (numero_commande,Date_commande,adresse_livraison,id_part,ville_livraison,code_postal_livraison,etat,id_expedition)
EDIT: I solved it by adding "where :NEW.numero_commande= Commande.numero_commande;" to my trigger.
Thanks
There is NOT NULL constraint on column NUMERO_COMMANDE in table COMMANDE.
More on this constraint you can find here:
http://www.w3schools.com/sql/sql_notnull.asp
By default, a table column can hold NULL values.
SQL NOT NULL Constraint
The NOT NULL constraint enforces a column to
NOT accept NULL values.
The NOT NULL constraint enforces a field to always contain a value.
This means that you cannot insert a new record, or update a record
without adding a value to this field.
Either provide a value for this field in your trigger:
INSERT INTO Commande (etat,NUMERO_COMMANDE)
VALUES ('livree', some-value-for-numero-commande-column);
or remove NOT NULL constraint on this column from the table.

Change from Number to Generated in PLSQL

I am trying to alter a table where the original datatype of this column is Number to a generated column, but I get an error of "ORA-00905 Missing Keyword"
Alter Table MyTable
Modify Column FlagColumn NUMERIC (38,0) GENERATED ALWAYS AS (CASE WHEN ValueColumn IS NULL THEN 1 ELSE 0 END) VIRTUAL;
Is my syntax correct?
Do I have any other options besides dropping and recreating the table?
The Oracle documentation pretty clearly doesn't support the syntax you're attempting. The obvious solution is to drop the column, then replace it:
ALTER TABLE mytable
DROP COLUMN flagcolumn;
ALTER TABLE mytable
ADD numeric GENERATED ALWAYS AS (CASE WHEN valuecolumn IS NULL THEN 1 ELSE 0 END) VIRTUAL;
There's really no reason not to do this, since you're getting rid of the column's original data in any case.

Alter table - change the default value of a column

I have a column with REAL data type, and I'm trying to write a statement to change the default value to 4. However, when I use the select * statement to double check, the column is still empty.
ALTER TABLE recipes
MODIFY NumberOfServings DEFAULT '4';
The DEFAULT value is used at INSERT time. It gives a default value to be inserted when you do not provide a value for the column in the INSERT statement.
You may want to update the table, so that all NULL values are replaced by a given value:
UPDATE recipes SET NumberOfServings=4 WHERE NumberOfServings IS NULL;
You can also specify a value to use when the column is NULL at QUERY time.
This can be done by using the NVL function:
SELECT NVL(NumberOfServings,4) FROM recipes;
http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions105.htm
The default value applies only when you insert new records into the table. It does not update any existing values in the table. Why do you enclose a numeric value in quotes?

Inserting an empty row

This is so simple it has probably already been asked, but I couldn't find it (if that's the case I'm sorry for asking).
I would like to insert an empty row on a table so I can pick up its ID (primary key, generated by an insert trigger) through an ExecuteScalar. Data is added to it at a later time in my code.
My question is this: is there a specific insert syntax to create an empty record? or must I go with the regular insert syntax such as "INSERT INTO table (list all the columns) values (null for every column)"?
Thanks for the answer.
UPDATE: In Oracle, ExecuteScalar on INSERT only returns 0. The final answer is a combination of what was posted below. First you need to declare a parameter, and pick up it up with RETURNING.
INSERT INTO TABLENAME (ID) VALUES (DEFAULT) RETURNING ID INTO :parameterName
Check this out link for more info.
You would not have to specify every single column, but you may not be able to create an "empty" record. Check for NOT NULL constraints on the table. If none (not including the Primary Key constraint), then you would only need to supply one column. Like this:
insert into my_table ( some_column )
values ( null );
Do you know about the RETURNING clause? You can return that PK back to your calling application when you do the INSERT.
insert into my_table ( some_column )
values ( 'blah' )
returning my_table_id into <your_variable>;
I would question the approach though. Why create an empty row? That would/could mean there are no constraints on that table, a bad thing if you want good, clean, data.
Basically, in order to insert a row where values for all columns are NULL except primary
key column's value you could execute a simple insert statement:
insert into your_table(PK_col_name)
values(1); -- 1 for instance or null
The before insert trigger, which is responsible for populating primary key column will
override the value in the values clause of the insert statement leaving you with an
empty record except PK value.

UPDATE in procedure ORA-00001

I have w problem with UPDATE in procedure. Procedure compiling and I see(DBMS...) results example 100records and error
ORA-00001: unique constraint violated (CUSTOMER_INFO_COMM_METHOD_UX)
My update:
UPDATE customer_info_comm_method_tab SET Value=wynikOK WHERE
customer_id=cus_rec.customer_id AND method_id='E_MAIL' AND Value = p_stringWyn;
wynikOK - actual new Value
cus_rec.customer_id - actual customer_id from cursor
p_stringWyn - old Value in table
key is founded on three attributes that I use (CUSTOMER_ID, VALUE, METHOD_ID)
Of course I can't remove index CUSTOMER_INFO_COMM_METHOD_UX because Its not my database
If I commented update procedure compile 100% without error but I need to do this update
It means that your new value (wynikOK) is causing the violation. The combination of customer_id , your new value(wynikOK) and method id is already existing in another row of your table. But this has to be unique...
If this combination of values(customer,value,method) is not gonna be unique , then remove the unique constraint in the table..
Else value has to be unique. Try appending some strings for your new value so that it will be unique. Say if your value is 1234, try appending the date to this value 1234_23112012 so that this will be unique always.

Resources