Database Cheat Sheet

Contents

Informix

Who is connected to a database (change wildcard)

select
  sysdatabases.name database, -- Database Name
  syssessions.username,       -- User Name
  syssessions.hostname,       -- Workstation
  syssessions.pid,            -- Process ID
  syssessions.feprogram,      -- Command / Application
  syslocks.owner sid          -- Informix Session ID
from
  syslocks,
  sysdatabases ,
  syssessions
where
  syslocks.tabname = "sysdatabases"       -- Find locks on sysdatabases
and syslocks.rowidlk = sysdatabases.rowid -- Join rowid to database
and syslocks.owner   = syssessions.sid    -- Session ID to get user info
and sysdatabases.name like '%'
order by 1

What tables have a foreign key to a tables PK

select
  e.tabname,
  g1.colname
from
  systables      a,
  sysconstraints b,
  sysreferences  c,
  sysconstraints d,
  systables      e,
  sysindexes     f,
  syscolumns     g1
where
  a.tabname      = 'TABLENAME'
and a.tabid      = b.tabid
and b.constrtype ='P'
and b.constrid   = c.primary
and b.tabid      = c.ptabid
and c.constrid   = d.constrid
and d.tabid      = e.tabid
and e.tabid      = f.tabid
and f.idxname    = d.idxname
and f.tabid      = g1.tabid
and abs(f.part1) = g1.colno

Adding a new dbspace (chunk)

Useful resources

Create the space

touch /opt/informix_11.70/data/datadbs_02
chmod 660 /opt/informix_11.70/data/datadbs_02

Add the chunk

onspaces \
  -a datadbs \
  -p /opt/informix_11.70/data/datadbs_02 \
  -o 0
  -s 2048000 # size of chunk in kb

Check the space is available and force a level 0 backup

onstat -d
ontape -s -L 0 -d > /dev/null
onstat -m

The following is a useful query to check the amount of free space

select
  name[1,8] dbspace,
  sum(chksize) Pages_size,
  sum(chksize) - sum(nfree) Pages_used,
  sum(nfree) Pages_free,
  round ((sum(nfree))/(sum(chksize))*100,2) percent_free
from
  sysdbspaces d, syschunks c
where
  d.dbsnum=c.dbsnum
group by 1
order by 1;

PostgreSQL

comments powered by Disqus