PostgreSQL: foreign table and discarded records - postgresql

where PostgreSQL stores records which were discarded from the foreign table during select? I have following table:
CREATE FOREIGN TABLE ext.alternatenamesext (
altid BIGINT,
geoid BIGINT,
isolanguage VARCHAR(7),
alternatename TEXT,
ispreferredname INTEGER,
isshortname INTEGER,
iscolloquial INTEGER,
ishistoric INTEGER
)
SERVER edrive_server
OPTIONS (
delimiter E'\t',
encoding 'UTF-8',
filename '/mnt/storage/edrive/data/alternateNames.txt',
format 'csv');
alternateNames.txt contains ~11 mln records. But when I do "SELECT * FROM ext.alternatenamesext" it returns only ~9.5mln records. Where the rest of 2mln are? Is there a way to put them into the separate file, like Oracle's sql*ldr?

Problem has been solved by the following syntax of CREATE FOREIGN TABLE...:
CREATE FOREIGN TABLE ext.alternatenamesext (
altid BIGINT,
geoid BIGINT,
isolanguage VARCHAR(7),
alternatename VARCHAR(400),
isPreferredName INT,
isShortName INT,
isColloquial INT,
isHistoric INT
)
SERVER edrive_server
OPTIONS (
delimiter E'\t',
encoding 'UTF-8',
filename '/mnt/storage/edrive/data/alternateNames.txt',
format 'text', -- not 'csv'!
null ''); -- eliminate null values (some kind of TRAILING NULLCOLLS in Oracle I guess)

Related

POSTGRESQL: inserting multiple values in second table using uuid from first insert

Tables
CREATE TABLE “transactions” (
“uuid” uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
“user_uuid” uuid,
“chain_uuid” uuid,
“block_number” int,
“timestamp” int,
“hash” text,
“from” text,
“to” text,
“address” text,
“value” numeric,
“value_fiat” numeric
“fees” numeric,
“fees_fiat” numeric,
“is_error” int,
“input” text,
“protocol” text,
“type” text,
“reviewed_status”,
“is_manual” boolean,
“is_transferring_native_asset” boolean
);
CREATE TABLE “transaction_assets” (
“uuid” uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
“transaction_uuid” uuid,
“asset_uuid” uuid,
“type” text,
“is_assetin”, boolean
“balance” numeric,
“price” numeric,
“value” numeric
--CONSTRAINT transaction_assets_pkey PRIMARY KEY (uuid)
);
My Query
Begin;
with tx as (
INSERT INTO
transactions (user_uuid, chain_uuid, ..blah blah blah)
VALUES($ 1, $ 2, $ 3,.... $ 16 ) RETURNING uuid)
INSERT INTO
transaction_assets (transaction_uuid, asset_uuid, type, is_assetin, balance, price,value
)
VALUES
((SELECT tx.uuid FROM tx),$ 17,$ 18,$ 19,$ 20,$ 21),
((SELECT tx.uuid FROM tx),$ 22,$ 23,$ 24,$ 25,$ 26);
// repeat with many transactions
Commit;
I am using pg-promise. I have an array of transaction objects in javascript, each transaction has an array of assets associated with it. I would like to insert the transactions into the transaction table and the assets associated with that transaction into the transaction_assets table, using the transaction UUID from the first insert. I have many transactions to insert. which is why I am using the begin/commit. I would like to insert many transactions at the same time but if it fails be able to role back all of them
my current query is getting the error
syntax error at or near "from"

Redshift - inserting into identity column

I have created a table as mentioned below.
create table employee (
surrogate_key bigint IDENTITY(1,1),
first_name varchar(200),
last_name varchar(200),
phone_number varchar(200),
creditcard_number bigint
)
insert into employee values
('gaurang', 'shah', '356-776-4456', '4716973408090483')
However, following code is giving error.
Error
[Code: 500310, SQL State: 0A000] [Amazon](500310) Invalid operation: cannot set an identity column to a value;
insert into employee(first_name,last_name,phone_number,creditcard_number)
values('gaurang', 'shah', '356-776-4456', 4716973408090483)
You have to specify column names when identity column present in the table
One more option is define surrogate key with default like this
surrogate_key bigint generated by default as IDENTITY(1,1),
Then run this query
insert into employee1
values(default,'gaurang', 'shah', '356-776-4456', 4716973408090483)

How to automatically fill columns on a row based on the foreign key being the primary key on another table

I'm working on a sql database for QGIS. I have 8 tables: 3 three of them are the base tables and the others are relational tables. One relational table,
"tabela_is_po", has 4 fields:
indice_sequencial_po (integer and Primary Key),
tema (character varying),
subtema (character varying),
designacao (character varying)
And the base table, "tabela_objecto_area", has 7 fields:
identificador integer ,
dtcc character varying(4),
planta_po boolean,
indice_sequencial_po integer (foreign key),
tema_po character varying(254),
subtema_po character varying(254),
designacao_po character varying(254)
In "tabela_objecto area", when I fill, indice_sequencial_po integer (foreign key), I want the table to be able to automatically get the data from the relational table "tabela_is_po", based on the number that is filled in indice_sequencial_po.
I've tried the trigger function but it never worked well.
tabela_objecto_area code:
CREATE TABLE pdm2019.tabela_objecto_area
(
identificador integer NOT NULL DEFAULT nextval('pdm2019.tabela_objecto_area_identificador_seq'::regclass),
dtcc character varying(4) NOT NULL DEFAULT '0101',
planta_po boolean NOT NULL,
indice_sequencial_po integer,
tema_po character varying(254),
subtema_po character varying(254),
designacao_po character varying(254),
CONSTRAINT tabela_objecto_area_pkey PRIMARY KEY (identificador)
)
WITH (
OIDS=FALSE
);
Trigger function:
CREATE OR REPLACE FUNCTION tema_e_subtema_automatico() RETURNS TRIGGER AS $tema_e_subtema_automatico$
BEGIN
IF NEW.pdm2019.tabela_objecto_area.indice_sequencial_po IS NOT NULL THEN
INSERT INTO pdm2019.tabela_objecto_area(tema_po,subtema_po,designacao_po) SELECT tema,subtema,designacao FROM pdm2019.tabela_is_po WHERE indice_sequencial_po = NEW.pdm2019.tabela_objecto_area.indice_sequencial_po;
END IF;
END;
$tema_e_subtema_automatico$ LANGUAGE plpgsql;
CREATE TRIGGER tema_e_subtema_automatico AFTER INSERT OR UPDATE ON pdm2019.tabela_objecto_area
FOR EACH ROW EXECUTE PROCEDURE tema_e_subtema_automatico();
You would probably be better off using a view. See https://en.wikipedia.org/wiki/Relational_model for an overview, basically each item should appear only once in the database.
For data entry, it's often easier to use a framework like django (https://www.djangoproject.com/) to create the structure of your tables and input data, which you can then view in qgis.
CREATE TABLE pdm2019.tabela_objecto_area
(
identificador integer NOT NULL DEFAULT nextval('pdm2019.tabela_objecto_area_identificador_seq'::regclass),
dtcc character varying(4) NOT NULL DEFAULT '0101',
planta_po boolean NOT NULL,
indice_sequencial_po integer,
-- tema_po character varying(254), -- use a view to populate these
-- subtema_po character varying(254), -- use a view to populate these
-- designacao_po character varying(254), -- use a view to populate these
CONSTRAINT tabela_objecto_area_pkey PRIMARY KEY (identificador)
)
WITH (
OIDS=FALSE
);
CREATE VIEW v_tabela_objecto_area as (
select a.*, b.tema_po, b.subtema_po, b.designacao_po
from tabela_objecto_area a, tabela_is_po b
where a.indice_sequencial_po = b.indice_sequencial_po);

Access database, Sql query , Error "Syntax error in DROP TABLE or DROP INDEX."

This is the query , running this in C#.
n getting above error
"DROP TABLE IF EXISTS `NATIONAL_ID_ISSUANCE_CENTER`;
CREATE TABLE `NATIONAL_ID_ISSUANCE_CENTER` (
`ID` INTEGER NOT NULL AUTO_INCREMENT,
`NAME` VARCHAR(100),
`APPLICATION_ID` INTEGER,
`STATUS` INTEGER,
`CREATED_BY` INTEGER,
`UPDATED_BY` INTEGER,
`CREATED_DATE` DATETIME,
`UPDATED_DATE` DATETIME,
`THIRD_PARTY_ID` INTEGER,
`PROVINCE_ID` INTEGER,
INDEX (`APPLICATION_ID`),
PRIMARY KEY (`ID`),
INDEX (`PROVINCE_ID`),
INDEX (`THIRD_PARTY_ID`)
)"
You can't put an IF statement inside Drop and Create statements. Anytime you want to drop a table that you're not sure exists, use the following:
IF(OBJECT_ID('[Database].[Schema].[TableName]') is not null)
BEGIN
DROP TABLE [Database].[Schema].[TableName];
END;
Please note you should replace [Database], [Schema], and [TableName] with the appropriate database, schema, and table names, respectively.

create tables from sql file on schema PostgreSQL

I try to create tables from file on my schema, but i receive error message. Users table created on public schema was successfully, but other table i created on test1 schema was wrong. I don't know why, help me please.
Thank for advance.
CREATE SCHEMA test1
--Create sequence
CREATE SEQUENCE test1.table_id_seq START 1;
--Create function to auto generate ID
CREATE OR REPLACE FUNCTION test1.next_id(OUT result bigint) AS $$
DECLARE
our_epoch bigint := 1314220021721;
seq_id bigint;
now_millis bigint;
shard_id int := 1;
BEGIN
SELECT nextval('test1.table_id_seq') % 1024 INTO seq_id;
SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000) INTO now_millis;
result := (now_millis - our_epoch) << 23;
result := result | (shard_id << 10);
result := result | (seq_id);
END;
$$ LANGUAGE PLPGSQL;
--Talbe ----users----
CREATE TABLE users
(
id bigserial NOT NULL PRIMARY KEY,
username varchar(30) NOT NULL UNIQUE,
password varchar NOT NULL,
first_name varchar(10),
last_name varchar(10),
profile_picture varchar,
create_time timestamp (0) without time zone
);
--Table ----photos----
CREATE TABLE test.photos
(
id bigint NOT NULL PRIMARY KEY DEFAULT test1.next_id(),
caption text,
low_resolution varchar,
hight_resolution varchar,
thumnail varchar,
user_id bigint NOT NULL REFERENCES users(id),
create_time timestamp (0) without time zone
--Table ----comments----
CREATE TABLE test1.comments
(
id bigint NOT NULL PRIMARY KEY DEFAULT test1.next_id(),
create_time timestamp (0) without time zone,
text text,
user_id bigint REFERENCES users(id),
photo_id bigint REFERENCES test1.photos(id)
);
--Table ----likes----
CREATE TABLE test1.likes
(
photo_id bigint REFERENCES test1.photos(id),
user_id bigint REFERENCES users(id),
);
--Table ----follows----
CREATE TABLE test1.follows
(
user_id bigint NOT NULL REFERENCES users(id),
target_id bigint NOT NULL REFERENCES users(id),
);
CREATE TABLE teset1.feeds
(
user_id bigint NOT NULL REFERENCES users(id),
photo_id bigint NOT NULL REFERENCES test1.photos(id),
create_time timestamp (0) without time zone,
);
Well, it would have been helpful for you to have shown us what errors you were getting and what you tried to do to fix them. But here are some obvious problems with the SQL you posted:
Missing semicolon after "CREATE SCHEMA test1 "
Missing closing paren and semicolon at the end of CREATE TABLE test.photos ...
Dump neglects to create the "test" schema where the "photos" table wants to go.
Several foreign key references of test1.photos.id, but "photos" was created in the "test" schema, not "test1".
Extraneous trailing commas after the last column definitions for several tables
Typo: "teset1.feeds", should be "test.feeds"
TL;DR Whoever created this dump file wasn't paying very close attention.
After fixing all the problems above, I managed to load your SQL.