Add column in Oracle table - sql

I'm trying to add an XMLType column into a table, but it returns an error. Why?
This is the query:
alter table TEST_ID add column xml_column xmltype;
It returns the error:
[SQL] alter table TEST_ID add column xml_column xmltype
[Err] ORA-00904: : invalid identifier

You don't need the "column" word in there, so it's:
ALTER TABLE test_id
ADD xml_column xmltype;

In addition,
you can add multiple columns at the same time with:
ALTER TABLE table_name ADD (column1 VARCHAR(40), column2 Date, column3 Number);

There is a syntax error- key COLUMN is not required before column name :
1. To add a single column:
ALTER TABLE TABLE_NAME ADD
COLUMN_NAME DATA_TYPE;
2. To add multiple columns:
ALTER TABLE TABLE_NAME ADD (
COLUMN_NAME1 DATA_TYPE1,
COLUMN_NAME2 DATA_TYPE2,
COLUMN_NAME3 DATA_TYPE3
.
.
.
);

Related

Alter Table Difference?

Is there a difference between
1st Way
Alter Table Test_Table
Add Test_Column
and
2nd way
Alter Table Test_Table
Add Column Test_Column
Alter Table Test_Table Add Test_Column
Use for Add new columns in table
Alter Table Test_Table Add Column Test_Column
use for Modify column in table
It depends on the database.
For example Oracle enough
ALTER TABLE xxx ADD column_name TYPE;
But in postgresql you must column after add
ALTER TABLE xxx ADD COLUMN column_name TYPE;

Change constraint of clob column

I want to change a NOT NULL constraint of a clob column to a NULL constraint. However, when trying
ALTER TABLE myTable ALTER COLUMN myClobCol clob NULL;
or
ALTER TABLE myTable modify myClobCol clob NULL;
I get the following errors:
ORA-01735: invalid ALTER TABLE option
or
ORA-22859: invalid modification of columns
What am I doing wrong? Do I have to use a temp column in this case too? I know the scenario of changing the data type from clob to varchar2 by using a temp column but here I just want to change the constraint. Why this is not possible?
Thanks in advance!
You are trying to set the type of the column from CLOB to CLOB, it's invalid because any try to set an object column is invalid. Use just ALTER TABLE myTable modify myClobCol NULL; to set the NULL constraint of the column.
Try this:
declare
col_nullable varchar2(1);
begin
select nullable into col_nullable
from user_tab_columns
where table_name = 'myTable'
and column_name = 'myClobCol';
if col_nullable = 'N' then
execute immediate 'alter table mytable modify (myClobCol null)';
end if;
end;

DB2: How do I remove AUTO_INCREMENT?

How do I remove AUTO_INCREMENT of a column in DB2?
I tried
ALTER TABLE my_table ALTER my_id
but no luck. What is the correct SQL statement?
You can modify a column definition of a DB2 table and remove the generation of values:
ALTER TABLE mytable ALTER COLUMN mycol DROP GENERATED;
You can remove the Auto_increment by using the following
ALTER TABLE table_name ALTER COLUMN column_name DROP IDENTITY;

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

How to DROP multiple columns with a single ALTER TABLE statement in SQL Server?

I would like to write a single SQL command to drop multiple columns from a single table in one ALTER TABLE statement.
From MSDN's ALTER TABLE documentation...
DROP { [CONSTRAINT] constraint_name | COLUMN column_name }
Specifies that constraint_name or column_name is removed from the table. DROP COLUMN is not allowed if the compatibility level is 65 or earlier. Multiple columns and constraints can be listed.
It says that mutliple columns can be listed in the the statement but the syntax doesn't show an optional comma or anything that would even hint at the syntax.
How should I write my SQL to drop multiple columns in one statement (if possible)?
For SQL Server:
ALTER TABLE TableName
DROP COLUMN Column1, Column2;
The syntax is
DROP { [ CONSTRAINT ] constraint_name | COLUMN column } [ ,...n ]
For MySQL:
ALTER TABLE TableName
DROP COLUMN Column1,
DROP COLUMN Column2;
or like this1:
ALTER TABLE TableName
DROP Column1,
DROP Column2;
1 The word COLUMN is optional and can be omitted, except for RENAME COLUMN (to distinguish a column-renaming operation from the RENAME table-renaming operation). More info here.
Summarizing
Oracle:
ALTER TABLE table_name DROP (column_name1, column_name2);
MS SQL Server:
ALTER TABLE table_name DROP COLUMN column_name1, column_name2
MySQL:
ALTER TABLE table_name DROP column_name1, DROP column_name2;
PostgreSQL
ALTER TABLE table_name DROP COLUMN column_name1, DROP COLUMN column_name2;
Be aware
DROP COLUMN does not physically remove the data for some DBMS. E.g. for MS SQL. For fixed length types (int, numeric, float, datetime, uniqueidentifier etc) the space is consumed even for records added after the columns were dropped. To get rid of the wasted space do ALTER TABLE ... REBUILD.
create table test (a int, b int , c int, d int);
alter table test drop column b, d;
Be aware that DROP COLUMN does not physically remove the data, and for fixed length types (int, numeric, float, datetime, uniqueidentifier etc) the space is consumed even for records added after the columns were dropped. To get rid of the wasted space do ALTER TABLE ... REBUILD.
This may be late, but sharing it for the new users visiting this question.
To drop multiple columns actual syntax is
alter table tablename drop column col1, drop column col2 , drop column col3 ....
So for every column you need to specify "drop column" in Mysql 5.0.45.
The Syntax as specified by Microsoft for the dropping a column part of an ALTER statement is this
DROP
{
[ CONSTRAINT ]
{
constraint_name
[ WITH
( <drop_clustered_constraint_option> [ ,...n ] )
]
} [ ,...n ]
| COLUMN
{
column_name
} [ ,...n ]
} [ ,...n ]
Notice that the [,...n] appears after both the column name and at the end of the whole drop clause. What this means is that there are two ways to delete multiple columns. You can either do this:
ALTER TABLE TableName
DROP COLUMN Column1, Column2, Column3
or this
ALTER TABLE TableName
DROP
COLUMN Column1,
COLUMN Column2,
COLUMN Column3
This second syntax is useful if you want to combine the drop of a column with dropping a constraint:
ALTER TBALE TableName
DROP
CONSTRAINT DF_TableName_Column1,
COLUMN Column1;
When dropping columns SQL Sever does not reclaim the space taken up by the columns dropped. For data types that are stored inline in the rows (int for example) it may even take up space on the new rows added after the alter statement. To get around this you need to create a clustered index on the table or rebuild the clustered index if it already has one. Rebuilding the index can be done with a REBUILD command after modifying the table. But be warned this can be slow on very big tables. For example:
ALTER TABLE Test
REBUILD;
For MySQL (ver 5.6), you cannot do multiple column drop with one single drop-statement but rather multiple drop-statements:
mysql> alter table test2 drop column (c1,c2,c3);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(c1,c2,c3)' at line 1
mysql> alter table test2 drop column c1,c2,c3;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'c2,c3' at line 1
mysql> alter table test2 drop column c1, drop column c2, drop c3;
Query OK, 0 rows affected (0.64 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql>
BTW, drop <col_name> is shorthanded for drop column <col_name> as you can see from drop c3 above.
If it is just single column to delete the below syntax works
ALTER TABLE tablename DROP COLUMN column1;
For deleting multiple columns, using the DROP COLUMN doesnot work, the below syntax works
ALTER TABLE tablename DROP (column1, column2, column3......);
Generic:
ALTER TABLE table_name
DROP COLUMN column1,column2,column3;
E.g:
ALTER TABLE Student
DROP COLUMN Name, Number, City;
alter table tablename drop (column1, column2, column3......);
for postgis is
alter table table01 drop columns col1, drop col2
this query will alter the multiple column test it.
create table test(a int,B int,C int);
alter table test drop(a,B);
ALTER table table_name Drop column column1, Drop column column2,Drop column column3;
for MySQL DB.
Or you can add some column while altering in the same line:
ALTER table table_name Drop column column1, ADD column column2 AFTER column7;