I'm using Postgres 9.1. and the auto_increment (serial) is not working. I've just found this about 'serial':
https://www.postgresql.org/docs/current/static/datatype-numeric.html#DATATYPE-SERIAL
CREATE TYPE FAMILY AS(
id int,
name VARCHAR(35),
img_address VARCHAR(150));
CREATE TABLE FAMILIES of FAMILY(
id SERIAL primary key NOT NULL,
name NOT NULL
);
ERROR: syntax error at or near "SERIAL"
LINE 7: id SERIAL primary key NOT NULL,
^
********** Error **********
ERROR: syntax error at or near "SERIAL"
SQL state: 42601
When you create a table using the syntax:
CREATE TABLE xxx OF yyyy
you can add default values and constraints, but not alter or specify the type of the columns.
The type SERIAL is in effect a combination of a data type, NOT NULL constraint and default value specification. It is equivalent to:
integer NOT NULL DEFAULT nextval('tablename_colname_seq')
See: documentation for SERIAL
So, instead you would have to use:
CREATE SEQUENCE families_id_seq;
CREATE TABLE FAMILIES of FAMILY(
id WITH OPTIONS NOT NULL DEFAULT nextval('families_id_seq'),
name WITH OPTIONS NOT NULL
);
ALTER SEQUENCE families_id_seq OWNED BY FAMILIES.id;
You would have to create the sequence families_id_seq manually as well, as shown above.
Related
It happens when i run this code:
CREATE TABLE distributors (
did integer PRIMARY KEY DEFAULT nextval('serial'),
name varchar(40) NOT NULL CHECK (name <> '')
);
I have tried remover the nextval('serial') but to no avail
You want to do this:
CREATE TABLE distributors (
did serial PRIMARY KEY DEFAULT,
name varchar(40) NOT NULL CHECK (name <> '')
);
The serial type is actually a macro. That per docs (Serial) does:
CREATE SEQUENCE tablename_colname_seq AS integer;
CREATE TABLE tablename (
colname integer NOT NULL DEFAULT nextval('tablename_colname_seq')
);
ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname;
Assuming you are on a recent version(10+) of Postgres generated always as identity(see Create Table) is the preferred alternative these days.
nextval('someseries') relies on having an existing series. You can create that with:
CREATE SEQUENCE someseries;
When you removed the nextval, you probably still had the DEFAULT keyword there, which expects a value afterward to define what the default for the column is.
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.
I was tryiong to switch from MSSQL to PostgreSQL and hence trying to convert queries to PostgreSQL equivalent. However running PostgreSQL query is giving an error:
ERROR: type "uniqueidentifier" does not exist LINE 3: ID
UNIQUEIDENTIFIER DEFAULT UUID_GENERATE_V4()::VARCHAR NO...
^ SQL state: 42704 Character: 38
MSSQL
CREATE TABLE [dbo].[ISS_AUDIT]
(
[ID] UNIQUEIDENTIFIER DEFAULT NEWID() NOT NULL,
[GRAPH_ID] [varchar](196)
PRIMARY KEY(ID)
);
PostgreSQL
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE public.ISS_AUDIT
(
ID UNIQUEIDENTIFIER DEFAULT UUID_GENERATE_V4()::VARCHAR NOT NULL,
GRAPH_ID VARCHAR(196),
PRIMARY KEY(ID)
);
Am I missing something on UNIQUEIDENTIFIER ?
This is the correct script:
CREATE TABLE public.ISS_AUDIT
(
ID uuid PRIMARY KEY DEFAULT UUID_GENERATE_V4(),
GRAPH_ID VARCHAR(196)
);
See this link. Extract:
SQL Server calls the type UniqueIdentifier and PostgreSQL calls the
type uuid. Both types occupy 16-bytes of storage. For compatibility
reasons with other software or databases, many use some stanardized
text representation of them particularly for transport rather than
using the native type.
We need UUID which can be used as below:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE public.ISS_AUDIT(
ID UUID DEFAULT UUID_GENERATE_V4()::UUID NOT NULL,
GRAPH_ID VARCHAR(196),
PRIMARY KEY(ID)
);
Existing table with 5 columns.
qid which is the PK, question geo_type user_input active
I need to be able to insert into the table with each new insert getting a new primary key id (which would be the max existing id +1).
So i need to be able to do this
insert into sip_questions (question,geo_type,user_input,active) values('noury','octagon',TRUE,TRUE)
but this give me this error
ERROR: duplicate key value violates unique constraint "s_questions_pkey"
DETAIL: Key (qid)=(1) already exists.
********** Error **********
ERROR: duplicate key value violates unique constraint "s_questions_pkey"
SQL state: 23505
Detail: Key (qid)=(1) already exists.
this is the table
CREATE TABLE public.sip_questions
(
qid integer NOT NULL DEFAULT nextval('s_questions_qid_seq'::regclass),
question character varying(200),
geo_type character varying(10),
user_input boolean,
active boolean,
CONSTRAINT s_questions_pkey PRIMARY KEY (qid)
)
WITH (
OIDS=FALSE
);
ALTER TABLE public.sip_questions
OWNER TO postgres;
i know how to do this from a fresh table like this
ALTER TABLE table ADD COLUMN id SERIAL PRIMARY KEY;
and every insert will increment the PK without me having to specify the id column
The new sequence must be bumped to the current max value.
You can reset it using
SELECT setval('s_questions_qid_seq', max(id)) FROM sip_questions;
I'm using postgresql 9.1 on Ubuntu 12.04. I have a user defined type as one column of a table. When I create a primary key constraint I get a syntax error.
Here is a sample sql script I'm using with psql to create the table:
CREATE TYPE my_type AS
(
field1 integer,
field2 integer
);
CREATE TABLE my_table
(
my_data my_type,
other_data integer
);
ALTER TABLE my_table ADD CONSTRAINT pk_my_table PRIMARY KEY (my_data.field1);
I get this error:
ERROR: syntax error at or near "."
LINE 1: ...ble ADD CONSTRAINT pk_my_table PRIMARY KEY (my_data.field1);
I've tried using (my_data).field1 but also get a syntax error.
If I just use my_data in the constraint there is no error:
ALTER TABLE my_table ADD CONSTRAINT pk_my_table PRIMARY KEY (my_data);
But I would like to use just one field as part of the constraint.
Thanks for any ideas.
I found this question using google and I'm pretty sure is dead but I still want to answer it cause someone else might see it.
Short answer is you have to use the field's name:
ALTER TABLE "public"."carga" ADD CONSTRAINT "pk_my_table" PRIMARY KEY ("field1");