First of all can I give Varchar for primary key? Here is my create table;
CREATE TABLE appinfo (
app_id VARCHAR (30) PRIMARY KEY,
app_secret VARCHAR ( 500 ) NOT NULL,
app_name VARCHAR ( 255 ) NOT NULL,
app_type VARCHAR ( 255 ) NOT NULL,
prog_lang VARCHAR ( 255 ) NULL,
description VARCHAR ( 255 ) NULL,
created_on TIMESTAMP NOT NULL DEFAULT NOW()
);
And I am trying to insert below value
INSERT INTO appinfo (app_id, app_secret, app_name, app_type)
VALUES ('ABC1', 'aaaaaaabbbbbbccccccccc', 'AccountInfoAPI', 'API');
but this shows below error;
ERROR: invalid input syntax for type integer: "ABC1"
LINE 2: VALUES ('ABC1', 'aaaaaaabbbbbbccccccccc', 'AccountInfoAPI...
I am very new to Postgres and I am not sure where is this Integer coming from. Or is that because Primary key is always Integer in Postgres?
Here is the SQL I'm using to create my table:
create table LAB_REQUESTS_REQUREMENTS
(
id INTEGER not null,
request_id INTEGER not null,
test_id INTEGER not null,
requrement VARCHAR2(30) not null,
assistant_id INTEGER,
measured_result VARCHAR2(50),
measured_condition VARCHAR2(50),
price_id INTEGER not null,
single_price NUMBER(15,2) default 0 not null,
measured_date DATE,
measure NVARCHAR2(30),
metric_tolerance NVARCHAR2(30)
)
tablespace VIK
pctfree 10
initrans 1
maxtrans 255
storage
(
initial 64K
next 1M
minextents 1
maxextents unlimited
);
How should I go about inserting those characters inside 'MEASURE' for example without going through conversion when inserting and then reading from the table?
I have the following tables :
CREATE TABLE public.participant_audit
(
participant_audit_id bigint NOT NULL DEFAULT nextval('participant_audit_participant_audit_id_seq'::regclass),
participant_id bigint,
shared_asset_id bigint NOT NULL,
asset_role_type character varying(200) NOT NULL,
user_external_ref_uuid uuid NOT NULL,
user_first_name character varying(200) NOT NULL,
user_last_name character varying(200) NOT NULL,
user_email_address character varying(200) NOT NULL,
deleted_timestamp timestamp(0) with time zone,
row_updated_timestamp timestamp(6) with time zone NOT NULL,
row_created_timestamp timestamp(6) with time zone NOT NULL,
row_created_by_db_user oid NOT NULL,
row_updated_by_db_user oid NOT NULL,
created_by_client uuid,
updated_by_client uuid,
CONSTRAINT participant_audit_pkey PRIMARY KEY (participant_audit_id)
)
WITH (
OIDS=FALSE
);
CREATE TABLE public.participant
(
participant_id bigint NOT NULL DEFAULT nextval('participant_participant_id_seq'::regclass),
shared_asset_id bigint NOT NULL,
asset_role_type_id bigint NOT NULL,
user_external_ref_uuid uuid NOT NULL,
user_first_name character varying(200) NOT NULL,
user_last_name character varying(200) NOT NULL,
user_email_address character varying(200) NOT NULL,
deleted_timestamp timestamp(0) with time zone,
row_updated_timestamp timestamp(6) with time zone NOT NULL,
row_created_timestamp timestamp(6) with time zone NOT NULL,
row_created_by_db_user oid NOT NULL,
row_updated_by_db_user oid NOT NULL,
created_by_client uuid,
updated_by_client uuid,
CONSTRAINT participant_pkey PRIMARY KEY (participant_id),
CONSTRAINT participant_asset_role_type_id_fkey FOREIGN KEY (asset_role_type_id)
REFERENCES public.asset_role_type (asset_role_type_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT participant_shared_asset_id_fkey FOREIGN KEY (shared_asset_id)
REFERENCES public.shared_asset (shared_asset_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
OIDS=FALSE
);
And the following TRIGGER FUNCTION:
-- DROP FUNCTION public.participant_audit();
CREATE OR REPLACE FUNCTION public.participant_audit()
RETURNS trigger AS
$BODY$
BEGIN
insert into participant_audit
(participant_audit_id, participant_id , shared_asset_id , asset_role_type , user_external_ref_uuid,
user_first_name , user_last_name , user_email_address , deleted_timestamp, row_updated_timestamp,
row_created_timestamp , row_created_by_db_user , row_updated_by_db_user , created_by_client,
updated_by_client
)
select NEW.* ;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql VOLATILE SECURITY DEFINER
COST 100;
When I execute the following INSERT
INSERT INTO participant (shared_asset_id,asset_role_type_id,
user_external_ref_uuid,user_first_name,user_last_name,
user_email_address,row_created_by_db_user,
row_updated_by_db_user,created_by_client,updated_by_client)
VALUES (1, 1, 'c9d140ad-b0da-4a9d-a898-8719000c7b7b'::uuid , 'john', 'simpson', 'js#gmail.com', 1::oid,1::oid, '53ed670d-f680-4e81-b53d-59b3d487633f'::uuid, '53ed670d-f680-4e81-b53d-59b3d487633f'::uuid);
I get the following error:
ERROR: INSERT has more target columns than expressions LINE 2:
...user , row_updated_by_db_user , created_by_client,updated_by...
^ QUERY: insert into public.participant_audit
(participant_audit_id, participant_id , shared_asset_id , asset_role_type ,
user_external_ref_uuid,user_first_name , user_last_name ,
user_email_address , deleted_timestamp,
row_updated_timestamp,row_created_timestamp , row_created_by_db_user ,
row_updated_by_db_user , created_by_client,updated_by_client)
select NEW.* CONTEXT: PL/pgSQL function participant_audit() line 3 at SQL statement
********** Error **********
ERROR: INSERT has more target columns than expressions SQL state:
42601 Context: PL/pgSQL function participant_audit() line 3 at SQL
statement
How can I fix this issue ??
The problem is in your trigger. Count the columns that you are trying to insert into the audit table here.
insert into participant_audit
(participant_audit_id, participant_id , shared_asset_id , asset_role_type , user_external_ref_uuid,
user_first_name , user_last_name , user_email_address , deleted_timestamp, row_updated_timestamp,
row_created_timestamp , row_created_by_db_user , row_updated_by_db_user , created_by_client,
updated_by_client
)
select NEW.* ;
That's quite a few more than what's contained in NEW because your insert statement has only 10 columns in it. I believe some of your columns maybe taking NULL values. Pass nulls explicitly in the SELECT part of your statement inside the trigger.
Trying to set up my postgres table in heroku and I'm getting this error when trying to set up the auto-incrmenting in the primary key on my table
user_id_seq relation does not exist
This is the create statement
CREATE TABLE "public"."user" (
"id" INTEGER DEFAULT nextval('user_id_seq'::regclass) NOT NULL UNIQUE,
"uname" CHARACTER VARYING( 255 ) COLLATE "pg_catalog"."default" UNIQUE,
"description" CHARACTER VARYING( 2044 ) COLLATE "pg_catalog"."default",
"country" CHARACTER( 3 ) COLLATE "pg_catalog"."default" DEFAULT 'USA'::bpchar NOT NULL,
PRIMARY KEY ( "id" )
, CONSTRAINT "unique_uname" UNIQUE( "uname" ) );
What does that error mean and do I have to set up that relation beforehand somehow?
You have to create the equence first with something like:
CREATE SEQUENCE user_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;`
Or declaring you column id as a serial:
CREATE TABLE "public"."user" (
"id" bigserial NOT NULL UNIQUE,
"uname" CHARACTER VARYING( 255 ) COLLATE "pg_catalog"."default" UNIQUE,
"description" CHARACTER VARYING( 2044 ) COLLATE "pg_catalog"."default",
"country" CHARACTER( 3 ) COLLATE "pg_catalog"."default" DEFAULT 'USA'::bpchar NOT NULL,
PRIMARY KEY ( "id" )
, CONSTRAINT "unique_uname" UNIQUE( "uname" ) );
which is equivalent to:
CREATE SEQUENCE user_id_seq;
CREATE TABLE "public"."user" (
"id" bigint NOT NULL UNIQUE DEFAULT nextval('user_id_seq'),
"uname" CHARACTER VARYING( 255 ) COLLATE "pg_catalog"."default" UNIQUE,
"description" CHARACTER VARYING( 2044 ) COLLATE "pg_catalog"."default",
"country" CHARACTER( 3 ) COLLATE "pg_catalog"."default" DEFAULT 'USA'::bpchar NOT NULL,
PRIMARY KEY ( "id" )
, CONSTRAINT "unique_uname" UNIQUE( "uname" ) );
ALTER SEQUENCE user_id_seq OWNED BY public.user.id;
Just installed SQL Server 2014.
Syntax straight from the documentation fails.
This fails on Sales:
CREATE TYPE [Sales].[SalesOrderDetailType_inmem] AS TABLE(
[OrderQty] [smallint] NOT NULL,
[ProductID] [int] NOT NULL,
[SpecialOfferID] [int] NOT NULL,
[LocalID] [int] NOT NULL,
INDEX [IX_ProductID] HASH ([ProductID]) WITH ( BUCKET_COUNT = 8),
INDEX [IX_SpecialOfferID] NONCLUSTERED
)
WITH ( MEMORY_OPTIMIZED = ON )
But if I remove Sales it still fails
CREATE TYPE [SalesOrderDetailType_inmem] AS TABLE(
[OrderQty] [smallint] NOT NULL,
[ProductID] [int] NOT NULL,
[SpecialOfferID] [int] NOT NULL,
[LocalID] [int] NOT NULL,
INDEX [IX_ProductID] HASH ([ProductID]) WITH ( BUCKET_COUNT = 8),
INDEX [IX_SpecialOfferID] NONCLUSTERED
)
WITH ( MEMORY_OPTIMIZED = ON )
With the following error
Msg 8135, Level 16, State 0, Line 10
Table level constraint does not specify column list, table 'SalesOrderDetailType_inmem'.
How to create a memory optimize table type?
The following syntax seems to work. I've scripted for the dbo schema. It is recommended that the BUCKET_COUNT is set to ~1x-2x the unique values that will be held.
CREATE TYPE [SalesOrderDetailType_inmem] AS TABLE(
[OrderQty] [smallint] NOT NULL,
[ProductID] [int] NOT NULL
INDEX [IX_ProductID] HASH ([ProductID]) WITH ( BUCKET_COUNT = 8),
[SpecialOfferID] [int] NOT NULL
INDEX [IX_SpecialOfferID] NONCLUSTERED ,
[LocalID] [int] NOT NULL
)
WITH ( MEMORY_OPTIMIZED = ON )