Is it possible to add multiple columns in a single Alter query in Redshift
Alter table employee
add column Name,
add column Age,
add column Salary
According to Redshift Documentation, You can add only one column in each ALTER TABLE statement. Only way to add multiple columns is executing multiple ALTER TABLE statements.
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;
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.
Code:
ALTER TABLE tblUser
DROP COLUMN Mobile
Error:
ALTER TABLE DROP COLUMN Mobile failed because one or more objects access this column.
This column had values in Table. How can I delete all objects that access this column?
How can I DROP COLUMN with values?
how can do it with code? How can I delete all constraints in column automatically?
ALTER TABLE DROP COLUMN Mobile failed because one or more objects access this column.
Your column won't be deleted. Because one column or multiple columns are getting reference from this column that you want to delete.
So first, you will have to find in which table your column is being referenced by below query.
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'TABLENAME'
It will show you all constraints of all tables of your current database. You need to find it and remove the constraint. After that your column will be deleted successfully because there is no reference of your column in any table.
To remove constraint from column - use below query
alter table tablename
drop constraint constraintid
SQL Search is a great tool. I will search for your all the objects which are using the targeted object.
You can easily find where your column is being used, then simply you can modify or drop that objects too.
Use below query to find the constraints name for particular tablename
SELECT * FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS WHERE TABLE_NAME = 'TABLENAME'
Noe you can see the constraints name under constraint_name column, drop all constraint using below syntax
ALTER TABLE TABLENAME DROP CONSTRAINT CONSTRATINTSNAME
After that you can use below statement to drop the column
ALTER TABLE TABLENAME DROP COLUMN COLUMNNAME
You need to know what those constraints are and what their names are in order to drop them; there's nothing in SQL Server to say DROP ALL CONSTRAINTS and just do it. – marc_s yesterday
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.
I'm trying to create a table from an existing table. What are the other constraints that are passed apart from the NOT NULL constraints? Also what are the other attributes/features passed to the new table?
The query that I use to create the new table is this:
create table sales_emp
as
select * from emp;
The following information can be found in the official manual (all the emphases are mine):
Oracle Database derives datatypes and lengths from the subquery. Oracle Database follows the following rules for integrity constraints and other column and table attributes:
Oracle Database automatically defines on columns in the new table any NOT NULL constraints that were explicitly created on the corresponding columns of the selected table if the subquery selects the column rather than an expression containing the column. If any rows violate the constraint, then the database does not create the table and returns an error.
NOT NULL constraints that were implicitly created by Oracle Database on columns of the selected table (for example, for primary keys) are not carried over to the new table.
In addition, primary keys, unique keys, foreign keys, check constraints, partitioning criteria, indexes, and column default values are not carried over to the new table.
If the selected table is partitioned, you can choose whether the new table will be partitioned the same way, partitioned differently, or not partitioned. Partitioning is not carried over to the new table. Specify any desired partitioning as part of the CREATE TABLE statement before the AS subquery clause.