Sunday, 4 May 2014

How to alter an identity column as generated by default to generated always

How to alter an identity column as generated by default to generated always


1 - First Step


Drop the propriety identity

db2 alter table <table_name> alter column <column_name> drop identity

2 - Second step


Create the propriety identity again, now as generated always

db2 alter table <table_name> alter column <column_name> set generated always as identity (start with <max(column_identity_name)>)

Example:
db2 "alter table identity alter column col3 set generated always as identity (start with 116)"

Saturday, 3 May 2014

How to acess DB2 using Python

How to acess DB2 using Python

To acess DB2 databases using python, you have to get the PyDB2 drive, you can get PyDb2 in the link below

http://sourceforge.net/projects/pydb2

With PyDB2 installed, we can start to connect to DB2 Databses using Python:

DB2 Interactive Example

Here is a simple example that illustrates an entire interaction with a DB2 database, from beginning to end:
>>> import DB2
>>> conn = DB2.connect(dsn='sample', uid='db2inst1', pwd='******')
>>> curs = conn.cursor()
>>> curs.execute('SELECT * FROM ORG')
>>> rows = curs.fetchall()
>>> rows
[('A00', 'SPIFFY COMPUTER SERVICE DIV.', '000010', 'A00', None), 
('B01', 'PLANNING', '000020', 'A00', None), 
('C01', 'INFORMATION CENTER', '000030', 'A00', None), 
('D01', 'DEVELOPMENT CENTER', None, 'A00', None), 
('D11', 'MANUFACTURING SYSTEMS', '000060', 'D01', None), 
('D21', 'ADMINISTRATION SYSTEMS', '000070', 'D01', None), 
('E01', 'SUPPORT SERVICES', '000050', 'A00', None), 
('E11', 'OPERATIONS', '000090', 'E01', None), 
('E21', 'SOFTWARE SUPPORT', '000100', 'E01', None), 
('F22', 'BRANCH OFFICE F2', None, 'E01', None),
 ('G22', 'BRANCH OFFICE G2', None, 'E01', None), 
('H22', 'BRANCH OFFICE H2', None, 'E01', None), 
('I22', 'BRANCH OFFICE I2', None, 'E01', None), 
('J22', 'BRANCH OFFICE J2', None, 'E01', None)]
>>> curs.close()
>>> conn.close()