Error during creation of sequence on specific schema in Postgres - postgresql

I have a strange error in Postgres during creation of sequence and linking on existing table.
I tried to execute the following script on existing DB which have a schema named 'testschema':
BEGIN TRANSACTION;
CREATE TABLE testschema.mytable (
id INTEGER,
value VARCHAR(30),
CONSTRAINT pk_mytable PRIMARY KEY (id));
CREATE SEQUENCE testschema.mytable_id_seq;
ALTER TABLE testschema.mytable ALTER COLUMN id SET NOT NULL;
ALTER TABLE testschema.mytable ALTER COLUMN id SET DEFAULT nextval('mytable_id_seq');
COMMIT;
I got the following error for the second ALTER TABLE query:
********** Erreur **********
ERREUR: la relation « mytable_id_seq »
n'existe pas
État SQL :42P01
Error message can be translated by "Relation 'mytable_id_seq' does not exist".
If I try to execute this script without schema (i.e. in the public schema), it works.
I don't understand what it is failing. Does anyone see where the problem is?
Thanks for your help.

Replace:
ALTER TABLE testschema.mytable ALTER COLUMN id SET DEFAULT nextval('mytable_id_seq');
by
ALTER TABLE testschema.mytable ALTER COLUMN id SET DEFAULT nextval('testschema.mytable_id_seq');
By default, objects without using schema quallifier are searched in the schema that are in the search_path variable. by default default, only public schema exists in this variable.
So the simplest way is to use full quallified qualifier when referencing your objects.
Or, you can put your new schema in the path, like this:
SET search_path TO testschema,public;
Source

Related

syntax error in my postgres statement for alter sequence

UPDATE:
using postgres 14,
I just get error :Query 1 ERROR: ERROR: syntax error at or near "ok"
This is my database:
-- Sequence and defined type
CREATE SEQUENCE IF NOT EXISTS id_seq;
-- Table Definition
CREATE TABLE "public"."ok" (
"id" int8 NOT NULL DEFAULT nextval('id_seq'::regclass),
PRIMARY KEY ("id")
);
And I want to modify the sequence:
ALTER SEQUENCE ok_id_seq RESTART;
I keep getting errors at ok_id_seq.
I tried id_seq only
Tried quotes everywhere.
Ok, I now learnt that sequences are not table specific. I was trying to do alter table alter sequence.
I also used SELECT * FROM information_schema.sequences; to view all available sequences.
I don't know what happened so I recreated the table, checked for the sequence name.
Then was able to alter it with restart

Postgresql alter table column type to unique not null

ALTER TABLE users ALTER COLUMN email VARCHAR(50) UNIQUE NOT NULL;
ERROR: syntax error at or near "VARCHAR"
LINE 1: ALTER TABLE users ALTER COLUMN email VARCHAR(50) UNIQUE NOT ...
I want to alter column email to add its type as UNIQUE NOT NULL in Postgresql and get this error. Can you explain to me what's wrong?
You cannot create 2 constraints with one single statement. And you have to use PostgreSQL syntax.
alter table users alter column email set not null;
alter table users add constraint email_unique unique (email);

Adding Identity column in Ingres Db

I am trying to add an identity column in a table through alter query using Ingres DB. While creating the table, i am able to define the identity column but not when i am trying to add it through alter query. Kindly Suggest me an alter query for it.
It's not as straightforward as you might think, "alter table" has a a number of restrictions which make this a multi-step operation. Try this:
create table something(a integer, b varchar(20)) with page_size=8192;
alter table something add column c integer not null with default;
modify something to reconstruct;
alter table something alter column c integer not null generated always as identity;
modify something to reconstruct;

How to rename a table inside a schema?

I'm using PostgreSQL 9.x, I want to rename a table. This SQL code:
CREATE TABLE new (id int);
ALTER TABLE new RENAME TO old;
DROP TABLE old;
renames the table correctly. But this SQL code:
CREATE SCHEMA domain;
CREATE TABLE domain.old (id int);
ALTER TABLE domain.old RENAME TO domain.new;
fails, with error:
ERROR: syntax error at or near "."
The "." underlined is the one between 'domain' and 'new'
One way to do this:
ALTER TABLE domain.old RENAME TO new
Other way:
SET search_path TO domain;
ALTER TABLE old RENAME TO new;
Documentation for search_path.
SET search_path TO domain;
ALTER TABLE IF EXISTS old_table_name RENAME TO new_table_name;
Switch to your database
machine$\c my_database
Tename the db
my_databse=# alter table old_name rename to new_name;

SQL Azure - Could not able to alter column type

Today I created a new table in SQL Azure portal and by default there is an Id INT column.
Id ( int , PK , Not Null)
When I tried to change it to BIGINT it gave me the following error:
An error was encountered while applying the changes.An exception occurred
while executing the Transact-SQL statement:
ALTER TABLE [dbo].[PerformanceData]
ALTER COLUMN [Id] BIGINT NOT NULL.
The object 'PrimaryKey_029c7a8d-e6b2-43b8-94f1-98fc5b0115e3' is dependent on
column 'Id'. ALTER TABLE ALTER COLUMN Id failed because one or more objects
access this column.
Why did this happen?
Looks like the column you are trying to alter is the primary key column. You need to drop related constraints first. Something like this:ALTER TABLE [dbo].[PerformanceData] DROP CONSTRAINT Id