Schema "tmp" doesn't exist inside SQL generated dump file? - sql

I'm trying to import my SQL dump to psql but there is a problem (as mentioned in the title). I'm not to sure why this happens, and what to do against it.
This is the command I am using
psql -U postgres -d app -1 -f postgres.sql
This is the error in cmd
C:\Users\Admin\Documents> psql -U postgres -d app -1 -f postgres.sql
Password for user postgres:
psql:postgres.sql:3: ERROR: schema "tmp" does not exist
and this is my SQL file
DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;
CREATE TABLE users (
id INTEGER NOT NULL,
email VARCHAR(120),
PRIMARY KEY (id),
UNIQUE (email)
);
INSERT INTO users VALUES(1,'KimFake1#outlook.com');
INSERT INTO users VALUES(2,'osman.narnia#live.co.uk');
INSERT INTO users VALUES(3,'KimFake1#outlook.com');
INSERT INTO users VALUES(4,'jaakume#gmail.com');
INSERT INTO users VALUES(5,'omarblack#protonmail.com');
INSERT INTO users VALUES(6,'osman.everton#hotmail.com');
INSERT INTO users VALUES(7,'radoslaw#ganczarek.in');
INSERT INTO users VALUES(8,'kane1001#live.co.uk');
INSERT INTO users VALUES(9,'osman.soloking009#outlook.com');
INSERT INTO users VALUES(10,'Shum1945#fleckens.hu');
CREATE TABLE raffle (
id INTEGER NOT NULL,
user_id INTEGER,
colour VARCHAR(120),
up1 VARCHAR(4),
up2 VARCHAR(4),
PRIMARY KEY (id),
CONSTRAINT _color_up1_up2_uc UNIQUE (colour, up1, up2),
FOREIGN KEY(user_id) REFERENCES users (id)
);
INSERT INTO raffle VALUES(1,1,'Blue','7c4c','5c7e');
INSERT INTO raffle VALUES(2,1,'Pink','635d','853f');
INSERT INTO raffle VALUES(3,1,'Plum','5e80','7611');
INSERT INTO raffle VALUES(4,1,'Aqua','937c','1b75');
INSERT INTO raffle VALUES(5,2,'Navy','1d9a','8914');
INSERT INTO raffle VALUES(6,1,'Grey','d869','fc97');
INSERT INTO raffle VALUES(7,4,'Rose','5fee','b31f');
INSERT INTO raffle VALUES(8,1,'Ruby','d5b4','e749');
INSERT INTO raffle VALUES(9,2,'Teal','cf0b','3bf5');
INSERT INTO raffle VALUES(10,1,'Gold','98a7','3079');
INSERT INTO raffle VALUES(11,1,'Jade','5c69','66f8');
INSERT INTO raffle VALUES(12,1,'Lime','156f','6b34');
INSERT INTO raffle VALUES(13,1,'Blue','7da3','d95b');
INSERT INTO raffle VALUES(14,1,'Pink','a63e','b9b6');
INSERT INTO raffle VALUES(15,1,'Plum','d989','71a5');
INSERT INTO raffle VALUES(16,1,'Aqua','7372','0682');
/****
INSERT INTO raffle VALUES(17,9,'Navy','01b3','e444');
INSERT INTO raffle VALUES(18,1,'Grey','d679','0123');
INSERT INTO raffle VALUES(19,1,'Rose','5963','692d');
...
***/
COMMIT;

Related

writing trigger containing multiple conditions

So I have a table trans which has two columns tx_type and ref_nbr
and I want to create a trigger such that the trigger ensures the following condition
in the trans table.
The following two conditions should be ensured:
if tx_type = D or W then ref_nbr should match the branch_nbr in branch table
if tx_type= B , P or R then ref_nbr should match mer_nbr in mer table
Triggers are not intended for keeping consistency in database relations. Use foreign keys for that. So make a table trans not with one column ref_nbr but use 2 columns - one for each relation (foreign key). Additionaly you can create check constraint for making sure that correct column is filled for given tx_type.
If you try to use triggers, you will have problems with concurrent transactions changing related tables like deleting your ref_nbr.
Example definitions for mer, branch and trans tables with some sample inserts:
create table branch(
branch_nbr number generated by default on null as identity start with 3 primary key,
branch_name varchar2(100) not null
);
create table mer (
mer_nbr number generated by default on null as identity start with 2 primary key,
mer_name varchar2(100) not null
);
create table trans (
id number generated by default on null as identity primary key,
tx_type varchar2(1) not null,
ref_branch_nbr number,
ref_mer_nbr number,
constraint ck_tx_type check (tx_type in ('D', 'W', 'B', 'P', 'R')),
constraint ck_correct_ref_for_tx_type
check (
(tx_type in ('D', 'W') and ref_branch_nbr is not NULL and ref_mer_nbr is NULL)
or (tx_type in ('B', 'P', 'R') and ref_branch_nbr is NULL and ref_mer_nbr is not NULL)
),
constraint fk_trans_ref_branch_nbr
foreign key (ref_branch_nbr)
references branch(branch_nbr),
constraint fk_trans_ref_mer_nbr
foreign key (ref_mer_nbr)
references mer(mer_nbr)
);
insert into branch(branch_nbr, branch_name) values(1, 'Master');
insert into branch(branch_nbr, branch_name) values(2, 'Test');
insert into mer(mer_nbr, mer_name) values(1, 'Test to Master');
commit;
-- working:
insert into trans(tx_type, ref_mer_nbr) values('P', 1);
insert into trans(tx_type, ref_branch_nbr) values('D', 1);
-- not working - non existing parent:
insert into trans(tx_type, ref_mer_nbr) values('P', 999);
insert into trans(tx_type, ref_branch_nbr) values('D', 999);
-- not working - wrong tx_type or wrong ref column:
insert into trans(tx_type, ref_mer_nbr) values('D', 1);
insert into trans(tx_type, ref_branch_nbr) values('P', 1);
insert into trans(tx_type, ref_branch_nbr, ref_mer_nbr ) values('P', 1, 1);
-- not working - cant insert without tx_type
insert into trans(ref_mer_nbr, ref_branch_nbr) values(1, 1);

I can not open my SQL code on Postgres and no error message was returned

As the title, I was unable to open my SQL code in Postgres, here are my codes:
DROP view ChampionList CASCADE;
CREATE TABLE ChampionList (
ChampID integer,
ChampName TEXT,
CONSTRAINT ChampionList_PK PRIMARY KEY (ChampID),
);
INSERT INTO ChampionList VALUES (1, 'Volibear');
INSERT INTO ChampionList VALUES (2, 'Lux');
INSERT INTO ChampionList VALUES (3, 'Caitlyn');
INSERT INTO ChampionList VALUES (4, 'Vel Koz');
I have tried multiple times but still can not open the table. Postgres also did not respond with an error message. I googled but was unable to figure out why. Here are my attempts:

Check if many-to-many relationship exists before insert or delete

I have 3 tables
For example:
Book
id
title
Tag
id
name
BookTag
book_id
tag_id
The goal to disallow having Book without Tag. i.e. when I try insert/delete data I need something to check on database level that Book has at least one Tag through many-to-many. If such validation fails it should throw constaint violation error or some sort of that. How should I implement that? Can it be reached by check constraint or should I create some trigger, if so then how?
please help me. thanks for your help in advance
You can enforce this at the pure database level by adding a foreign key in the book table that points back to a tag (any tag) in the book_tag table. As of now, your database model looks like:
create table book (
id int primary key not null,
title varchar(50)
);
create table tag (
id int primary key not null,
name varchar(50)
);
create table book_tag (
book_id int not null,
book_tag int not null,
primary key (book_id, book_tag)
);
Now, add the extra foreign key that points back to a tag:
alter table book add column a_tag int not null;
alter table book add constraint fk1 foreign key (id, a_tag)
references book_tag (book_id, tag_id) deferrable initially deferred;
Now when you insert a book, it can temporarily not have a tag, but only while the transaction hasn't finished yet. You need to insert a tag before committing. If you don't the constraint will fail, the transaction will rollback, and the insert won't happen.
Note: Please notice that this requires the use of deferrable constraints (look at deferrable initially deferred), something that is part of the SQL Standard but seldomly implemented. Fortunately, PostgreSQL does.
EDIT - Adding an example
Considering the previous modified tables you can try inserting a book without tags (will fail) and with tags (succeeding) as shown below:
insert into tag (id, name) values (10, 'classic');
insert into tag (id, name) values (12, 'action');
insert into tag (id, name) values (13, 'science fiction');
-- begin transaction
insert into book (id, title, a_tag) values (1, 'Moby Dick', 123);
commit; -- fails
-- begin transaction
insert into book (id, title, a_tag) values (2, 'Frankenstein', 456);
insert into book_tag (book_id, book_tag) values (2, 10);
insert into book_tag (book_id, book_tag) values (2, 13);
update book set a_tag = 10;
commit; -- succeeds

psql error for restoring pgsl backup on cmd

I'm having a hard time understanding what this error means. The command I used was:
psql -U postgres -d app -1 -f postgres.sql
and this is the error:
psql:postgres.sql:1879: ERROR: current transaction is aborted, commands ignored
until end of transaction block
ROLLBACK
psql:postgres.sql:0: WARNING: there is no transaction in progress
Not really sure how to make a transaction in progress. This is the sql file that I was trying to import to postgresl: http://pastebin.com/2xMGhstd
As joop explained, your SQL file is inconsistent.
There is a foreign key constraint from raffle.user_id to "user".id, which means that for every value in raffle.user_id there must be a row in "user" where id has the same value.
Now there is no row inserted in "user" with an id equal to 1, but the script attempts to insert a row in raffle with user_id equal to 1.
That violates the foreign key constraint an causes an error. Once there has been an error in a PostgreSQL transaction, all you can do is ROLLBACK. Until you do that, all statements in the transaction will fail with the error you observe.
The only solutions you have are either to fix the data so that they are consistent or to give up consistency by removing the foreign key constraint.
Remark: it is a bad idea to choose a reserved SQL keyword like USER as name.
As #joop commented: your .dmp file is crippled because user_id=1 does not exist in the users table, but is referred in the raffle table. Example: run the next snippet, then uncomment the line with user#1 on it and run it again.
-- Drop the schema *after* usage
-- DROP SCHEMA tmp CASCADE;
CREATE SCHEMA tmp ;
SET search_path=tmp;
CREATE TABLE users (
id INTEGER NOT NULL,
email VARCHAR(120),
PRIMARY KEY (id),
UNIQUE (email)
);
-- UNCOMMENT the next line to also add user#1
-- INSERT INTO users VALUES(1,'Jim.Fake#nolive.co.uk');
INSERT INTO users VALUES(2,'osman.narnia#live.co.uk');
INSERT INTO users VALUES(3,'KimFake1#outlook.com');
INSERT INTO users VALUES(4,'jaakume#gmail.com');
INSERT INTO users VALUES(5,'omarblack#protonmail.com');
INSERT INTO users VALUES(6,'osman.everton#hotmail.com');
INSERT INTO users VALUES(7,'radoslaw#ganczarek.in');
INSERT INTO users VALUES(8,'kane1001#live.co.uk');
INSERT INTO users VALUES(9,'osman.soloking009#outlook.com');
INSERT INTO users VALUES(10,'Shum1945#fleckens.hu');
CREATE TABLE raffle (
id INTEGER NOT NULL,
user_id INTEGER,
colour VARCHAR(120),
up1 VARCHAR(4),
up2 VARCHAR(4),
PRIMARY KEY (id),
CONSTRAINT _color_up1_up2_uc UNIQUE (colour, up1, up2),
FOREIGN KEY(user_id) REFERENCES users (id)
);
INSERT INTO raffle VALUES(1,1,'Blue','7c4c','5c7e');
INSERT INTO raffle VALUES(2,1,'Pink','635d','853f');
INSERT INTO raffle VALUES(3,1,'Plum','5e80','7611');
INSERT INTO raffle VALUES(4,1,'Aqua','937c','1b75');
INSERT INTO raffle VALUES(5,2,'Navy','1d9a','8914');
INSERT INTO raffle VALUES(6,1,'Grey','d869','fc97');
INSERT INTO raffle VALUES(7,4,'Rose','5fee','b31f');
INSERT INTO raffle VALUES(8,1,'Ruby','d5b4','e749');
INSERT INTO raffle VALUES(9,2,'Teal','cf0b','3bf5');
INSERT INTO raffle VALUES(10,1,'Gold','98a7','3079');
INSERT INTO raffle VALUES(11,1,'Jade','5c69','66f8');
INSERT INTO raffle VALUES(12,1,'Lime','156f','6b34');
INSERT INTO raffle VALUES(13,1,'Blue','7da3','d95b');
INSERT INTO raffle VALUES(14,1,'Pink','a63e','b9b6');
INSERT INTO raffle VALUES(15,1,'Plum','d989','71a5');
INSERT INTO raffle VALUES(16,1,'Aqua','7372','0682');
/****
INSERT INTO raffle VALUES(17,9,'Navy','01b3','e444');
INSERT INTO raffle VALUES(18,1,'Grey','d679','0123');
INSERT INTO raffle VALUES(19,1,'Rose','5963','692d');
...
***/
COMMIT;

Variables in PostgreSQL with UUID

All my tables use UUID primary keys. I want to fill tables with test data in one sql file, but can't use UUID PK as FK in other tables, because default function uuid_generate_v4() generates a random value during execution.
Example what I want:
SET Uservar = uuid_generate_v4();
SET Postvar = uuid_generate_v4();
INSERT INTO public."User" (id, name) VALUES (Uservar, 'Foo Bar');
INSERT INTO public."Post" (id, content, userId) VALUES (Postvar, 'Test message', Uservar)
How to do this? Or how to select already created UUID and store for next insert?
E.g. say you had table like this:
create table my_table(uuid_column uuid PRIMARY KEY NOT NULL);
You can insert a variablized uuid like so:
DO $$
DECLARE
my_uuid uuid = uuid_generate_v4();
BEGIN
insert into my_table (uuid_column) values (my_uuid);
select * from my_table where uuid_column = my_uuid;
END $$;
Check this documentation.
N.B. To have uuid_generate_v4() available, make sure you have the below snipped ran before your use it:
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
When you combine both INSERTs into a single statement you can re-use the uuid values quite easily:
WITH newUser(id uuid) AS (
INSERT INTO public."User" (id, name)
VALUES (uuid_generate_v4(), 'Foo Bar')
RETURNING id
)
INSERT INTO public."Post" (id, content, userId)
SELECT uuid_generate_v4(), 'Test message', id
FROM newUser;
When you want to add a post for an existing user, you can use a very similar approach:
INSERT INTO public."Post" (id, content, userId)
SELECT uuid_generate_v4(), 'Test message', id
FROM public."User"
WHERE name = 'Foo Bar';
This would also work when the PK's are auto-generated (i.e. id uuid PRIMARY KEY DEFAULT uuid_generate_v4()) but then you would not explicitly include the PK columns in the INSERT statements.
As I cannot comment
it should be
DO $$
DECLARE
my_uuid uuid := uuid_generate_v4();
BEGIN
insert into my_table (uuid_column) values (my_uuid);
select * from my_table where uuid_column = my_uuid;
END $$;