How to dump individual view statements in MySQL - view

I want to be able to dump individual view statements/definitions from a MySQL database (version 5.5.28) so I can edit them and insert them into a new database. I've been trying to do it with MySQL Workbench (version 5.2.44 on OSX) but it dumps the views as 'CREATE Table' statements without the view logic.
I'm looking for something to analyze a db, give an options for views to dump, and dumps 'CREATE VIEW...' statements to a file to re-create those views.

MySQL has a SHOW CREATE VIEW statement that should do what you need.
Example usage, assuming you view's name is sampleView:
SHOW CREATE VIEW sampleView;
As a note, your user will need the SHOW VIEW and the SELECT privileges to be able to use the SHOW CREATE VIEW command.
An alternative, you can also get this information from the information_schema.VIEWS table.
Using the sampleView name again, the query would look something like:
SELECT
VIEW_DEFINITION
FROM
INFORMATION_SCHEMA.VIEWS
WHERE
TABLE_NAME = 'sampleView';

Related

How to get the CREATE query of an existing materialized view in Hive?

I want to extract the entire create query of a materialized view present in Hive. How can I do that? I have the underlying query for data but I want the create query to include storage characteristics, etc.
Two options -
use describe formatted mv_name anad then refer to the 'view original text' line for view SQL.
xx is the name of materialized view
if you are using hue, then you can use table browser to check MV definition/SQL used to create it.
Here is a screenshot on how to get the SQL - xx is the name of materialized view. Create statement below.
CREATE MATERIALIZED VIEW xx AS SELECT * FROM activity;
2.1 Click on 'i' and then click on 'Table Browser'.
2.2 Click on 'view sql' to see view query.

ORA-01436: CONNECT BY loop in user data in Toad Used by Tab for tables

As you know in TOAD, we can see where a table is used (in package or in view).
But when i try to see where my table is used it is showing the error :ORA-01436: CONNECT BY loop in user data.
Other tables are showing up their used status nicely.
Any help??enter image description here
Alternatively you can use ALL_SOURCE to check where your table has been referred in the code.

Table and Proc dependency

I am new to schema and not sure how the table is being populated (How the data is being inserted into the table ). How can we find out ?
This should work.
select *
from dba_source
where upper(text) like '%TABLE_NAME%'
But as I do not have DBA rights , can not execute this command. What is the other way to find this out ?
To see dependencies between objects you have access to you can query the all_dependencies data dictionary view. In this case:
select * from all_dependencies where referenced_name = 'YOUR_TABLE_NAME';
If the objects are in your own schema when you can use the user_dependencies view. If you want to see objects you don't have privileges against then you can use dba_dependencies, but it sounds like you are unlikely to have the privileges required to query that view, since you can't see dba_source.
Of course, that will only identify references within your stored PL/SQL code; it won't tell you about any external application code that is performing inserts directly against the database (as opposed to via CRUD procedures) or manual inserts.
And it will only tell you which objects have dependencies, you'll still need to dig through the object source code, either by querying all_source (or user_source if you're the owner) for the relevant type and name. I would avoid the possibility of false-positives from, say, comments that happen to mention the table name in code which doesn't access it. You could also do that outside the database - hopefully your code is under source control (right!?).
If you know the query you need to run but do not have the necessary privileges then perhaps you can write the query using USER_ or ALL_ views to validate the syntax then change the view to DBA_ and ask the DBA to run the query for you.

How can I create a table with same attributes of a View another system delivers me?

An external system granted to my system a view named V_EXT so that I can make selections on it, reading all its content:
SELECT *
FROM V_EXT;
this view has a lot of fields and I would like to create an empty table in my system with the exactly same attributes of this view (same names and same types). Is there a way to do this without simply guessing from the content I received what each attribute is?
I am using Oracle SQL Developer.
With Code.
create table objects_copy2
as
select *
from all_objects
where 1=2; -- add this line if you want NO data, otherwise you get all the data too
With SQL Developer specifically, it's actually harder. You would need to find the underlying OBJECT(s) used in the query. Then look up those data types, and manually build out your CREATE TABLE statement.
CREATE TABLE AS SELECT is the way to go. (Docs)
Note there are some limitations, for example this won't pick up Identity Column definitions from the source table used in the view.
An example:

How to create a table identical to other table in structure and constraints in Oracle?

I want to create a table (lets say table_copy) which has same columns as other table (lets call it table_original) in Oracle database, so the query will be like this :
create table table_copy as (select * from table_original where 1=0);
This will create a table, but the constraints of table_original are not copied to table_copy, so what should be done in this case?
Only NOT NULL constraints are copied using Create Table As Syntax (CTAS). Others should be created manually.
You might however query data dictionary view to see the definitions of constraints and implement them on your new table using PL/SQL.
The other tool that might be helpful is Oracle Data Pump. You could import the table using REMAP_TABLE option specifying the name for the new table.
Use a database tool to extract the DDL needed for the constraints (SQL Developer does the job). Edit the resulting script to match the name of the new class.
Execute the script.
If you need to do this programmatically you can use a statement like this:
DBMS_METADATA.GET_DDL('TABLE','PERSON') from DUAL;

Resources