How to add notnull constraint to a column of an existing table in which no value is yet inserted i.e. only table is created and now i want to alter the table
i am writing the following query
ALTER TABLE TABLENAME MODIFY COLUMNAME DATATYPE NOT NULL;
but oracle is throwing error "invalid alter table option"
In an alter table you only have to specify what changes
ALTER TABLE TABLENAME MODIFY COLUMNAME NOT NULL;
As you don't change the datatype, you don't have to specify it again.
Related
The id column in the student table is an auto incrementing one.I wanted to make that to non - autoincrementing. May i know, how can i modify the below query to work as such?
DB::statement("ALTER TABLE student SET AUTO_INCREMENT=FALSE;");
the above code shows the below error.
Illuminate\Database\QueryException
SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "AUTO_INCREMENT"
LINE 1: ALTER TABLE student SET AUTO_INCREMENT=FALSE;
^ (SQL: ALTER TABLE student SET AUTO_INCREMENT=FALSE;)
The correct syntax in PostgreSQL would be:
ALTER TABLE student ALTER COLUMN id DROP DEFAULT;
Where id is the serial column.
You might also want to drop the not null constraint :
ALTER TABLE student ALTER COLUMN id DROP NOT NULL;
This is an example of why posting table definitions (ddl) and Postgres version can be critical. #Zakaria is correct if the Postgres version is prior to version 10, or if version 10 or later and is still defined as serial/bigserial. However the preferred definition for version 10 and later is generated always as identity. If defined as identity you need:
alter table student alter column id drop identity.
I would not drop a not null constraint, and if it is the PK it is moot point as it will automatically be not null.
I created a sequence EMP_SEQ then added a column to a table with EMP_SEQ as column as below:
ALTER TABLE EMPLOYEE ADD EMP_SEQ;
UPDATE EMPLOYEE SET EMP_SEQ = EMP_SEQ.NEXTVAL;
ALTER TABLE EMPLOYEE MODIFY(EMP_SEQ DEFAULT ON NULL EMP_SEQ.NEXTVAL);
Later I had to rename the sequence to EMP_SEQ_AUTO_INC and column name of EMPLOYEE table to the same as below:
ALTER TABLE EMPLOYEE COLUMN EMP_SEQ TO EMP_SEQ_AUTO_INC;
RENAME EMP_SEQ TO EMP_SEQ_AUTO_INC;
And now the ORA 02289 occurs when I try to insert data into the table with column_name EMP_SEQ_AUTO_INC.
Should I complete the below steps with new column name to overcome this error:
UPDATE EMPLOYEE SET EMP_SEQ_AUTO_INC = EMP_SEQ_AUTO_INC.NEXTVAL;
ALTER TABLE EMPLOYEE MODIFY(EMP_SEQ_AUTO_INC DEFAULT ON NULL EMP_SEQ_AUTO_INC.NEXTVAL);
That's not Oracle 11g, it doesn't support syntax you used. I suggest you fix the database version tag.
Anyway: with all modifications you made, you'll just need to re-run
ALTER TABLE employee MODIFY
(emp_seq_auto_inc DEFAULT ON NULL emp_seq_auto_inc.NEXTVAL);
and that's all. No need to update rows once again, you won't get anything new because values already exist, and sequence was just renamed (so it continues with its values, i.e. you won't get duplicates which might happen if you dropped it and created a new one).
This is my exact query which is erroring out
alter table INDIL_MCAR drop constraint ABOB.INDI_MCAR_PK;
I m trying to remove the unique key constring from the table. It gives me the following error.
ORA-01735: invalid ALTER TABLE option
You can't prefix the constraint name... the table name yes, but not the constraint name. Remove the ABOB.:
alter table INDIL_MCAR drop constraint INDI_MCAR_PK;
How can I change DATA TYPE of a column from number to varchar2 without deleting the table data?
You can't.
You can, however, create a new column with the new data type, migrate the data, drop the old column, and rename the new column. Something like
ALTER TABLE table_name
ADD( new_column_name varchar2(10) );
UPDATE table_name
SET new_column_name = to_char(old_column_name, <<some format>>);
ALTER TABLE table_name
DROP COLUMN old_column_name;
ALTER TABLE table_name
RENAME COLUMN new_column_name TO old_coulumn_name;
If you have code that depends on the position of the column in the table (which you really shouldn't have), you could rename the table and create a view on the table with the original name of the table that exposes the columns in the order your code expects until you can fix that buggy code.
You have to first deal with the existing rows before you modify the column DATA TYPE.
You could do the following steps:
Add the new column with a new name.
Update the new column from old column.
Drop the old column.
Rename the new column with the old column name.
For example,
alter table t add (col_new varchar2(50));
update t set col_new = to_char(col_old);
alter table t drop column col_old cascade constraints;
alter table t rename column col_new to col_old;
Make sure you re-create any required indexes which you had.
You could also try the CTAS approach, i.e. create table as select. But, the above is safe and preferrable.
The most efficient way is probably to do a CREATE TABLE ... AS SELECT
(CTAS)
alter table table_name modify (column_name VARCHAR2(255));
Since we can't change data type of a column with values, the approach that I was followed as below,
Say the column name you want to change type is 'A' and this can be achieved with SQL developer.
First sort table data by other column (ex: datetime).
Next copy the values of column 'A' and paste to excel file.
Delete values of the column 'A' an commit.
Change the data type and commit.
Again sort table data by previously used column (ex: datetime).
Then paste copied data from excel and commit.
ALTER TABLE testTable ADD column1 NUMBER(1) DEFAULT 0 NOT NULL AFTER column2;
Why can't I use mySql syntax in Oracle too? The above command works in MySql. Can you give me an equivalent that works?
Error report:
SQL Error: ORA-01735: invalid ALTER TABLE option
01735. 00000 - "invalid ALTER TABLE option"
I am asking if there is any way to use after clause in Oracle command that I provided?
Because SQL is a relational algebra. It doesn't care one bit about "where" columns are located within a table, only that they exist.
To get it to work in Oracle, just get rid of the after clause. The Oracle documentation for alter table is here but it boils down to:
alter table testTable
add ( column1 number(1) default 0 not null )
There is no after clause for the alter table command.
Oracle does not support adding columns in the middle of a table, only adding them to the end. Your database design and app functionality should not depend on the order of columns in the database schema. You can always specify an order in your select statement, after all.
However if for some reason you simply must have a new column in the middle of your table there is a work around.
CREATE TABLE tab1New AS SELECT 0 AS col1, col1 AS col2 FROM tab1;
DROP TABLE tab1 PURGE;
RENAME tan1New to tab1;
Where the SELECT 0 AS col1 is your new column and then you specify other columns as needed from your original table. Put the SELECT 0 AS col1 at the appropriate place in the order you want.
Afterwards you may want to run an alter table statement on the column to make sure it's the data type you desire.
Try this :
ALTER TABLE testTable ADD column1 NUMBER(1) DEFAULT 0 NOT NULL