postgresql: whats wrong in my sql statement of altering a column in a table - sql

I am trying to alter a tables column with a default value of -11 and it can be null
ALTER TABLE devicedata ALTER COLUMN "weather" smallint NULL DEFAULT -11 ;
it says error

In Postgres the syntax for this is:
alter table device_data alter column wheather type smallint;
Then you can change the default:
alter table device_data alter column wheather set default 10;
The column keyword is optional.

Related

Alter a table column into smallint column in postgres

I have a postgres table called update_profile with a column that is a table:
And I want to alter this column to a smallint containing the value of update_type_id.
Initially I tried:
ALTER TABLE update_profile ALTER COLUMN update_type TYPE SMALLINT USING update_type.update_type_id;
But I had the following error: missing FROM-clause entry for table "update_type"
Then I tried:
ALTER TABLE update_profile AS u ALTER COLUMN u.update_type TYPE SMALLINT USING u.update_type.update_type_id;
Which is not allowed.
Note: update_type_id is also a smallint
Is there a way to do this operation?
Don't repeat the table name when you reference the other column. You can't assign any alias for the table (or column) either.
ALTER TABLE update_profile
ALTER COLUMN update_type TYPE SMALLINT
USING update_type_id;
This is what I ended up doing:
ALTER TABLE update_profile ADD COLUMN update_type_id SMALLINT;
UPDATE update_profile up SET update_type_id =
(
SELECT ut.update_type_id
FROM n3rgy_update_type ut
WHERE ut = up.update_type
)
WHERE up.update_type IS NOT NULL;
ALTER TABLE update_profile DROP COLUMN update_type;
Because I didn't find a way to alter the column update_type, I created a new column called update_type_id, passed the values of update_profile.update_type.update_type_id, and then dropped update_type.
So now I have the values of update_profile.update_type.update_type_id in update_profile.update_type_id

Cannot Alter Column in Squirell SQL Client

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);

SQL Alter table default error

I am trying to modify an Integer field on existing table from nullable to non-nullable and adding default value to it.
ALTER TABLE dbo.current_status
ALTER COLUMN next_sign_id INT NOT NULL
This statement works, but this one doesn't:
ALTER TABLE dbo.current_performance_status
ALTER COLUMN next_sign_tp_id INT NOT NULL DEFAULT(0)
What is the problem here and how do I achieve both in one statement? I am using sql 2008.
You have to do this in three statements (thanks #MartinSmith for the sanity check, who suggested WITH VALUES which isn't correct in this case but still reminded me that this table may not be empty):
ALTER TABLE dbo.current_performance_status
ADD CONSTRAINT df DEFAULT (0) FOR next_sign_id;
UPDATE dbo.current_performance_status
SET next_sign_id = 0
WHERE next_sign_id IS NULL
ALTER TABLE dbo.current_performance_status
ALTER COLUMN next_sign_id INT NOT NULL;

How to modify datatype of a column with a default value

I'm trying to change the datatype of a column in SQL Server from tinyint to smallint.
But there's a default value on my column and I don't know the name of the constraint.
Is there an easy way to do it ?
This don't work because of the default constraint :
ALTER TABLE mytable
Alter Column myColumn smallint NOT NULL default 1
You need to do this in several steps - first: drop the default constraint on your column, then modify your column.
You could use code something like this:
-- find out the name of your default constraint -
-- assuming this is the only default constraint on your table
DECLARE #defaultconstraint sysname
SELECT #defaultconstraint = NAME
FROM sys.default_constraints
WHERE parent_object_id = object_ID('dbo.mytable')
-- declare a "DROP" statement to drop that default constraint
DECLARE #DropStmt NVARCHAR(500)
SET #DropStmt = 'ALTER TABLE dbo.mytable DROP CONSTRAINT ' + #defaultconstraint
-- drop the constraint
EXEC(#DropStmt)
-- alternatively: if you *know* the name of the default constraint - you can do this
-- more easily just by executing this single line of T-SQL code:
-- ALTER TABLE dbo.mytable DROP CONSTRAINT (fill in name of constraint here)
-- modify the column's datatype
ALTER TABLE dbo.mytable
Alter Column myColumn smallint NOT NULL
-- re-apply a default constraint - hint: give it a sensible name!
ALTER TABLE dbo.mytable
ADD CONSTRAINT DF_mytable_myColumn DEFAULT 1 FOR MyColumn
You could do it as a three step process
add the new column with a different name,
copy the values from the old column to the new
drop the old column
It it matters that the name is the same, then repeat the process to change the name back.
You can find the constraint name for the default using MS Management Studio. Just find the tables folder for the given DB and look under Constraints. If there are many constraints, you can "Script the Constraint(s) to a query window which show the associated column name.

Altering a column to be nullable

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;