I need a SQL query which adds a new column after an existing column, so the column will be added in a specific order.
ALTER TABLE table_name ADD COLUMN MiddleName varchar(50) AFTER Gender.
You have two options. First, you could simply add a new column with the following:
ALTER TABLE {tableName} ADD COLUMN COLNew {type};
Second, and more complicatedly, but would actually put the column where you want it, would be to rename the table:
ALTER TABLE {tableName} RENAME TO TempOldTable;
Hope it helped.
Related
GOAL
I would like to add a Redshift SUPER column to and existing redshift table.
I need this to store JSON data there
CODE
This is how Normally I would add a new column.
ALTER TABLE products
ADD COLUMN created_at NOT NULL;
1. Tried
CREATE TABLE temp_test_persons (
PersonID int,
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
ALTER TABLE temp_test_persons ADD COLUMN newSuperColumn SUPER NOT NULL;
Error running query: ALTER TABLE ADD COLUMN defined as NOT NULL must have a non-null default expression
Reviewed Solutions
Alter column data type in Amazon Redshift
AWS Redshift - Add IDENTITY column to existing table
Adding column to existing tables
Add dynamic column to existing MySQL table?
SQLAlchemy: How to add column to existing table?
adding columns to an existing table
add column to existing table postgres
How to add not null unique column to existing table
Adding columns to existing csv file using super-csv
UCanAccess: Add column to existing table
Adding columns to existing redshift table
How to add column to existing table in laravel?
Add column to existing table in rds
oracle add column to existing table
The solution is to set a default value for the column. Then you can define the column as NotNull.
Like this
ALTER TABLE temp_test_persons
ADD COLUMN newSuperColumn SUPER NOT NULL
DEFAULT '';
ALTER TABLE temp_test_persons
ADD COLUMN newSuperColumn SUPER;
When I add a column to my my table with alter table, it is inserted as the last record. Is there a way to insert the column into the position I choose?
Thank you.
Some dbms let you control the column position with an alter table statement, but most do not. MySQL is one that does. Assume you start with
create table table_name (b char(1), d char(1));
You can alter that table to put column a first, and alter it again to put c after b.
alter table table_name add column a char(1) first;
alter table table_name add column c char(1) after b;
All dbms support "reordering" columns using create view... and select... though.
In the relational model of data, the order of columns is irrelevant. But I know what it's like to work in companies where that irrelevance is irrelevant.
I am creating a package in SSIS and this will insert records based on the look up operation as like below .
Source : View
Destination : Table
NOw I have an issue while inserting record i need to insert into a destination column which is not in the source and that column needs to be autoincremented by 1. How to do it here.
Any suggestion is welcome ?
Regards
Let us say the column name is "Id". If column "Id" already has an identity to it, then you're good. If not, then in your destination table you will have to create a new identity column, remove the "Id" column and then rename new column to "Id".
ALTER TABLE (yourTable) ADD NewColumn INT IDENTITY(1,1)
ALTER TABLE (yourTable) DROP COLUMN Id
EXEC sp_rename 'yourTable.NewColumn', 'Id', 'COLUMN'
After this in your SSIS destination do not map any column from the source to Id column and the Id column in database will start from value 1 and automatically increment by a value of 1. Hope it helps.
You could add to target table column defined as IDENTITY/AUTO_INCREMENT/DEFAULT sequence_name.NEXTVAL.
In your SSIS package you need to skip that column to allow DB to populate that column.
I have old SQL sever table with 5000 rows. It has a column called OrderID which has the data type int. But this table doesn't have a primary key and OrderID is not on the sorted order. Can you please tell me how can I make this OrderID column the primary key and make it auto increment
You can't add identity to an existing column.
Your best option is to create a new table with the same structure and an identity column, set identity_insert on and then copy your records from the old one into the new one.
Check out this answer from the MS SQL Forum
You can't add identity to existing column. Create a new column "new_OderId" , copy data from "OderId" column paste in "new_OderId" column.
#add new column to Order_table
alter table Order_table add new_OderId int
#copy data from OrderId to new_OrderId
update Order_table set new_OrderId=OderId
#drop OderId column
alter table Order_table drop column OrderId
I have a H2 database with 16 million entries and no primary key. I successfully added an auto-incrementing primary key using the following statements:
ALTER TABLE
PUBLIC.ADDRESSES ADD ID BIGINT AUTO_INCREMENT;
ALTER TABLE
PUBLIC.ADDRESSES ADD PRIMARY KEY (ID)
Now the problem is, that the column order is STREET, HOUSENUMBER, ..., ID, but I would like ID to be the first column of the table. It looks like there is a corresponding ALTER TABLE statement MySQL (see here), but I'm unable to adapt it to H2.
Long story short: How can I change the column order to ID, STREET, HOUSENUMBER ...? Is there a solution similar to:
ALTER TABLE "ADDRESSES" MODIFY COLUMN "ID" BEFORE "STREET";
Any help is kindly appreciated.
H2 does not currently support re-ordering columns. You would need to run multiple statements:
First, rename the column, then add a new column with the right name at the right position (alter table add supports positioning), and finally drop the old column.
Or, probably more elegant, use rename table and then create table ... as select.