I am testing the alter command in AWS Redshift. I have created a test_table_sort table to which I am trying to add a column called school.
create table test_table_sort (
id int generated by default as identity (1,3) not null,
student_name varchar(50),
primary key(id))
sortkey (student_name);
I have added some student ids and name. Now I've altered by adding and age column
alter table test_table_sort add column age int default 10
But when I try to add a varchar column as alter table test_table_sort add column school varchar(30) default "SVS school" the system throws an error as below
ERROR: column "svs school" does not exist in test_table_sort .
I've first thought there may be some spacing error or something like that but not sure what is the problem with this?
the problem is with a quote's format
"" is used for accessing a column's name
try
alter table test_table_sort add column school varchar(30) default 'SVS school'
Related
I am having this table which I migrated from MySQL using pgLoader
CREATE TABLE IF NOT EXISTS schema.example
(
"ExampleID" integer NOT NULL,
"Name" character varying(255) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT "idx_43997_PRIMARY" PRIMARY KEY ("ExampleID"),
)
I am trying to convert the ExampleID to be serial so it will auto increment using this method:
Changing primary key int type to serial
So I am doing
CREATE SEQUENCE example_id_seq MINVALUE 3
Which works fine, but then
ALTER TABLE example ALTER "ExampleID" SET DEFAULT nextval('example_id_seq')
Gives the error:
ERROR: cannot use column reference in DEFAULT expression
SQL state: 0A000
But if I remove the "" and just put ExampleID like that:
ALTER TABLE example ALTER ExampleID SET DEFAULT nextval('example_id_seq')
I will get the error
ERROR: column "exampleid" of relation "example" does not exist
SQL state: 42703
The problem was that unlike in the example
ALTER TABLE example ALTER "ExampleID" SET DEFAULT nextval('example_id_seq')
I forgot to put ' ' next to the sequence name so it was
ALTER TABLE example ALTER "ExampleID" SET DEFAULT nextval(example_id_seq)
The column reference was that it mistaken the sequence for column name reference.
To add the default string value in the column created through pgadmin dashboard need to wrap the string around single quotes.
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);
I would like to perform the following change, from
CREATE TABLE IF NOT EXISTS scheme.xxx (
id SERIAL PRIMARY KEY,
to
CREATE TABLE IF NOT EXISTS scheme.xxx (
id UUID DEFAULT uuid_generate_v4(),
in an existing table with records, but I fail to achieve it.
An example that doesn't work is:
ALTER TABLE scheme.xxx
ALTER COLUMN id TYPE UUID SET DEFAULT uuid_generate_v4()
USING id::uuid_generate_v4() ;
You'll have to remove the default value first, then change the type, then add the new default value.
But first find out the sequence:
SELECT pg_get_serial_sequence('scheme.xxx', 'id');
pg_get_serial_sequence
------------------------
scheme.xxx_id_seq
(1 row)
Now do it:
ALTER TABLE scheme.xxx
ALTER id DROP DEFAULT,
ALTER id TYPE uuid USING uuid_generate_v4(),
ALTER id SET DEFAULT uuid_generate_v4();
All in one statement!
Now get rid of the sequence:
DROP SEQUENCE public.xxx_id_seq;
I would like to changes my existing column as Auto Identity in a Postgres Database.
I tried with below script but it won't worked.
Let me know if you have solution for the same
I don't want to use postgres SEQUENCE. I would like to use GENERATED ALWAYS AS IDENTITY.
ALTER TABLE public.patient ALTER COLUMN patientid Type int4
USING patientid::int4 GENERATED ALWAYS AS IDENTITY;
Following the documentation
ALTER TABLE patient
ALTER patientid SET NOT NULL, -- optional
ALTER patientid ADD GENERATED ALWAYS AS IDENTITY
(START WITH 2); -- optional
Add NOT NULL constraint if the column does not have the constraint yet. The optional clause START WITH start changes the recorded start value of the sequence.
Test it in DB<>Fiddle.
Suppose you have a table patient previously created as
CREATE TABLE patient( patientid int, col1 int );
and a row inserted as
INSERT INTO patient VALUES(1,5);
Firstly create a sequence starting +1 iterated from the max value of ID and make it default for your column
CREATE SEQUENCE mySeq START WITH 2;
ALTER TABLE patient ALTER COLUMN patientid SET DEFAULT nextval('mySeq');
and convert your column to a primary key
ALTER TABLE patient ALTER COLUMN patientid SET NOT NULL;
ALTER TABLE patient ADD CONSTRAINT uk_patientid UNIQUE (patientid);
whenever you insert new rows such as
INSERT INTO patient(col1) VALUES(10);
INSERT INTO patient(col1) VALUES(15);
you'll observe that you sucessfully made your column as an identity column
SELECT * FROM patient
patientid col1
--------- ----
1 5
2 10
3 15
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