Derby - constraints

前端 未结 2 2068
傲寒
傲寒 2021-01-13 07:51

In the Derby server, how can you use the information in the system tables of the schema to create a select statement in order to retrieve the constraint names for each table

2条回答
  •  猫巷女王i
    2021-01-13 08:40

    SELECT sc.schemaname, co.constraintname, t.tablename, cg.descriptor, t2.tablename, cg2.descriptor, f.deleterule, f.updaterule
    FROM sys.sysconstraints co
    JOIN sys.sysschemas sc ON co.schemaid = sc.schemaid
    JOIN sys.systables t ON co.tableid = t.tableid
    JOIN sys.sysforeignkeys f ON co.constraintid = f.constraintid
    JOIN sys.sysconglomerates cg ON f.conglomerateid = cg.conglomerateid
    JOIN sys.sysconstraints co2 ON f.keyconstraintid = co2.constraintid
    JOIN sys.systables t2 ON co2.tableid = t2.tableid
    JOIN sys.syskeys k ON co2.constraintid = k.constraintid
    JOIN sys.sysconglomerates cg2 ON k.conglomerateid = cg2.conglomerateid
    WHERE co.type = 'F' 
        and sc.schemaname = current schema    
    

    the two descriptor entries contain a list of column numbers for each table, like

    BTREE(2,1)

    where the numbers correspond to the column numbers in the syscolumns table for the corresponding table.

    If anyone has an elegant way of extracting this in this query, I would like to know. I am getting a list of all the columns for a table in a separate query and extracting the names from that after parsing the descriptors to get the numbers.

提交回复
热议问题