Invalid Identifier in ALTER TABLE ADD COLUMN - sql

I'm getting the error "Invalid Identifier" whilst running this ALTER TABLE statement:
ALTER TABLE TRUCK ADD COLUMN WEIGHT INTEGER NOT NULL;
The syntax is like this, so I don't understand the error:
ALTER TABLE table_name ADD COLUMN column_name data_type[NOT NULL][UNIQUE]
The attribute WEIGHT doesn't need unique.
How do I include the "not greater than 1000" for the Integer data type into the Alter statement?

SQL Server:
ALTER TABLE TRUCK
ADD WEIGHT INT NOT NULL

This query will be helpful.
alter table truck
add column weight int not null default 0 check (weight < 1000)

Related

Can't ALTER COLUMN TYPE in PostgreSQL "0A000: Cannot alter column "FIELDA" of relation "TABLEA"

Trying to run the below script:
ALTER TABLE SCHEMA.TABLEA
ALTER COLUMN FIELDA TYPE VARCHAR(5)
And I am getting the following error:
0A000: Cannot alter column "FIELDA" of relation "TABLE"
The field is currently an integer and contains no data, only nulls.
As per the AWS documentation you cannot change the data type of a VARCHAR only the size.
https://docs.aws.amazon.com/redshift/latest/dg/r_ALTER_TABLE.html

How to alter new not null column in Postgresql?

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

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

Can't ALTER TABLE in ACCESS 2012

I'm trying to alter the table of an access database. I keep getting a syntax error for something I think should be pretty simple.
Here is what I tried to do and the error I am getting.
yes you have syntax error as what access tells you, try this one
ALTER TABLE EMP_2 ADD COLUMN EMP_PCT NUMBER(4);
ALTER TABLE EMP_2 ADD COLUMN PROJ_NUM CHAR(3);
the syntax
ALTER TABLE table {ADD {COLUMN field type[(size)] [NOT NULL] [CONSTRAINT index] |
ALTER COLUMN field type[(size)] |
CONSTRAINT multifieldindex} |
DROP {COLUMN field I CONSTRAINT indexname}}
ALTER TABLE statement

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;