kingbase7获取唯一索引和子分区键的view

Posted buffercache

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了kingbase7获取唯一索引和子分区键的view相关的知识,希望对你有一定的参考价值。

--构造表
CREATE TABLE ZYJ.TEST(ID INT PRIMARY KEY,NAME VARCHAR(200),ADDR VARCHAR(200),TEL VARCHAR(11));
ALTER TABLE ZYJ.TEST ADD COLUMN VARCHAR1 VARCHAR(200);
ALTER TABLE ZYJ.TEST ADD COLUMN VARCHAR2 VARCHAR(200);
ALTER TABLE ZYJ.TEST ADD COLUMN VARCHAR3 VARCHAR(200);

CREATE INDEX IDX_ADDR ON ZYJ.TEST(ADDR);
CREATE UNIQUE INDEX IDX_TEL ON ZYJ.TEST(TEL);
CREATE UNIQUE INDEX IDXU_VARCHAR123 ON ZYJ.TEST(VARCHAR1,VARCHAR2,VARCHAR3);

--构造系统视图结构
CREATE TABLE ZYJ.SYS_ATTRIBUTE AS SELECT * FROM SYS_ATTRIBUTE ATTR WHERE ATTR.ATTRELID=16493;

CREATE TABLE ZYJ.SYS_INDEX AS SELECT * FROM SYS_INDEX INDEX WHERE INDRELID=16493;
--------------------------------------------------------------------------------------------------------------------------
--获取索引名称的函数
CREATE OR REPLACE FUNCTION ZYJ.GET_COL_INDEXS( TABOID OID, COLNUM INT4) RETURN TEXT AS
DECLARE
indexs_text TEXT;
index_record record;
temp_index_oid OID;
BEGIN
indexs_text := NULL;
temp_index_oid := NULL;
FOR index_record IN SELECT CLASS.RELNAME indexname, ATTR.ATTNAME colname, INDEX.INDEXRELID indexoid
FROM SYS_INDEX INDEX, SYS_CLASS CLASS, SYS_ATTRIBUTE ATTR
WHERE taboid = INDEX.INDRELID AND colnum = any(INDEX.INDKEY) AND INDEX.INDEXRELID = CLASS.OID
AND ATTR.ATTRELID = INDEX.INDRELID AND ATTR.ATTNUM = any(INDEX.INDKEY) ORDER BY indexoid
LOOP -- get indexs and column information
IF temp_index_oid = index_record.indexoid THEN
indexs_text := CONCAT(indexs_text, ‘, ‘);
indexs_text := CONCAT(indexs_text, index_record.colname);
ELSE
IF indexs_text IS NOT NULL THEN
indexs_text := CONCAT(indexs_text, ‘)‘);
indexs_text := CONCAT(indexs_text, ‘, ‘);
ELSE
indexs_text := ‘‘;
END IF;
indexs_text := CONCAT(indexs_text, index_record.indexname);
indexs_text := CONCAT(indexs_text, ‘(‘);
indexs_text := CONCAT(indexs_text, index_record.colname);
temp_index_oid := index_record.indexoid;
END IF;
END LOOP;
IF indexs_text IS NOT NULL THEN
indexs_text := CONCAT(indexs_text, ‘)‘);
END IF;
RETURN indexs_text;
END GET_COL_INDEXS;
--------------------------------------------------------------------------------------------------------------------------
SELECT CASE WHEN INDEX.INDISPRIMARY=FALSE AND INDEX.INDISUNIQUE=TRUE THEN 1 ELSE 0 END AS IS_UNIQUE_INDEX,
INDEX.INDISPRIMARY,INDEX.INDISUNIQUE,C.OID,INDEX.INDRELID,INDEX.INDEXRELID,
CAST((( SELECT SYS_NAMESPACE.NSPNAME
FROM SYS_NAMESPACE
WHERE SYS_NAMESPACE.OID = C.RELNAMESPACE)) AS CHARACTER VARYING(30 BYTE)) AS NSPNAME,
CAST(C.RELNAME AS CHARACTER VARYING(30 BYTE)) AS TABLE_NAME, CAST(ATTR.ATTNAME AS CHARACTER VARYING(30 BYTE)) AS COLUMN_NAME, CAST(
CASE T.TYPNAME
WHEN ‘BPCHAR‘ THEN ‘CHAR‘
ELSE CAST(T.TYPNAME AS TEXT)
END AS CHARACTER VARYING(106 BYTE)) AS DATA_TYPE, CAST(NULL AS CHARACTER VARYING(3 CHAR)) AS DATA_TYPE_MOD, CAST(AU.ROLNAME AS CHARACTER VARYING(30 BYTE)) AS DATA_TYPE_OWNER, CAST(
CASE
WHEN ATTR.ATTLEN = (-1) THEN
CASE T.TYPNAME
WHEN ‘BIT‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘BIT VARYING‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘TIMETZ‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘TIME‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘TIMESTAMPTZ‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘TIMESTAMP‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘INTERVAL‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘VARBIT‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘VARCHAR‘ THEN
CASE ATTR.ATTTYPMOD
WHEN (-1) THEN NULL
ELSE ABS(ATTR.ATTTYPMOD) - 4
END
WHEN ‘BPCHAR‘ THEN
CASE ATTR.ATTTYPMOD
WHEN (-1) THEN NULL
ELSE ABS(ATTR.ATTTYPMOD) - 4
END
ELSE
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16) - 4
ELSE 0
END
END
ELSE CAST(ATTR.ATTLEN AS INTEGER)
END AS NUMERIC(10,0)) AS DATA_LENGTH, CAST(
CASE CAST(
CASE T.TYPNAME
WHEN ‘BPCHAR‘ THEN ‘CHAR‘
ELSE CAST(T.TYPNAME AS TEXT)
END AS CHARACTER VARYING(106 BYTE))
WHEN ‘NUMERIC‘ THEN
CASE
WHEN ATTR.ATTTYPMOD = (-1) THEN CAST(38 AS NUMERIC(38,0))
ELSE CAST(((ATTR.ATTTYPMOD - 4) >> 16) AS NUMERIC(38,0)) & BITTONUMERIC(B‘1111111111111111‘)
END
WHEN ‘FLOAT4‘ THEN CAST(7 AS NUMERIC(38,0))
WHEN ‘FLOAT8‘ THEN CAST(15 AS NUMERIC(38,0))
ELSE NULL
END AS NUMERIC(10,0)) AS DATA_PRECISION, CAST(
CASE CAST(
CASE T.TYPNAME
WHEN ‘BPCHAR‘ THEN ‘CHAR‘
ELSE CAST(T.TYPNAME AS TEXT)
END AS CHARACTER VARYING(106 BYTE))
WHEN ‘NUMERIC‘ THEN CAST((ATTR.ATTTYPMOD - 4) AS NUMERIC(38,0)) & BITTONUMERIC(B‘1111111111111111‘)
ELSE NULL
END AS NUMERIC(10,0)) AS DATA_SCALE, CAST(IF(CAST(ATTR.ATTNOTNULL AS INTEGER), ‘N‘, ‘Y‘) AS CHARACTER VARYING(1 CHAR)) AS NULLABLE, CAST(ATTR.ATTNUM AS NUMERIC(10,0)) AS COLUMN_ID, CAST(
CASE ATTR.ATTHASDEF
WHEN false THEN NULL
ELSE CAST(
CASE
WHEN ATTR.ATTLEN = (-1) THEN
CASE T.TYPNAME
WHEN ‘BIT‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘BIT VARYING‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘TIMETZ‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘TIME‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘TIMESTAMPTZ‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘TIMESTAMP‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘INTERVAL‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘VARBIT‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘VARCHAR‘ THEN
CASE ATTR.ATTTYPMOD
WHEN (-1) THEN NULL
ELSE ABS(ATTR.ATTTYPMOD) - 4
END
WHEN ‘BPCHAR‘ THEN
CASE ATTR.ATTTYPMOD
WHEN (-1) THEN NULL
ELSE ABS(ATTR.ATTTYPMOD) - 4
END
ELSE
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16) - 4
ELSE 0
END
END
ELSE CAST(ATTR.ATTLEN AS INTEGER)
END AS NUMERIC(10,0))
END AS NUMERIC(10,0)) AS DEFAULT_LENGTH, DEF.ADSRC AS DATA_DEFAULT, CAST(0 AS NUMERIC(10,0)) AS NUM_DISTINCT, CAST(0 AS NUMERIC(10,0)) AS LOW_VALUE, CAST(0 AS NUMERIC(10,0)) AS HIGH_VALUE, CAST(0 AS NUMERIC(10,0)) AS DENSITY, CAST(0 AS NUMERIC(10,0)) AS NUM_NULLS, CAST(0 AS NUMERIC(10,0)) AS NUM_BUCKETS, CAST(NULL AS TIMESTAMP(0) WITHOUT TIME ZONE) AS LAST_ANALYZED, CAST(0 AS NUMERIC(10,0)) AS SAMPLE_SIZE, CAST(NULL AS CHARACTER VARYING(44 CHAR)) AS CHARACTER_SET_NAME, CAST(0 AS NUMERIC(10,0)) AS CHAR_COL_DECL_LENGTH, CAST(‘NO‘ AS CHARACTER VARYING(3 CHAR)) AS GLOBAL_STATS, CAST(‘NO‘ AS CHARACTER VARYING(3 CHAR)) AS USER_STATS, CAST(0 AS NUMERIC(10,0)) AS AVG_COL_LEN, CAST(
CASE CAST(
CASE T.TYPNAME
WHEN ‘BPCHAR‘ THEN ‘CHAR‘
ELSE CAST(T.TYPNAME AS TEXT)
END AS CHARACTER VARYING(106 BYTE)) = ANY (ARRAY[‘VARCHAR‘, ‘BPCHAR‘, ‘NCHAR‘, ‘NVARCHAR‘])
WHEN true THEN CAST(
CASE
WHEN ATTR.ATTLEN = (-1) THEN
CASE T.TYPNAME
WHEN ‘BIT‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘BIT VARYING‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘TIMETZ‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘TIME‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘TIMESTAMPTZ‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘TIMESTAMP‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘INTERVAL‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘VARBIT‘ THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16)
ELSE 0
END
WHEN ‘VARCHAR‘ THEN
CASE ATTR.ATTTYPMOD
WHEN (-1) THEN NULL
ELSE ABS(ATTR.ATTTYPMOD) - 4
END
WHEN ‘BPCHAR‘ THEN
CASE ATTR.ATTTYPMOD
WHEN (-1) THEN NULL
ELSE ABS(ATTR.ATTTYPMOD) - 4
END
ELSE
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ATTR.ATTTYPMOD - ((ATTR.ATTTYPMOD >> 16) << 16) - 4
ELSE 0
END
END
ELSE CAST(ATTR.ATTLEN AS INTEGER)
END AS NUMERIC(10,0))
ELSE CAST(0 AS NUMERIC(38,0))
END AS NUMERIC(10,0)) AS CHAR_LENGTH, CAST(
CASE (CAST(
CASE T.TYPNAME
WHEN ‘BPCHAR‘ THEN ‘CHAR‘
ELSE CAST(T.TYPNAME AS TEXT)
END AS CHARACTER VARYING(106 BYTE)) = ANY (ARRAY[‘VARCHAR‘, ‘BPCHAR‘, ‘NCHAR‘, ‘NVARCHAR‘])) AND ATTR.ATTTYPMOD <> (-1)
WHEN true THEN
CASE ATTR.ATTTYPMOD >> 16
WHEN 0 THEN ‘C‘
ELSE ‘B‘
END
ELSE NULL
END AS CHARACTER VARYING(1 CHAR)) AS CHAR_USED, CAST(‘NO‘ AS CHARACTER VARYING(3 CHAR)) AS V80_FMT_IMAGE, CAST(‘NO‘ AS CHARACTER VARYING(3 CHAR)) AS DATA_UPGRADED, CAST(‘NO‘ AS CHARACTER VARYING(3 CHAR)) AS HIDDEN_COLUMN, CAST(‘NO‘ AS CHARACTER VARYING(3 CHAR)) AS VIRTUAL_COLUMN, CAST(0 AS NUMERIC(10,0)) AS SEGMENT_COLUMN_ID, CAST(ATTR.ATTNUM AS NUMERIC(10,0)) AS INTERNAL_COLUMN_ID, CAST(NULL AS CHARACTER VARYING(15 CHAR)) AS HISTOGRAM, CAST(NULL AS CHARACTER VARYING(4000 CHAR)) AS QUALIFIED_COL_NAME, ATTR.ATTISENCRYPTED AS ISENCRYPTED, CAST(ATTR.ATTCOMPMETHOD AS CHARACTER VARYING(1 CHAR)) AS COMPMETHOD, CAST(ATTR.ATTSTORAGE AS CHARACTER VARYING(1 CHAR)) AS "STORAGE", CAST((( SELECT "IDENTITY".RELNAME
FROM SYS_CLASS "IDENTITY"
WHERE "IDENTITY".OID = ATTR.ATTSEQOID)) AS CHARACTER VARYING(30 BYTE)) AS IDENTITY_NAME, GET_COL_CHECKS(ATTR.ATTRELID, CAST(ATTR.ATTNUM AS INTEGER)) AS CHECKS, GET_COL_FOREIGNS(ATTR.ATTRELID, CAST(ATTR.ATTNUM AS INTEGER)) AS FOREIGNS, GET_COL_PRIMARY(ATTR.ATTRELID, CAST(ATTR.ATTNUM AS INTEGER)) AS PRIMARY_KEY, GET_COL_UNIQUES(ATTR.ATTRELID, CAST(ATTR.ATTNUM AS INTEGER)) AS UNIQUES, ARRAY_CAT(ATTR.ATTACL, C.RELACL) AS GRANTS,
GET_COL_INDEXS(C.OID, CAST(ATTR.ATTNUM AS INTEGER)) AS INDEXS,
CAST(((( SELECT
CASE PARTTABLES.PARTITIONKEY
WHEN NULL THEN NULL
ELSE
CASE STRPOS(PARTTABLES.PARTITIONKEY, CAST(ATTR.ATTNAME AS TEXT))
WHEN 0 THEN NULL
ELSE PARTTABLES.PARTITIONTYPE
END
END AS "CASE"
FROM SYS_PARTTABLES PARTTABLES, SYS_NAMESPACE NAMESPACE
WHERE CAST(C.RELNAME AS TEXT) = PARTTABLES.TABLENAME AND NAMESPACE.OID = C.RELNAMESPACE AND NAMESPACE.NSPNAME = PARTTABLES.SCHEMANAME))) AS CHARACTER VARYING(30 BYTE)) AS PARTITIONTYPE, ( SELECT
CASE PARTTABLES.PARTITIONKEY
WHEN NULL THEN NULL
ELSE
CASE STRPOS(PARTTABLES.PARTITIONKEY, CAST(ATTR.ATTNAME AS TEXT))
WHEN 0 THEN NULL
ELSE PARTTABLES.PARTITIONKEY
END
END AS "CASE"
FROM SYS_PARTTABLES PARTTABLES, SYS_NAMESPACE NAMESPACE
WHERE CAST(C.RELNAME AS TEXT) = PARTTABLES.TABLENAME AND NAMESPACE.OID = C.RELNAMESPACE AND NAMESPACE.NSPNAME = PARTTABLES.SCHEMANAME) AS PARTITIONKEY
FROM SYS_INDEX INDEX,SYS_CLASS C,SYS_TYPE T, SYS_AUTHID AU, SYS_ATTRIBUTE ATTR
LEFT JOIN SYS_ATTRDEF DEF ON ATTR.ATTNUM = DEF.ADNUM AND ATTR.ATTRELID = DEF.ADRELID
WHERE ATTR.ATTNUM > 0 AND ATTR.ATTTYPID = T.OID AND ATTR.ATTRELID = C.OID AND T.TYPOWNER = AU.OID
AND ATTR.ATTRELID = INDEX.INDRELID
AND ATTR.ATTNUM = any(INDEX.INDKEY)
AND NSPNAME=‘ZYJ‘ AND TABLE_NAME=‘TEST‘;

以上是关于kingbase7获取唯一索引和子分区键的view的主要内容,如果未能解决你的问题,请参考以下文章

如何从表中删除分区?

具有唯一索引和主键的列给出唯一约束违规

MySQL之唯一索引外键的变种SQL语句数据行操作补充

没有主键的事务复制(唯一索引)

powerdesigner设置唯一键,但不是主键的方式

:分区