I am trying to alter a column in my ingres DB to expand the size of the column.
The query i'm running is
ALTER TABLE test_table ALTER COLUMN address varchar(100) NOT NULL
Which gives error
Error: ALTER TABLE: invalid change of attributes on an ALTER COLUMN
SQLState: 42000 ErrorCode: 3859
Anyone any idea why I'm getting this error? I've checked the syntax for altering tables.
Probably you have NULL in data. Update your table first (set to empty string or any value you want):
LiveDemo
UPDATE test_table
SET address = '' -- or another value indicating missing addres like 'none'
WHERE address IS NULL;
And then try:
ALTER TABLE test_table ALTER COLUMN address varchar(100) NOT NULL;
EDIT:
If you don't want to enforce NOT NULL use just:
LiveDemo2
ALTER TABLE test_table ALTER COLUMN address varchar(100);
Related
I tried to set null to columns like following.
ALTER TABLE myschema.table ALTER COLUMN (test_id,type) SET NOT NULL;
But it returned syntax error like Syntax error at or near Line 3, Position 47
Are there any proper way to achieve this ?
If someone has opinion please let me know.
Thanks
You can't provide a list of column in parentheses, you need to use multiple ALTER COLUMN options separated by a comma:
ALTER TABLE the_table
ALTER COLUMN test_id set not null,
ALTER COLUMN type SET NOT NULL;
Try doing it separately for both the columns:
ALTER TABLE myschema.table ALTER COLUMN test_id SET NOT NULL;
ALTER TABLE myschema.table ALTER COLUMN type SET NOT NULL;
I am trying to change the column size from 100 to 150 varchar data type using following query:
alter table data_warehouse.tbl_abc
alter column first_nm varchar(150) null;
Getting the following error:
SQL Error [42601]: ERROR: syntax error at or near "varchar"
Position: 77
The syntax is a bit different, so try this:
ALTER TABLE data_warehouse.tbl_abc
ALTER COLUMN first_nm type varchar(120);
The error in your syntax is that you missed a TYPE keyword:
ALTER TABLE data_warehouse.tbl_abc
ALTER COLUMN first_nm TYPE varchar(150);
and if you have a NOT NULL constraint you want to remove, add a new ALTER COLUMN inside the same ALTER TABLE statement:
ALTER TABLE data_warehouse.tbl_abc
ALTER COLUMN first_nm TYPE varchar(150),
ALTER COLUMN first_nm DROP NOT NULL;
for reference look here: https://www.postgresql.org/docs/current/sql-altertable.html
Edit: as in the comment, if you have a view which involves the same column, drop it and re-create it under transaction:
BEGIN TRANSACTION;
DROP VIEW [...];
ALTER TABLE [...];
CREATE VIEW [...];
COMMIT;
Be aware that to alter a table, you must acquire an exclusive lock on it, so during the whole process, all the queries over the same table and on the views of the table are locked, also if they don't read from the altered column (because the whole table is locked) - use with caution in production environment
In my database table, I've column entryId with data type varchar and size=10. Now I want to change size to 100, when I hit following query -
ALTER TABLE ft ALTER COLUMN entryId TYPE varchar(100);
it results into following error -
Incorrect syntax near 'varchar'.
My query syntax looks correct. Help me to understand what I did wrong.
No "Type" is need here:
ALTER TABLE ft ALTER COLUMN entryId varchar(100);
I SQL Server, the keyword type is not needed
ALTER TABLE ft ALTER COLUMN entryId varchar(100);
Remove 'Type' from your query :
ALTER TABLE ft ALTER COLUMN entryId VARCHAR(100);
no need for 'TYPE' keyword
ALTER TABLE ft ALTER COLUMN entryId varchar(100);
I have a table that I want to add new not null varchar(255) column
My query is:
alter table poll_management.DASHLETS add column name varchar(255) not null;
update poll_management.DASHLETS as dashlet set name = report.name
from poll_management.REPORTS as report
WHERE dashlet.id = report.reportdashletid
But I have an error:
ERROR: column "name" contains null values
********** Error **********
ERROR: column "name" contains null values
SQL state: 23502
To avoid your error, two solutions come at once:
BEGIN;
alter table poll_management.DASHLETS add column name varchar(255);
update poll_management.DASHLETS as dashlet set name = report.name
from poll_management.REPORTS as report;
--mind I removed where, cos you need to update ALL rows to have some avlue
alter table poll_management.DASHLETS alter column "name" set not null;
END;
and the other:
alter table poll_management.DASHLETS add column name varchar(255) NOT NULL default 'not set';
i found that my query must be change to :
alter table poll_management.DASHLETS add column name varchar(255) not null DEFAULT 'value';
I want to alter a table column to be nullable. I have used:
ALTER TABLE Merchant_Pending_Functions Modify NumberOfLocations NULL
This gives an error at Modify. What is the correct syntax?
Assuming SQL Server (based on your previous questions):
ALTER TABLE Merchant_Pending_Functions ALTER COLUMN NumberOfLocations INT NULL
Replace INT with your actual datatype.
If this was MySQL syntax, the type would have been missing, as some other responses point out.
Correct MySQL syntax would have been:
ALTER TABLE Merchant_Pending_Functions MODIFY NumberOfLocations INT NULL
Posting here for clarity to MySQL users.
In PostgresQL it is:
ALTER TABLE tableName ALTER COLUMN columnName DROP NOT NULL;
for Oracle Database 10g users:
alter table mytable modify(mycolumn null);
You get "ORA-01735: invalid ALTER TABLE option" when you try otherwise
ALTER TABLE mytable ALTER COLUMN mycolumn DROP NOT NULL;
Although I don't know what RDBMS you are using, you probably need to give the whole column specification, not just say that you now want it to be nullable. For example, if it's currently INT NOT NULL, you should issue ALTER TABLE Merchant_Pending_Functions Modify NumberOfLocations INT.
As others have observed, the precise syntax for the command varies across different flavours of DBMS. The syntax you use works in Oracle:
SQL> desc MACAddresses
Name Null? Type
----------------------------------------- -------- ----------------------------
COMPUTER NUMBER
MACADDRESS VARCHAR2(12)
CORRECTED_MACADDRESS NOT NULL VARCHAR2(17)
SQL> alter table MACAddresses
2 modify corrected_MACAddress null
3 /
Table altered.
SQL> desc MACAddresses
Name Null? Type
----------------------------------------- -------- ----------------------------
COMPUTER NUMBER
MACADDRESS VARCHAR2(12)
CORRECTED_MACADDRESS VARCHAR2(17)
SQL>
For SQL Server or TSQL
ALTER TABLE Complaint.HelplineReturn ALTER COLUMN IsDisposed BIT NULL
This depends on what SQL Engine you are using, in Sybase your command works fine:
ALTER TABLE Merchant_Pending_Functions
Modify NumberOfLocations NULL;
For HSQLDB:
ALTER TABLE tableName ALTER COLUMN columnName SET NULL;
ALTER TABLE Merchant_Pending_Functions MODIFY COLUMN `NumberOfLocations` INT null;
This will work for you.
If you want to change a not null column to allow null, no need to include not null clause. Because default columns get not null.
ALTER TABLE Merchant_Pending_Functions MODIFY COLUMN `NumberOfLocations` INT;
Oracle
ALTER TABLE Merchant_Pending_Functions MODIFY([column] NOT NULL);
SQLite
The ALTER TABLE command is a bit special. There is no possibility to modify a column. You have to create a new column, migrate the data, and then drop the column:
-- 1. First rename
ALTER TABLE
Merchant_Pending_Functions
RENAME COLUMN
NumberOfLocations
TO
NumberOfLocations_old
-- 2. Create new column
ALTER TABLE
Merchant_Pending_Functions
ADD COLUMN
NumberOfLocations INT NULL
-- 3. Migrate data - you need to write code for that
-- 4. Drop the old column
ALTER TABLE
Merchant_Pending_Functions
DROP COLUMN
NumberOfLocations_old
Make sure you add the data_type of the column to modify.
ALTER TABLE TABLE_NAME MODIFY COLUMN_NAME DATA_TYPE NULL;