Postgresql can not Alter table in schema - sql

I have a table named "well_all", which is in a schema named "staging", postgresql.
Its data is from a csv, the table doesn't have "id" field. I wanted to add "id" field in the table. So I did:
ALTER TABLE staging.well_all ADD COLUMN id SERIAL PRIMARY KEY;
But I am getting an error:
"well_all" is not a table, composite type, or foreign table
So, I'd tried to add "id" field simply:
ALTER TABLE staging.well_all ADD COLUMN id INTEGER NOT NULL;
But getting the same error, too.
I checked the database exists in that schema. It exists.
Is this something you faced before? Would you like to help me to solve this problem?

Related

Can´t add field without foreign key

I have two database one local and other in production.
In one of them I have column IdNivelDominio without FK but if I open Constraints folder and I have something like:
DF_EvaluacionDetalleCompetenciasFuncionales_IdNivelDominio
So I want to reply this field into my another database as:
ALTER TABLE Reclutamiento.EvaluacionDetalleCompetenciasFuncionales
ADD IdNivelDominio INT NOT NULL;
But I get error:
ALTER TABLE only allows columns to be added that can contain nulls, or
have a DEFAULT definition specified, or the column being added is an
identity or timestamp column, or alternatively if none of the previous
conditions are satisfied the table must be empty to allow addition of
this column. Column 'IdNivelDominio' cannot be added to non-empty
table 'EvaluacionDetalleCompetenciasFuncionales' because it does not
satisfy these conditions.
Problem is that field is not linked with a foreign key (into original table) so I can´t add constraint.
Can anyone explain me how it occurs? or there any way to do constraint without foreign key? Regards
ALTER TABLE Reclutamiento.EvaluacionDetalleCompetenciasFuncionales
ADD IdNivelDominio INT NOT NULL DEFAULT 0;
You're trying to add a new column that should be NOT NULL to table with data. You need to specify what value will be in that new column because it's NOT NULL

How i can modify the Parent table for my Sql server 2008 r2 FK

I have a FK inside my table but i want to modify the parent table for the FK . so is there an alter command that can achieve this ? or i need to remove the FK and create a new one ?
Thanks
Add this to your PK and it will automatically update all FKs for you:
ON UPDATE CASCADE
For full details, you can read this article.
EDIT Based on your comment, if you want to change the PK data type, it depends on the change:
If the old type can be implicitly casted to the new type without any loss:
Change the PK type first.
Change the FK type to the same.
If the old type cannot be implicitly casted to the new type without any loss:
Break the relationship first (i.e. remove the FK restriction/index).
Convert the PK. If the data needs to be modified, save both the old values and the new ones in a temporary table.
Convert the FK. If the PK data was changed in previous step, update the FK using the mapped values from the temporary table.
Create the relationship again (i.e. create the FK restriction/index).
To modify the data type, use the ALTER command, the syntax is:
ALTER TABLE table_name
ALTER COLUMN column_name datatype
Examples:
ALTER TABLE table_name
ALTER COLUMN id NUMBER(10,2);
ALTER TABLE table_name
ALTER COLUMN id VARCHAR(20);
For full details, you can read this article.
Looks like you are looking for alter statement but since you didn't mention exactly what you are looking to modify; I assume that you want to change column data type size. You can do something like this (an example; say you want to change size from 10 to 15)
alter table sample3
alter column name varchar(15)
EDIT:
In that case this is what you should be doing. You need to drop the existing constraint and recreate the constraint to point to TableC
alter table TableA
drop constraint your_FK_constraint_name
alter table TableA
add constraint constraint_name
FOREIGN KEY (column_name) references TableC(some other column name)
An Example:
alter table sample2
drop constraint FK__sample2__realnam__09DE7BCC
alter table sample2
add constraint FK__sample2__realnam
FOREIGN KEY (realname) references sample1(name)
Based on this comment, "now my current FK inside TableA is referring to another table primary key TableB. but i need my modify my current FK to refer to tableC instead of tableB ... this what i need (to modify the parent table for my FK)– "
The parent table is TableB. No action is required on that table.
On TableA, you have to:
Drop the existing foreign key constraint.
Update as necessary so that all values in the applicable column have a matching value in TableC.
Add a new foreign key constraint.
in that order.
Edit Starts Here
Here is a link to the syntax,

H2 Database - Reorder columns using SQL

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.

Altering my primary key to auto increment - JavaDB

I am using Netbeans, writing in Java and using Derby.
I have a table within APP called PERSON. Within PERSON I have a column called PID with the following properties:
Name: PID
Nulls allowed: [ ]
Data type: NUMERIC
Column size: 4
Decimal digits: 0
Position: 1
Part of a primary key: [/]
Part of an index: [/]
I used the meta data isAutoIncrement function to check if it was already auto incrementing and it is not!
I have since tried using the following SQL commands to alter it:
I believe this may not have been for Derby:
ALTER TABLE APP.PERSON ALTER PID NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY
(START WITH 1, INCREMENT BY 1);
Upon checking the Oracle website, I found the correct syntax:
ALTER TABLE APP.PERSON ALTER PID SET INCREMENT BY 1;
I even tried leading zeros:
ALTER TABLE APP.PERSON ALTER PID SET INCREMENT BY 0001;
None of which have worked, the error I get on the last two are:
ALTER TABLE '"APP"."PERSON"' specified attributes for column 'PID' that are
not compatible with the existing column.
Any ideas of the correct syntax?
Here's what I generally do to accomplish this:
Create a new table, with the desired schema, including the generated primary key
Issue a INSERT INTO newtable SELECT columns FROM oldtable to populate the new table's data from the old table
Rename the old table to some temporary name, like table_soon_to_be_deleted
Rename the new table to the desired table name
Do some testing to make sure that my behavior is as expected
Drop the old table that I renamed in step (4).
JavaDB does not allow altering a column with generated key word so I found the best way is to recreate the table and specify the primary key as auto incremented. For example;
create table staff(
ID int primary key always generated as identity,
name varchar(100)
);
This worked for me.

How to add a PK to an existing table in DB2?

I am facing one problem. I have a table already create in DB2.
CREATE TABLE "DDL12"
(
"D4_1" decimal(10,0),
"D4_2" decimal(10,0),
);
I am trying to create a PK on this table as :-
ALTER TABLE "DDL12" ADD CONSTRAINT "Key4" PRIMARY KEY ("D4_1");
But while running the command, I am getting the error saying D4_1 is NULLABLE.
Now, how can I create a PK on this table?
Thanks
Yes, this is due the fact, that your database "could have" rows having NULL value in that non PK column right now.
So first set the column to NOT NULL (+ make sure having a unique value in all rows) and then set the primary key with the command above.
You can change a column to not NULL like this:
ALTER TABLE "DDL12"
MODIFY "D4_1" decimal(10,0) NOT NULL;