object name already exists: PUBLIC in HSQLDB - hsqldb

I have an HSQLDB database (script/log) that I want to read into an In-Memory database. This script has near its top:
CREATE SCHEMA PUBLIC AUTHORIZATION DBA;
which leads to an error. So I tried executing that manually and I don't understand the result. Here's what I did:
Why do I get object name already exists: PUBLIC / Error Code: -5504 / State: 42504?
Did I not drop the schema correctly or why am I unable to create it?

The PUBLIC schema exists in all new databases. When you drop the PUBLIC schema, an empty version gets recreated automatically. Therefore, you do not need to create the PUBLIC schema.

Related

Public synonym_OracleSQL

I have created a public synonym in my code, however, I am unable to see it in the all_synonyms or user_synonyms views just to make sure the synonym has created and existed in my database. Help me guys?! Thanks in advance...
I have tried to look in the user_synonyms and all_synonyms or dba_synonyms vies, still unable to find it.
create public synonym EBS_PS as select * from EBS;
(Synonym created)
I should see the public synonym EBS_PS should be stored in a system view.
Your statement is not a valid Oracle statement:
SQL> create public synonym EBS_PS as select * from EBS;
create public synonym EBS_PS as select * from EBS
*
ERROR at line 1:
ORA-00905: missing keyword
To create the synonym you want you do:
create public synonym EBS_PS for EBS;
This will show up in the all_synonyms view.
On the future, I can advice that you should use "or replace" keyword. You can save time for solving the next errors.
create or replace public synonym EBS_PS for EBS;

PosgreSQL - ERROR: relation "table_name" does not exist. How to query without schema name?

I am migrating from oracle to postgreSQL.
In my application I have a lot of queries like this
ResultSet resultSet = statement.executeQuery("SELECT NAME FROM Table_name");
But I am failing with exception
ERROR: relation "table_name" does not exist.
As far as I understood I need also provide schema name, and when I am performing like this, it works:
ResultSet resultSet = statement.executeQuery("SELECT NAME FROM schema_name.Table_name");
So basically my question is - How could I avoid refactor all queries?
You want to put the tables in Postgres into the public schema (the default) or use a search path.
The documentation explains this.
searchpath defines the order and list of schemas to be search for unqualified names. By default it is "$user", public so first user schema and then public is looked to.
You can change this order via:
SET search_path TO schema_name,public;
More details on this are here.
You can configure this after connection is open for example.
Alternatively you can set this for user once:
ALTER ROLE username SET search_path = schema_name,public;
This command, as mentioned above, will set search path for current session
SET search_path TO schema_name,public;
However if there is need to do it for all session, just correct config file - pgsql/11/data/postgresql.conf add search path
search_path = '"$user", YOUR_SCHEMA'
Restart the DB.

Explicitly refer to public DBLink when schema owns a private one of same name?

I'm working a migration project requiring an import of several Oracle database schema onto an existing database. This requirement has brought about an interesting conflict where I now have two dblinks with the same name:
One is a private dblink which uses account A to access the Foobar database
The other is a public dblink to the same Foobar database which uses account B for its access
Global Names is set to true so I cannot change the names of these dblinks.
I've already figured out through trial and error that when signed into schema that owns the private dblink that the following:
SELECT *
FROM table#foobar;
will refer to the private dblink and not the public one. But for situations where I require the account B privileges, I cannot figure out how to explicitly refer to the public dblink.
Does anyone know of syntax I can use to refer to the public #foobar?
From Oracle documentation.
Oracle first searches for a private database link in your own schema with the same name as the database link in the statement. Then, if necessary, it searches for a public database link with the same name.
I don't think this can be changed in any way. Not that I know of or found in documentation. You could create public synonym but that will work only if you need to access with B specific objects. Synonym can't be created for whole database link.
Wouldn't it be easier to turn global names to False on session level and create new link to B with otherwise invalid link name. If you change global names on session level only that session will be allowed to use new link.

can we create synonym with the same name in same schema

I'm preparing for SQL Expert certification, and I found one question and It said,which is the correct option. And, out of four, one option said A table and a synonym can have the same name in the same schema.
And, per my knowledge, in oracle anything we create that treated as an object, which means when we say create synonym, which means we are creating new object. And create same object in same schema not allowed in Oracle or any database AFAIK.
Even Burleson says
You can have a public and private synonym of the same name. In fact,
you can have a public and private synonym called EMP in the SCOTT
schema and have a table called EMP in the same schema
So, I tried.
create synonym emp for scott.emp
It shows some error object already exist
Then I tried
create public synonym emp for scott.emp.
And, got same error. So, anyone please share some knowledge on Synonyms. Can we create synonyms with same name in same schema ?
You can have:
Table and Public Synonym with the same name
Public Synonym and Private Synonym with the same name
But can not have:
Table and Private Synonym with the same name inside the same schema
The first thing to note is that Public Synonyms are non-schema objects, while Private Synonyms and Tables are. Another is that the uniqueness of database objects' names are defined by the namespace. As it is stated in SQL Expert Study Guide:
USER, ROLE, and PUBLIC SYNONYM objects are in their own collective namespace.
TABLE, VIEW, SEQUENCE, PRIVATE SYNONYM, and user-defined TYPE objects have their own unique namespace within a given schema.
INDEX objects have their own namespace within a given schema.
CONSTRAINT objects have their own namespace within a given schema.
So, as long as objects do not share the same namespace you can give them the same names.
Hope this helps.
Public synonyms are non-schema objects, when private synonyms as tables are schema objects.
USER, ROLE and PUBLIC SYNONYM are in their own collective namespace.
TABLE, VIEW, SEQUENCE, PRIVATE SYNONYM have their own unique namespace.
An INDEX has their own unique namespace.
A CONSTRAINT object has their own unique namespace within a given schema
While the objects don't share the same namespace, you can give them the same names.
Remember, that you can have:
a table and public synonym with the same name
a public synonym and a private synonym with same name
You cannot have:
a table and a private synonym with the same name inside the same schema.

Purge postgresql database as it was new

I have created a PostgreSQL database dump using psql.
Now I want to restore this backup from the file:
psql -d thesamename -f /my/backup/file
But I get errors that the data already exists.
Is there any command to delete everything from the database to bring it to just created state, except dropping and creating once again?
(I don't want to set up owner, tablescpace etc. once again)
Maybe some way to overwrite the database with the one from the backup file? (the backup file is from another database server)
https://www.postgresql.org/docs/current/static/app-pgdump.html
https://www.postgresql.org/docs/current/static/app-pgrestore.html
Have a look specifically at the -c option for these scripts.
You can drop the public schema of your database and re create it after :
DROP SCHEMA PUBLIC CASCADE;
CREATE SCHEMA PUBLIC AUTHORIZATION postgres;
GRANT ALL ON SCHEMA public TO public;
Hope it helps…
you can manipulate it as you want..and you want only dumping of data.once you will start to dumping data schema file will generate automatically.and no need to drop old data.it will overlap with new data over existing data.
You can go through this link for more clarification..
http://www.postgresql.org/docs/8.4/interactive/app-pgdump.html
http://www.postgresql.org/docs/8.4/interactive/app-pgrestore.html
You can use
GRANT ALL ON SCHEMA public TO public;