How would I drop a schema and all of its contents, using SQL, in DB2 8.x without knowing what the content is?
I do not have a schema to drop at hand, but the infocenter (http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/admin/t0005230.htm) says that a DROP SCHEMA [schema name] shall work on DB2 8.x
EDIT: Ok, the Infocenter also says: "Before dropping a schema, all objects that were in that schema must be dropped themselves or moved to another schema"
So, need to drop all the objects in the schema first. The objects (tables, views, triggers, procedures, indexes...) can be listed quering the catalog views in SYSIBM schema.
E.g. to get all tables that belong to schema, run:
select table_name from sysibm.tables where table_schema = '[your schema name]'
Check the other sysibm views to get all objects that belong into a schema. the views are: sysibm.views, sysibm.triggers, sysibm.routines, sysibm.indexes, ... - consult IBM's Infocenter (the link above) for details.
use the comand centre-GUI to drop all contents of the schema, then DROP SCHEMA
Related
We had one of the devs create a foreign data wrapper with these commands:
CREATE SERVER serverName FOREIGN DATA WRAPPER postgres_fdw OPTIONS (xxxx);
CREATE USER MAPPING FOR user SERVER foreign_db OPTIONS (user 'xxxx', password 'xxxx');
CREATE SCHEMA foreign_db;
IMPORT FOREIGN SCHEMA public FROM SERVER serverName INTO foreign_db;
To drop this schema the suggestion was to run:
DROP SCHEMA if exists foreign_db cascade;
DROP USER mapping if exists for user server foreign_db;
DROP SERVER if exists serverName;
In the spec I see this for CASCADE:
Automatically drop objects (tables, functions, etc.) that are
contained in the schema, and in turn all objects that depend on those
objects
what concerns me is this line:
and in turn all objects that depend on those objects
My question is there a possibility of dropping anything outside of foreign_db schema and if yes, how can I check it?
Thank you.
It is possible that the command drops something outside the schema. Consider this:
create schema example;
create table example.my_table (id int);
create view public.my_view as select * from example.my_table;
If the schema is dropped with the cascade option, public.my_view will also be dropped. However, the behavior is logical and desirable.
You can check this executing these commands one by one:
begin;
drop schema example cascade;
rollback;
The schema will not be dropped and after drop... you should get something like this:
NOTICE: drop cascades to 2 other objects
DETAIL: drop cascades to table example.my_tabledrop cascades to view my_view
Alternatively, you can use the system catalog pg_depend, see this answer How to list tables affected by cascading delete.
How to create and use Synonyms on PostgreSQL as in Oracle. Do I need to create some DB link or any thing else. I could not find any good official doc on this topic.
Edit 1
Actually as of now i have an application which has two separate modules which connects with two different oracle databases; One modules need to access tables of other so for which we use synonyms over db link in oracle. Now we are migrating application to postgresql, so we need synonyms.
Edit 2
When i say two different oracle databases it means it can be two different oracle instances or two schemas of same db, it is configurable in application and application must support both modes.
PostgreSQL version: 9.6.3
Approach 1:-
Finally i got it working using foreign data wrapper postgres_fdw as below
I have two databases named dba and dbb. dbb has a table users and i need to access it in dba
CREATE SERVER myserver FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost', dbname 'dbb', port '5432');
CREATE USER MAPPING FOR postgres
SERVER myserver
OPTIONS (user 'user', password 'password');
CREATE FOREIGN TABLE users (
username char(1))
SERVER myserver
OPTIONS (schema_name 'public', table_name 'users');
CREATE FOREIGN TABLE users (users char(1));
Now i can execute all select/update queries in dba.
Approach 2:-
Can be achieved by creating two schemas in same db, below are the steps:
create two schemas ex app_schema, common_schema.
Grant access:
GRANT CREATE,USAGE ON SCHEMA app_schema TO myuser;
GRANT CREATE,USAGE ON SCHEMA common_schema TO myuser;
Now set search path of user as below
alter user myuser set search_path to app_schema,common_schema;
Now tables in common_schema will be visible to myuser. For example let say we have a table user in common_schema and table app in app_schema then below queries will be running easily:
select * from user;
select * from app;
This is similar to synonyms in oracle.
Note- Above queries will work PostgreSQL 9.5.3+
I think you don't need synonyms in Postgres the way you need them in Oracle because unlike Oracle there is a clear distinction between a user and a schema in Postgres. It's not a 1:1 relationship and multiple users can easily use multiple schemas without the need to fully qualify the objects by exploiting Postgres' "search path" feature - mydb.public.mytable.
If the tables are supposed to be in a different database in PostgreSQL as well, you'd create a foreign table using a foreign data wrapper.
If you used the Oracle synonym just to avoid having to write atable#dblink, you don't have to do anything in PostgreSQL, because foreign tables look and feel just like local tables in PostgreSQL.
If you use the synonym for some other purposes, you can either set search_path to include the schema where the target table is, or you can create a simple view that just selects everything from the target table.
How to drop a schema which has a an additional space in its name? When listing out the schemas it shows up as BABBLER GROUP. But when trying to drop (sys as sysdba) using drop user BABBLER GROUP CASCADE; it shows error.
Use quotes; e.g. drop user "BABBLER GROUP" CASCADE
The same is true for any object with a space in its name.
NB: Per commments here: https://stackoverflow.com/a/13798120/361842
Some tools aren't compatible with this feature; SQL Plus should be.
Oracle documentation on the same here: https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements008.htm
Is there a query in db2 9.7 control center wherein I can't DELETE(DROP) all the contents of my schema (including the schema) at once?
My other option is to drop/delete the objects first and then DROP schema..
But I want to DROP THE ENTIRE SCHEMA WITH ALL OBJECTS at once.
DROP SCHEMA <schema_name> CASCADE/RESTRICT didn't work for me.
The ADMIN_DROP_SCHEMA procedure is what you're looking for.
The ADMIN_DROP_SCHEMA procedure is used to drop a specific schema and all objects contained in it.
http://publib.boulder.ibm.com/infocenter/db2luw/v9/topic/com.ibm.db2.udb.admin.doc/doc/r0022036.htm
First drop all the tables in the schema.
Then try to delete the schema using
DROP SCHEMA SCHEMA_NAME RESTRICT
webchain.in have sample java program, explains how to delete the schema using java program
in case drop schema fails after dropping all the tables with the error SQLCODE=-551, SQLSTATE=42501, try command
grant dbadm on database to USER_NAME
Is there a standard method to retrieve the 'CREATE TABLE..' definition of a table in SQL Server?
I can ask INFORMATION_SCHEMA for definitions of views and functions/procedures, so I thought it would be natural to get the same for tables, but I didn't find anything.
Yes - INFORMATION_SCHEMA.TABLES should be what you're looking for.
It will give you something like:
aspnetdb dbo aspnet_Paths BASE TABLE
aspnetdb dbo aspnet_PersonalizationAllUsers BASE TABLE
aspnetdb dbo aspnet_PersonalizationPerUser BASE TABLE
aspnetdb dbo vw_aspnet_WebPartState_Paths VIEW
aspnetdb dbo vw_aspnet_WebPartState_Shared VIEW
aspnetdb dbo vw_aspnet_WebPartState_User VIEW
aspnetdb dbo aspnet_Applications BASE TABLE
If you want to know about the columns of a table, look at INFORMATION_SCHEMA.COLUMNS.
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'aspnet_Users'
will give you all the details for the columns for the table specified.
Using INFORMATION_SCHEMA for views and code will fail. The data is limited to nvarchar(4000), so longer stuff will not be read. Use sys.sql_modules or OBJECT_DEFINITION.
For tables, it's more difficult. A "table" consists of columns, constraints, indexes, possibly rules, UDTs (did I forget anything?). This is why SSMS has so many table scripting options compared to a view or stored proc.
I'd suggest profiling SSMS and hope it doesn't use SMO... or use SMO via CLR code or even xp_cmdshell.
You can use SMO (Microsoft.SqlServer.Smo) from .NET to script the table.