informix and db2 an sql perspective

38
Informix and DB2 an SQL Perspective

Upload: tess98

Post on 12-Nov-2014

1.809 views

Category:

Documents


6 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Informix and DB2 an SQL Perspective

Informix and DB2 an SQL Perspective

Page 2: Informix and DB2 an SQL Perspective

Slide 2

1-22-2004

Informix and DB2 SQL

Bob CartsSenior Data Engineer, SAIC [email protected]

Keith GibertSoftware Engineer, SAIC [email protected]

Page 3: Informix and DB2 an SQL Perspective

Slide 3

1-22-2004

Our Story – How We Spent our Summer Vacation

We were told to convert to DB2 version 8.1 No DB2 Experience or Training Data Warehouse Application 900 GB of data Using Informix XPS version 8.31 684 pieces of ETL code ETL code SQL, KSH, PERL No stored procedures or triggers

Page 4: Informix and DB2 an SQL Perspective

Slide 4

1-22-2004

Introduction

Certified Informix DBA (Bob) Certified DB2 DBA (Bob) WAIUG Board of Directors (Bob) Work for SAIC (Science Applications International

Corporation) Windows and UNIX (Solaris, IBM AIX, HP-UX) IDS 7.31, 9.21, 9.3, XPS 8.31, DB2 8.1 Data Warehouse and OLTP Applications

Page 5: Informix and DB2 an SQL Perspective

Slide 5

1-22-2004

Scope of Presentation

Goal: To provide basic information on differences between Informix and DB2 SQL to help you get started in evaluating, planning or executing a conversion

Assumption: You are familiar with Informix, dbaccess, SQL

Included: Some basic SQL differences

Not Included: DBA Stuff, Internals,Embedded SQL, Triggers and Stored

Procedures

Page 6: Informix and DB2 an SQL Perspective

Slide 6

1-22-2004

Some Things We Learned - Support

Make friends with DB2 Developers in Toronto because the DB2 help desk does not answer SQL questions

DB2 SQL assistance is available for $ Informix Help Desk Does answer SQL questions

Page 7: Informix and DB2 an SQL Perspective

Slide 7

1-22-2004

Some Things We Learned – Documentation

DB2 documentation is on par or better than Informix documentation (and Informix documentation is pretty good!)

Improvements to the documentation are in the works (adding examples)

Look at IBM.com, DB2 Technical Support, Product Manuals

The manuals we use most: SQL reference Volumes 1 and 2 Data Movement Utilities Guide and Reference

Page 8: Informix and DB2 an SQL Perspective

Slide 8

1-22-2004

Some Things We Learned – Monitoring

Informix “onstat” commands make for easy monitoring

While monitoring tools are available in DB2, they can be awkward

Onstat type monitoring commands are on the list to be

added to DB2 in a future release

Page 9: Informix and DB2 an SQL Perspective

Slide 9

1-22-2004

Some Things We Learned – Monitoring

Determine which processes are running: INFORMIX: Onstat –g ses/sql/act/ath DB2: list applications show detail

View a specific process: INFORMIX: onstat –g ses <PID> DB2: get snapshot for application agentid <PID>

Kill a process: INFORMIX: onmode –z <PID> DB2: force application ‘(PID)’ or force application all

Page 10: Informix and DB2 an SQL Perspective

Slide 10

1-22-2004

Some Things We Learned – Monitoring

Veiw the database configuration: INFORMIX: onstat –c DB2: get database configuration and/or get database managers

configuration (get db cfg / get db mgr cfg)

View available tablespace: INFORMIX: onstat –d/-D/-t/-T DB2: list tablespace show detail

Page 11: Informix and DB2 an SQL Perspective

Slide 11

1-22-2004

Interactive Access

DBACCESS – Psuedo GUI, Menu bar driven DB2 CLP (command line processor) – A little clumsy, but

adequate. More like sybase or oracle interface Getting Help

Help dbaccess cntl-w Help ? CLP Command

Connecting Db2 initially requires an explicit connect Informix implicitly connects when using dbaccess

Page 12: Informix and DB2 an SQL Perspective

Slide 12

1-22-2004

DB2CLP

Several ways to execute commands db2 <command> Example: db2 connect to mydb

You can also use interactive modedb2 –t Connect to mydb;Select col1, col2From mytable;Quit;

Page 13: Informix and DB2 an SQL Perspective

Slide 13

1-22-2004

DB2CLP

You can execute OS commands within DB2 CLP! Cp file1 file2 Get a list of databases:List active databases; Get a list of columns:List tables [for schema <schemaname>; Get the layout of a table:Describe table <schemaname>.<tablename>;

Page 14: Informix and DB2 an SQL Perspective

Slide 14

1-22-2004

Calling from ksh Script

Dbaccess [dbname] <<EOF > stdout 2>stderrorSelect bla bla bla;EOF Db2 –tvl <logfilename> <<EOF >Connect to [dbname];Select bla bla blaEOF

Page 15: Informix and DB2 an SQL Perspective

Slide 15

1-22-2004

A few little things…

Default Permissions Informix: Public has permissions by default DB2: public does not

Updating Statistics (different syntax)Runstats on <schema>.<table> with distributionAnd indexes all shrlevel change;

Code Comments DB2 does support the dash dash for comments However, they need to start in column #1 of a line-- This works as a commentsomecol char(3) -- this does not

Page 16: Informix and DB2 an SQL Perspective

Slide 16

1-22-2004

A few little things…

Don’t use double quotes in DB2 !Select * from tabname where name = ‘Bob’

DB2 does not support Directives

Page 17: Informix and DB2 an SQL Perspective

Slide 17

1-22-2004

Datatypes

DB2 does not support implicit casting Explicitly cast all data types in expressions Example:Create table bob.tabname (col1 integer,col2 char(10),col3 char(3))…

Insert into tabname values (null, ‘bob’, null) --informix

Insert into tabname values (cast(null as integer), ‘bob’, cast(null as char))

Page 18: Informix and DB2 an SQL Perspective

Slide 18

1-22-2004

Limiting Number of Rows Returned/ Optimize for Number of Rows

Informix: Select first 100 ssn from people; DB2: Select ssn from people Fetch first 100 rows only; Optimize for a particular number of rows (db2 only) Db2: Select ssn from people Optimize for 20 rows;

Page 19: Informix and DB2 an SQL Perspective

Slide 19

1-22-2004

Join Syntax

DB2 Outer join syntax is different than Informix DB2 is reportedly ANSI standard and Informix is not

Page 20: Informix and DB2 an SQL Perspective

Slide 20

1-22-2004

Join Syntax

INFORMIX: Select a.name, a.employ_num, b.program, c.ed_level From employee a, training b, OUTER education c

Where a.employ_num = b.employ_num and

a.employ_num = c.employ_num and

b.program = ‘DB2101’

DB2: Select a.name, a.employ_num, b.program, c.ed_level From employee a INNER JOIN training b on a.employ_num = b.employ_num LEFT OUTER JOIN education c on a.employ_num = c.employ_num Where b.program = ‘DB2101’

Page 21: Informix and DB2 an SQL Perspective

Slide 21

1-22-2004

Group by

Can’t use “number” syntax Group by 1,2,3….

Forced to make case statements, etc redundant

Page 22: Informix and DB2 an SQL Perspective

Slide 22

1-22-2004

Group by - INFORMIX

Select gender, state_of_birth,Case when age > 19 and age < 31 then ‘Young’ when age > 30 and age < 46 then ‘middle aged’ when age > 46 then ‘Up there’End categoryFrom employee Group by 1,2,3

Page 23: Informix and DB2 an SQL Perspective

Slide 23

1-22-2004

Group by – DB2

Select gender, state_of_birth,Case when age > 19 and age < 31 then ‘Young’ when age > 30 and age < 46 then ‘middle aged’ when age > 46 then ‘Up there’End caseFrom employee Group by gender, state_of_birth,Case when age > 19 and age < 31 then ‘Young’ when age > 30 and age < 46 then ‘middle aged’ when age > 46 then ‘Up there’End case

Page 24: Informix and DB2 an SQL Perspective

Slide 24

1-22-2004

Having

Syntax available in DB2 and not Informix Look for duplicate keysselect * from people_table where ssn in(select ssn from people_tablegroup by ssn having count(*) > 1 );

Page 25: Informix and DB2 an SQL Perspective

Slide 25

1-22-2004

Alter Statements

Alter capabilities are limited in DB2 Can’t drop a column Can’t change a datatype for a column

We of course used the alter – drop in our Informix Code!

Page 26: Informix and DB2 an SQL Perspective

Slide 26

1-22-2004

UnLogged Tables

Using Unlogged databases in Informix is straight forward Using Unlogged tables in db2 version 7.2 is

Awkward Temporary Dangerous Still Possible

Db2 version 8.1 is less disastrous Basic problem is auto rollback makes table permanently

unavailable, must recreate or restore

Page 27: Informix and DB2 an SQL Perspective

Slide 27

1-22-2004

UnLogged Tables

When creating a table must specify that logging can be turned offCreate table bob.xyz(Col1 char(2))In tablespace123 index in indexspace456Not logged initially;

Must alter the table to temporarily turn logging offUpdate command options using c off;Alter table bob.xyz activate not logged initially;Insert into bob.xyz …Commit;

If anything goes wrong, boom no useable table!

Page 28: Informix and DB2 an SQL Perspective

Slide 28

1-22-2004

Utilities

DB2 has import, export, load utilities Load is fastest way to get data into table Load can handle various delimiters or no delimiters You can replace or insert (append) Terminate or restart Example:Load from /pathname/filenameOf del modified by coldel| keepblanks anyorderMessages messagefile.msgTemp files path /large_directoryReplace into bob.xyz;

Page 29: Informix and DB2 an SQL Perspective

Slide 29

1-22-2004

Utilities

Another load example:load from strip.txt OF ASCMETHOD L (1 7,9 43,45 54,56 90,92 126,128 145, 147 148,150 160,268 277,336 336)messages messagefile.msgtempfiles path $WORKDIRreplace INTO bob.xyz NONRECOVERABLE;

Import is slow

Page 30: Informix and DB2 an SQL Perspective

Slide 30

1-22-2004

Utilities

Export has several differences from dbexport By default numbers have a + and leading zeros Character data is enclosed by double quotes Character data is padded to full length Example:

Export to filename.outOf del modified by coldel| decplusblankSelect date_provided, rtrim(record_id) from tabname;

Used sed to strip out quotes and leading zeros

Page 31: Informix and DB2 an SQL Perspective

Slide 31

1-22-2004

Utilities

Getting the ddl Informix: dbschemaDbschema –d databasename outputfilename.out DB2: db2lookDb2look –d databasename –e > outputfilename.out Both have many options Both have usage built in, just type command

Page 32: Informix and DB2 an SQL Perspective

Slide 32

1-22-2004

Error Messages

Both databases allow retrieval of error messages from the command line INFORMIX: finderr -217 DB2: db2 ? SQL0203

Page 33: Informix and DB2 an SQL Perspective

Slide 33

1-22-2004

Error Messages - INFORMIX

-217 Column column-name not found in any table in the query(or SLV is undefined).

The name appears in the select list or WHERE clause of this query but isnot defined in a table and does not appear as a statement local variable(SLV) definition. Check that the column name or SLV name and the names ofthe selected tables are spelled as you intended.

If all names are spelled correctly, you are not using the right tables,the database has been changed, or you have not defined the SLV. If thename not found is a reference to a column, that column might have beenrenamed or dropped. If the name not found represents an SLV and youdefined the SLV in the statement, make sure that the SLV definitionappears before all other references to that SLV name.

This error message can also appear during the execution of an ALTER TABLEstatement when the engine tries to update views that depend on the table.

Page 34: Informix and DB2 an SQL Perspective

Slide 34

1-22-2004

Error Messages – DB2

SQL0203NA reference to column "<name>" is ambiguous. Explanation: The column "<name>" is used in the statement and there is more than one possible

column to which it could refer. This could be the result of:

two tables specified in a FROM clause that have columns with the same name

the ORDER BY clause refers to a name that applies to more than one column in the select list

a reference to a column from the subject table in a CREATE TRIGGER statement does not use the correlation name to indicate if it refers to the old or new transition variable.

The column name needs further information to establish which of the possible table columns it is.

The statement cannot be processed.

User Response: Add a qualifier to the column name. The qualifier is the table name or correlation name. A column may need to be renamed in the select list.

sqlcode: -203

sqlstate: 42702

Page 35: Informix and DB2 an SQL Perspective

Slide 35

1-22-2004

INFORMIX XPS (Version 8.x)

DB2 does not have the external table feature, must up import, export and load utilities

DB2 requires explicit indexes to perform adequately DB2 does not have the join update/batch update feature

(a subselect must be used) DB2 does not support truncate command

Page 36: Informix and DB2 an SQL Perspective

Slide 36

1-22-2004

Summary

Support Documentation Monitoring Interactive access Calling from ksh Default permissions Update Statistics Code comments Double quotes Directives Datatypes

Limiting rows returned Join Syntax Group by Having Alter statements Unlogged tables Utilities Error messages XPS differences Resources

Page 37: Informix and DB2 an SQL Perspective

Slide 37

1-22-2004

Resources

Db2 administration guide (book) http://www.ibm.com/developerworks/db2/

Online tutorials geared for certification testing Porting Resources Bunch of other items

Page 38: Informix and DB2 an SQL Perspective

Slide 38

1-22-2004

Contact

Contact [email protected],

[email protected]

Presentation will be placed on WAIUG site:

www.iiug.org/~waiug