How Can I Easily Document a Database Environment? - sql

HI Frdz, i have around 40 tables in database and i have created a document with all of these table names, fields, datatype, length of datatype, NUll or NOT NULL. but when i was working on tables in database i had to change the table field names, datatype and length of datatype as well. now as i am done with all my coding i have to have that document matching with what i have in database. so do u think is there any easy way i can do this or do i have to manually go through each table and update the doc?
Thanks

You can usually query the INFORMATION_SCHEMA tables for this kind of info.
E.g.,
select *
from INFORMATION_SCHEMA.COLUMNS

You can connect the database to Visio and get a nice database diagram out of it that way. If they are the visual type that is.

The DBdesigner 4 supports a approach to synchronize your entity relationship model and your real SQL table model. At the features page they mention
Model-To-Database syncronisation
I think this should be possible vice-versa.

In Oracle:
select *
from user_tab_columns

Related

Using SSMA to convert from Access to SQL, scripting the fixes

I am using SSMA to convert from an Access db to a SQL 2019 DB.
There are some things I need to fix in the access DB so I am trying to figure out whether or not these things can be done via a query in access or you have to use the goofy UI and do everything manually.
So I had a couple of questions about queries in Microsoft Access:
Can you modify the 'required' attribute on a column within a table by using a query?
Can you configure Index (dupes) on a column by using a query?
Can you change validation rules using a query?
Can you create/delete relationships using a query?
Can you change the field length of a column by using a query?
Any examples of any of these would be helpful, when I google for ms access related things all of the content is either related to Access 2007/2010 or its very UI heavy rather than Query heavy.
I am trying to script this because I may have to do this migration several times.
Update: I was able to get most of what i needed figured out..
ALTER TABLE Users ALTER COLUMN Type CHECK(In ("I","U","") Or Is Null);
Still havent found a way to change the 'ValidationRule'.. trying to change it to
In ("I","U","") Or Is Null
Look into the Data Definition Language section of the MS Access SQL Reference, specifically the ALTER TABLE statement, which will cover the majority of your questions.
For example, in response to:
Can you change the field length of a column by using a query?
ALTER TABLE Table1 ALTER COLUMN Field1 TEXT(100)
The above will change the data type of the field Field1 within table Table1 to a text field accommodating 100 characters.

Convert column oriented table to row oriented one

I have a table structure as below on Greenplum database:
Wish to change it to the following structure so as to support pie charts on Tableau.
Could some one help me out ? Thanks!
Export the table to a CSV file
Install the Tableau Excel add-in
Open CSV file in Excel and use the add-in to reshape the data
SQL Server 2008 : Convert column value to row
http://blog.devart.com/is-unpivot-the-best-way-for-converting-columns-into-rows.html
Just to make sure you know about this Tableau feature:
Once you have devised the SQL select statement that will unpivot the data the way you'd like, then you can tell Tableau to use that instead of a select * by editing the data connection and selecting the Custom SQL option.
The generic way to unpivot in your situation is to union together several select statements, unless your database offers a more efficient alternative as described in the blog entry that Revanayya cited.
The following would work for a static, known beforehand, set of metrics:
SELECT
t.Date,
x.Metric,
CASE x.Metric
WHEN 'metric1' THEN metric1_week
WHEN 'metric2' THEN metric2_week
END AS week_val,
CASE x.Metric
WHEN 'metric1' THEN metric1_13week
WHEN 'metric2' THEN metric2_13week
END AS "13week_val"
FROM
atable AS t
CROSS JOIN
(VALUES ('metric1'), ('metric2')) AS x (Metric)
;
You could build a dynamic query off the above to account for an unknown number of metrics. For that, you would need to read the metadata (probably the INFORMATION_SCHEMA.COLUMNS system view) to build the dynamic bits, which are the VALUES list and the two CASE expressions, before embedding them into the query.

In Oracle SQL how would you find a specific column entry(in an unknown table) in the database

Suppose I'm using Oracle SQL and I want to find out which columns in the database have these values:
FIM
FIM
FIM
FNP
FJH
What I do know is that it was designed using the eBusiness suite(I think it was Oracle Reports). So I can use eTRM to narrow down.
But can I just use a stored procedure( like this question ) to find where the values are?
thanks
Sounds to me like you have a couple of options. Create a SOLR index based off of your database and query against that OR you can use Oracle Text. My personal recommendation is to use SOLR and remove excess load from your database, that of course is preference and YMMV.

sql or trick to search through whole database

is there a way to actually query the database in a such a way to search for a particular value in every table across the whole database ?
Something like a file search in Eclipse, it searches accross the whole worspace and project ?
Sorry about that .. its MS SQL 2005
SQL Workbench/J has a built in tool and command to do that.
It's JDBC based and should also work with SQL Server.
You will need to use the LIKE operator, and search through each field separately. i.e.
SELECT * FROM <table name>
WHERE (<field name1> LIKE '%<search value>%') OR
(<field name2> LIKE '%<search value>%') OR
... etc.
This isn't a quick way though.
I think the best way would be to
1) programatically generate the query and run it
2) use a GUI tool for the SQL server you are using which provides this functionality.
In mysql you can use union operator like
(SELECT * from table A where name = 'abc') UNION (SELECT * from
table B where middlename = 'pqr')
and so on
use full text search for efficency
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
Well, your best bet is to write a procedure to do this. But to give you some pointers you can use the INFORMATION_SCHEMA.Tables to get a list of all the tables in a given database and INFORMATION_SCHEMA.Columns to get a list of all columns. These tables also give you the datatype of columns. So you will need a few loops on these tables to do the magic.
It should be mentioned most RDBMSs nowadays support these schemas.
In phpmyadmin, go to your database, reach the search tab.
Here you will be able to select all of your tables and search through your entire db in one time.

How can I see where data in a column comes from?

I don't know if anyone can help me. In my job, I inherited a completely undocumented database (Oracle 11). So far, I've managed to map most of the tables and determine what's going on where. However, there are a few columns that I haven't been able to decipher.
Is there some way of finding out how is the data in the column built? This is not a manual input. Everything seems to point to the data being the result of an entry in a different column in a completely different table.
It might be an impossible task, but any and all suggestions will be more than welcome.
Thanks!
C
Perhaps the data is being inserted in your mystery columns via a trigger? Try looking in the PL/SQL source table in the dictionary:
SELECT owner, name, type, line
FROM dba_source
WHERE UPPER(text) LIKE '%MYSTERY_COLUMN_NAME%'
AND type = 'TRIGGER'; -- use or omit this as desired.
This will get you pointed in some possible places to look.
Good luck!
You can retrieve the complete DDL for a table using the DBMS_METADATA package.
SELECT dbms_metadata.get_ddl('TABLE', 'YOUR_TABLE_NAME', 'YOUR_USER_NAME')
FROM dual;
If those columns are really computed columns then this should be visible in the DDL for the table.
Alternatively you can use SQL Developer to show the DDL for the table
I am presuming that you already have the sql that is in question.
select table_name from dba_tab_columns
where column_name = 'COLUMN_YOU_WANT_TO_KNOW'
will provide all tables that contain a column name that you are looking for. If you do not have dba privileges you can use all_tab_columns instead (which will show all tables your account would have access to).