How to check if existing database matches program's format - sql

First, I'm new to Java DB programming so maybe my approach is wrong. So please keep that in mind. Now the question:
My program let's the user open an existing Java Derby database. When it's opened I want to check if the database contains the correct tables and each table has the correct columns - not just names but type also. So far I'm trying to create each table and if it throws an exception then I know the table exists. Now I need to check column types. What is the best practice to do this? Hard-code check for each column type as answered here in the catch block?

If you want to figure out what tables are present in the database, and what columns each table has, etc., there are (at least) two simple approaches:
Issue SQL queries against the Derby system catalogs, such as
select s.schemaname || '.' || t.tablename
from sys.systables t, sys.sysschemas s
where t.schemaid = s.schemaid
and t.tabletype = 'T'
order by s.schemaname, t.tablename;
Use the API provided by java.sql.DatabaseMetaData, and its methods such as getSchemas(), getTables(), and getColumns()

Related

ADO SQL query create column if it doesn't exist

I have a query for a report based on an MS Access database (as the program project file). The tables in this database get updated with new fields periodically as new features are added.
We need to be able to support old and new versions of the file for our report, so need to know if there is a way to insert a field into the SQL SELECT query if it does not already exist. (Note: Do not want to create ALTER TABLE type statements, as the field only needs to be added into the result set, not into the table permanently.)
I know you can do something like "" AS [FieldName], but that only applies when you know the field doesn't exist and need to create a blank spot for it (such as when a unioned table does have that field). In this case, the table might have the field so I want to use it if it does, but if it doesn't I want to have it still exist in the query results with a default value.
Any help would be appreciated. (I also know you can force the user to update the file, but that option was stated as "only last resort".)
Thanks,
Chris

Check whether field exists in SQLite without fetching them all

I am writing a database abstraction layer that also abstracts some of the different query types. One of them is called "field_exists" - its purpose should be pretty self-explanatory.
And I want to implement that for SQLite.
The problem I am having is that I need to use one query that either returns a row confirming that the field exists or none if it doesn't. Thus, I cannot use the PRAGMA approach.
So, what query can I use to check whether a field exists in SQLite, that fulfills the above criteria?
EDIT: I should add that the query needs to be able to run in PHP code (using PDO).
Also, the query should look something like this (which only works with MySQL):
SHOW COLUMNS FROM table LIKE 'field'
Trying to select a field that doesn't exist will return an exception, then you can catch it and return nothing.
Use the .schema TABLENAME command. It will tell you the command that was issued to create the table. For more info chekcout the SQLite command shell documentation.
If you don't have access to the sqlite command line, you can always query the sqlite_master table. Let's say you want to know the command used to create the table MyTable. You'd issue this:
select sql from sqlite_master where name='MyTable';
This then gives you the sql command that was used to create the table. Then just grep through that output and see if the column you're looking for is in the command used to create the table.
UPDATE 2:
Actually better than the sql I posted above, you can use this:
PRAGMA table_info(*table_name*)
This will show you all the columns in a given table along with their types and other info.

How can I create a schema alias in DB2 on System z?

Part of a reporting toolkit we use for our development is configured to always use the same schema (say XYZZY).
However, certain customers have stored their data in a different schema PLUGH. Is there any way within DB2/z to alias the entire schema XYZZY to refer to the objects in schema PLUGH?
The reporting toolkit runs on top of ODBC using the DB2 Connect Enterprise Edition or Personal Edition 9.1 drivers.
I know I can set up individual aliases for tables and views but we have many hundreds of these database objects and it will be a serious pain to do the lot. It would be far easier to simply have DB2 auto-magically translate the whole schema.
Keep in mind we're not looking for being able to run with multiple schemas, we just want a way to redirect all requests for database objects to a single, differently named, schema.
Of course, if there's a way to get multiple schemas on a per-connection basis, that would be good as well. But I'm not helpful.
I am guessing that by DB/2 schema you mean the qualifying name in some two part object name. For
example, if a two
part table name is: PLUGH.SOME_TABLE_NAME. You want to do define XYZZY as an
alias name for PLUGH so the reporting program can refer to the table as XYZZY.SOME_TABLE_NAME.
I don't know how to directly do that (schema names don't take on aliases as far as I am aware).
The objection you have to defining individual alias names
using something like:
CREATE ALIAS XYZZY.SOME_TABLE_NAME FOR PLUGH.SOME_TABLE_NAME
is that there are hundreds of them to do making it a real pain. Have you thought about
using a SELECT against the DB/2 catalogue to generate CREATE ALIAS statements for
each of the objects you need to refer to? Something like:
SELECT 'CREATE ALIAS XYZZY.' || NAME || ' FOR PLUGH.' || NAME
FROM SYSIBM.SYSTABLES
WHERE CREATOR = 'PLUGH'
Capture the output into a file then execute it. Might be hundreds of commands,
but at least you didn't have to write them.

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).

Rename column in MySQL using an alias

Actually the problem arises since we have named a column authorization which is ok on MySQL but fails miserably on Postgres, authorization is a reserved keyword there.
We want to have all schemas in sync even on different databases, so we want to rename authorization in the MySQL catalogue(s) to something neutral.
It would be great if the renaming of the MySQL table columns would work seamlessly with older and newer versions of the applicatation at least for a couple of days so that the transition is smooth.
Does anybody know how to do this? A nice idea would be to have some sort of alias/redirection, e.g. create a new column _authorization that is actually the same column as authorization but under the new name. Queries using the new name _autorization will work as well as queries using the old name. Then we can update the application. If all servers have the latest binaries, we can drop the alias and rename the column.
Any ideas? Any help is greatly appreciated.
A nice idea would be to have some sort of alias/redirection...
No such functionality exists. The closest is to use a view based on the table, because it's easier to rename a column in a view as there isn't any underlying data to change. Otherwise:
Rename the column in MySQL using the ALTER TABLE statement:
ALTER TABLE [your table]
CHANGE authorization [new_col_name] [column_definition]
I also had to use an alias to a column (e.g. a particular app needed 'id' column instead of 'entry_id').
i was able to create a view (MySQL 5.1+ recommended for views) from the table with an extra alias to the culprit column. for the 'authorization' column from your example:
CREATE VIEW maintable_view AS
SELECT *, authorization AS authcol FROM MAINTABLE
if you run a update-query on the view, and set 'authcol' to a new value,
this will update the 'authorization' column in 'maintable'.
UPDATE maintable_view SET authcol=22 WHERE id=3
http://dev.mysql.com/doc/refman/5.1/en/create-view.html