Tải bản đầy đủ - 0 (trang)
8-12. Understanding the Oracle Data Dictionary

8-12. Understanding the Oracle Data Dictionary

Tải bản đầy đủ - 0trang

Chapter 8 ■ Metadata



Keep the following in mind:





DATA_PRECISION is the length—in decimal digits (for the NUMBER type) or in binary digits

(for the FLOAT type).







DATA_SCALE is the number of digits to the right of decimal point in a number.







CHAR_USED is C if the maximum length is specified in characters; B if it is specified in bytes.



Constraints

To see the data that is not allowed into the Oracle database, you can look at all the constraints on one or more

tables using the following SQL:

SELECT



ACC.TABLE_NAME, ACC.COLUMN_NAME, AC.SEARCH_CONDITION



FROM



ALL_CONS_COLUMNS ACC INNER JOIN ALL_CONSTRAINTS AC

ON ACC.CONSTRAINT_NAME = AC.CONSTRAINT_NAME



WHERE



CONSTRAINT_TYPE = 'C'



You can, of course, extend the WHERE clause to limit the output to the table(s) that you wish to analyze.



Primary Keys

If you need to verify the primary keys of the tables that you are importing, try the following:

SELECT

FROM



A.Table_name, C.Column_name

ALL_CONSTRAINTS A

JOIN ALL_CONS_COLUMNS C

ON A.CONSTRAINT_NAME = C.CONSTRAINT_NAME

CONSTRAINT_TYPE = 'P'



WHERE



You can extend the WHERE clause to limit the output to the table(s) that you wish to analyze. You can also

tweak the CONSTRAINT_TYPE in the WHERE clause to get the following:





P: Returns primary key constraints.







C: Returns NOT NULL or check constraints.







R: Returns foreign key constraints.







U: Returns unique key constraints.



Indexes

Without going into the myriad details of Oracle indexing, it can nonetheless be of use to know which columns

are indexed in the source data. This is especially appropriate if you are using staging tables to transform

data; this information can give you a “heads-up” on which fields to index in order to accelerate data staging

processes. The SQL is

SELECT ALL_IND_COLUMNS.INDEX_NAME, ALL_IND_COLUMNS.TABLE_OWNER,

ALL_IND_COLUMNS.TABLE_NAME, ALL_IND_COLUMNS.COLUMN_NAME



465

www.it-ebooks.info



Chapter 8 ■ Metadata



FROM



ALL_INDEXES INNER JOIN ALL_IND_COLUMNS

ON ALL_INDEXES.INDEX_NAME = ALL_IND_COLUMNS.INDEX_NAME;



If an index name appears more than once, it is because you are facing a multicolumn index.

Don’t hesitate to look further into the ALL_INDEXES table to see if there is any other metadata that can help

you. In general, this is true of all Oracle’s data dictionary tables. They provide a wealth of useful information.



Referential Integrity Constraints

To get a simple overview of referential integrity constraints, all you need is to query two tables:





ALL_CONSTRAINTS







ALL_CONS_COLUMNS



The trick is how you join them—as the following SQL snippet shows.

SELECT

ACR.CONSTRAINT_NAME

,ACR.TABLE_NAME AS PK_TableName

,AC.COLUMN_NAME AS PK_ColumnName

,ACR1.TABLE_NAME AS FK_TableName

,AC1.COLUMN_NAME AS FK_ColumnName

FROM



ALL_CONS_COLUMNS AC1

INNER JOIN ALL_CONSTRAINTS ACR1

INNER JOIN ALL_CONSTRAINTS ACR

INNER JOIN ALL_CONS_COLUMNS AC

ON ACR.CONSTRAINT_NAME = AC.CONSTRAINT_NAME

ON ACR1.R_CONSTRAINT_NAME = ACR.CONSTRAINT_NAME

ON AC1.CONSTRAINT_NAME = ACR1.CONSTRAINT_NAME



WHERE ACR.CONSTRAINT_TYPE= 'P'



Triggers

Finally, you can see the triggers that have been used—which can sometimes give an understanding of why the

data is the way it is—with the following SQL:

SELECT TRIGGER_NAME, TRIGGER_TYPE, TRIGGERING_EVENT, TABLE_NAME, WHEN_CLAUSE,

DESCRIPTION, TRIGGER_BODY

FROM ALL_TRIGGERS



How It Works

If I say that the alternative title for this recipe is “the shortest guide ever to the Oracle data dictionary,” I hope

that the objective of what I am describing here will be clear. Whereas some databases leave you scrambling for

enough metadata, with Oracle, it often seems quite the reverse—you feel that you are drowning in the sea of

information that Oracle calls the data dictionary. So this recipe provides the shortest course you will probably

ever get on Oracle metadata. I can only stress that there are many ways of obtaining Oracle metadata. There is so

much available, that I am concentrating on an extremely simple subset of the metadata that I have found helpful



466

www.it-ebooks.info



Chapter 8 ■ Metadata



in the past. I hope that you will find them useful as a starting point that you can expand and build on. Of course,

for each of the approaches that follow, there are another dozen ways to get what you are looking for—so go ahead

and keep searching the Oracle dictionary (among all the resources available out there), for you will certainly find

other ways of obtaining the metadata you seek.



8-13. Displaying DB2 Metadata

Problem

You need to query metadata from a DB2 database from which you will soon be importing data.



Solution

Use SSIS to capture and store DB2 metadata from DB2’s INFORMATION_SCHEMA tables. The following steps

describe how to do it.

1.



Create tables to hold the DB2 metadata in SQL Server using the following DDL:

CREATE TABLE dbo.IS_DB2_Views

(

TABLE_CATALOG NVARCHAR(50) NULL,

TABLE_SCHEMA NVARCHAR(50) NULL,

TABLE_NAME NVARCHAR(50) NULL,

VIEW_DEFINITION NVARCHAR(MAX) NULL,

CHECK_OPTION NVARCHAR(10) NULL,

IS_UPDATABLE NVARCHAR(5) NULL

);

GO

CREATE TABLE dbo.IS_DB2_Tables

(

TABLE_CATALOG NVARCHAR(50) NULL,

TABLE_SCHEMA NVARCHAR(50) NULL,

TABLE_NAME NVARCHAR(50) NULL,

TABLE_TYPE NVARCHAR(26) NULL

);

GO

CREATE TABLE dbo.IS_DB2_TableConstraints

(

CONSTRAINT_CATALOG NVARCHAR(50) NULL,

CONSTRAINT_SCHEMA NVARCHAR(50) NULL,

CONSTRAINT_NAME NVARCHAR(50) NULL,

TABLE_CATALOG NVARCHAR(50) NULL,

TABLE_SCHEMA NVARCHAR(50) NULL,

TABLE_NAME NVARCHAR(50) NULL,

CONSTRAINT_TYPE NVARCHAR(50) NULL,

IS_DEFERRABLE NVARCHAR(5) NULL,

INITIALLY_DEFERRED NVARCHAR(5) NULL

);

GO



467

www.it-ebooks.info



Chapter 8 ■ Metadata



CREATE TABLE dbo.IS_DB2_Schemas

(

CATALOG_NAME NVARCHAR(50) NULL,

SCHEMA_NAME NVARCHAR(50) NULL,

SCHEMA_OWNER NVARCHAR(50) NULL,

DEFAULT_CHARACTER_SET_CATALOG NVARCHAR(50) NULL,

DEFAULT_CHARACTER_SET_SCHEMA NVARCHAR(50) NULL,

DEFAULT_CHARACTER_SET_NAME NVARCHAR(50) NULL,

SQL_PATH VARCHAR(MAX) NULL

);

GO

CREATE TABLE dbo.IS_DB2_Columns

(

TABLE_CATALOG NVARCHAR(50) NULL,

TABLE_SCHEMA NVARCHAR(50) NULL,

TABLE_NAME NVARCHAR(50) NULL,

COLUMN_NAME NVARCHAR(50) NULL,

ORDINAL_POSITION INT NULL,

COLUMN_DEFAULT NVARCHAR(2002) NULL,

IS_NULLABLE NVARCHAR(5) NULL,

DATA_TYPE NVARCHAR(50) NULL,

CHARACTER_MAXIMUM_LENGTH INT NULL,

CHARACTER_OCTET_LENGTH INT NULL,

NUMERIC_PRECISION INT NULL,

NUMERIC_PRECISION_RADIX INT NULL,

NUMERIC_SCALE INT NULL,

DATETIME_PRECISION INT NULL,

INTERVAL_TYPE NVARCHAR(50) NULL,

INTERVAL_PRECISION INT NULL,

CHARACTER_SET_CATALOG NVARCHAR(50) NULL,

CHARACTER_SET_SCHEMA NVARCHAR(50) NULL,

CHARACTER_SET_NAME NVARCHAR(50) NULL

);

GO

2.



Create a new SSIS package. Add a Data Flow task on to the Control Flow tab.

Double-click to open this package and switch to the Data Flow tab.



3.



Add an OLEDB connection manager to both the destination (SQL Server) database.



4.



Add an ADO.NET connection manager in the Providers list. Expand .NET Providers

for OleDb. Select  “.NET Providers for OleDb \IBM DB2 for i5/OS IBMDA400 OLE DB

Provider” (or the provider which you are using). Enter the fully qualified server name,

the username, and password. Select the Initial Catalog.



5.



Add an Execute SQL task. Configure it to use the destination (SQL Server OLEDB)

connection manager. Set the SQL Statement to:

TRUNCATE

TRUNCATE

TRUNCATE

TRUNCATE

TRUNCATE



TABLE

TABLE

TABLE

TABLE

TABLE



IS_DB2_Tables;

IS_DB2_Columns;

IS_DB2_Views;

IS_DB2_Schemas;

IS_DB2_TableConstraints;



468

www.it-ebooks.info



Chapter 8 ■ Metadata



6.



Add a sequence container, connect the Execute SQL Task to it, and add five Data

Flow tasks inside it. Name them Tables, Views, Columns, Schemas, and Table

Constraints. Set each one to use the DB2 connection manager, and set the data

access mode to SQL Command. Define the commands as follows:



Data Flow Task



SQL Code



Tables



SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME,



TABLE_TYPE



FROM INFORMATION_SCHEMA.TABLES

Views



SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, VIEW_DEFINITION,

CHECK_OPTION,IS_UPDATABLE

FROM INFORMATION_SCHEMA.VIEWS



Columns



SELECT

TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME,

ORDINAL_POSITION, COLUMN_DEFAULT,



COLUMN_NAME,



IS_NULLABLE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH,

CHARACTER_OCTET_LENGTH, NUMERIC_PRECISION,

NUMERIC_PRECISION_RADIX, NUMERIC_SCALE,

DATETIME_PRECISION, INTERVAL_TYPE,

INTERVAL_PRECISION, CHARACTER_SET_CATALOG,

CHARACTER_SET_SCHEMA, CHARACTER_SET_NAME

FROM INFORMATION_SCHEMA.COLUMNS

Schemas



SELECT CATALOG_NAME,



SCHEMA_NAME,



SCHEMA_OWNER



,DEFAULT_CHARACTER_SET_CATALOG , DEFAULT_CHARACTER_SET_SCHEMA

,DEFAULT_CHARACTER_SET_NAME, SQL_PATH

FROM INFORMATION_SCHEMA.SCHEMATA

Table Constraints



SELECT CONSTRAINT_CATALOG, CONSTRAINT_SCHEMA

,CONSTRAINT_NAME, TABLE_CATALOG, TABLE_NAME

,CONSTRAINT_TYPE, IS_DEFERRABLE

,INITIALLY_DEFERRED

FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS



The final package should look something like Figure 8-13.



469

www.it-ebooks.info



Chapter 8 ■ Metadata



Figure 8-13.  An SSIS package to return DB2 metadata

7.



Run the package.



When you query (and join) the five metadata tables you can get a clearer insight into the DB2 metadata.



How It Works

Being able to take a quick peek at the metadata behind a data source may be edifying (and can frequently help

you to debug data flow problems), but in some cases it is not enough. You need to be able to gather and store the

metadata for reference on a local system.

This can be useful when you need to





Analyze the metadata in greater detail.







Store metadata and validate against future data ingestion in order to discover changes

and/or anomalies.



This recipe shows you how to gather a base set of DB2 metadata. Fortunately, this particular database uses

INFORMATION_SCHEMA views, and so we can use them to source our metadata. In essence, this is nothing more

than a simple data transfer—only the source data is not data, but metadata. This makes DB2 a good example of a

technique that you can use for any source database that implements these metadata views. You will need to have

a DB2 OLEDB provider installed and working on the destination SQL Server.



■■Note As the INFORMATION_SCHEMA views for each database can vary—in both the number of views and their

contents—this model cannot be used identically for all SQL databases that allow metadata to be discovered using

this approach. It is, however, easy to extend and modify for other databases.



470

www.it-ebooks.info



Chapter 8 ■ Metadata



8-14. Obtaining SQL Server Metadata Using .NET

Problem

You want to obtain complete metadata, but without using INFORMATION_SCHEMA or system views.



Solution

Use the .NET GetSchema class to query metadata from a variety of data sources.

1.



The following destination tables are required, and can be defined using this DDL

(C:\SQL2012DIRecipes\CH08\DotNetMetadataTables.sql):

CREATE TABLE dbo.ADOSQLServerMetadataViews

(

ID INT IDENTITY(1,1) NOT NULL,

TableCatalog VARCHAR(50) NULL,

TableName VARCHAR(50) NULL,

TableSchema VARCHAR(50) NOT NULL,

CheckOption VARCHAR(50) NULL,

IsUpdatable VARCHAR(5) NULL

);

CREATE TABLE dbo.ADOSQLServerMetadataViewColumns

(

ID INT IDENTITY(1,1) NOT NULL,

ViewCatalog VARCHAR(50) NULL,

ViewName VARCHAR(50) NULL,

ViewSchema VARCHAR(50) NOT NULL,

TableCatalog VARCHAR(50) NULL,

TableName VARCHAR(50) NULL,

TableSchema VARCHAR(50) NOT NULL,

ColumnName VARCHAR(50) NULL

) ;

CREATE TABLE dbo.ADOSQLServerMetadataTables

(

ID INT IDENTITY(1,1) NOT NULL,

TableCatalog VARCHAR(50) NULL,

TableName VARCHAR(50) NULL,

TableSchema VARCHAR(50) NOT NULL,

TableType VARCHAR(50) NULL

);

CREATE TABLE dbo.ADOSQLServerMetadataIndexes

(

ID INT IDENTITY(1,1) NOT NULL,

ConstraintCatalog VARCHAR(50) NULL,

ConstraintSchema VARCHAR(50) NULL,

ConstraintName VARCHAR(50) NULL,

TableCatalog VARCHAR(50) NULL,



471

www.it-ebooks.info



Chapter 8 ■ Metadata



TableSchema VARCHAR(50) NULL,

TableName VARCHAR(50) NULL

) ;

CREATE TABLE dbo.ADOSQLServerMetadataIndexColumns

(

ID INT IDENTITY(1,1) NOT NULL,

ConstraintCatalog VARCHAR(50) NULL,

ConstraintSchema VARCHAR(50) NULL,

ConstraintName VARCHAR(50) NULL,

TableCatalog VARCHAR(50) NULL,

TableSchema VARCHAR(50) NULL,

TableName VARCHAR(50) NULL,

ColumnName VARCHAR(50) NULL,

OrdinalPosition VARCHAR(50) NULL,

KeyType VARCHAR(50) NULL,

IndexName VARCHAR(50) NULL

);

CREATE TABLE dbo.ADOSQLServerMetadataColumns

(

ID INT IDENTITY(1,1) NOT NULL,

TableCatalog VARCHAR(50) NULL,

TableName VARCHAR(50) NULL,

TableSchema VARCHAR(50) NOT NULL,

ColumnName VARCHAR(50) NULL,

OrdinalPosition INT NULL,

ColumnDefault VARCHAR(50) NULL,

IsNullable VARCHAR(5) NULL,

DataType VARCHAR(50) NULL,

CharacterMaximumLength VARCHAR(50) NULL,

CharacterOctetLength VARCHAR(50) NULL,

NumericPrecision VARCHAR(50) NULL,

NumericPrecisionRadix VARCHAR(50) NULL,

NumericScale VARCHAR(50) NULL,

DateTimePresision VARCHAR(50) NULL,

CharacterSetCatalog VARCHAR(50) NULL,

CharacterSetSchema VARCHAR(50) NULL,

CharacterSetName VARCHAR(50) NULL,

CollationCatalog VARCHAR(50) NULL,

IsFilestream VARCHAR(5) NULL,

IsSparse VARCHAR(5) NULL,

IsColumnSet VARCHAR(5) NULL

) ;

2.



Create a new SSIS package, and configure an ADO.NET connection manager that

points to the source SQL Server database.



3.



Add a Data Flow task, and switch to the Data Flow pane.



4.



Add a Script Component onto the Data Flow pane.



5.



Select Source as the Data Flow type, and click OK.



472

www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

8-12. Understanding the Oracle Data Dictionary

Tải bản đầy đủ ngay(0 tr)

×