获取雪花中的主键列列表

时间:2020-09-24 17:19:35

标签: constraints primary-key snowflake-cloud-data-platform composite-primary-key information-schema

我有一个复合主键。在Information_Schema.Table_Constraints中,我可以看到1条该键的记录,但是在构成此主键的列上没有信息。

在哪里可以找到约束列的列表?

3 个答案:

答案 0 :(得分:0)

我有一个客户,他需要获取其表的复合主键,这些键作为结果集中的行返回。这是我为他们编写的用户定义表功能(UDTF)。如果您不希望将其作为表使用,则代码很容易修改,可以返回为数组,JSON等。

/**************************************************************************************************************
*                                                                                                             *
*  Set up test tables with four types of primary key: Named composite, unnamed composite, inline, and none.   * 
*                                                                                                             *
**************************************************************************************************************/
 
-- Named multi-column PK
create or replace temporary table table1
(
column_name1 number NOT NULL,
column_name2 number NOT NULL,
column_name3 string,
CONSTRAINT Constraint_name PRIMARY KEY (column_name1, column_name2)
);
 
-- Unnamed multi-column PK
create temporary table table2
(
column_name1 number NOT NULL,
column_name2 number NOT NULL,
column_name3 string,
PRIMARY KEY (column_name1, column_name2)
);
 
-- Inline single-column PK
create or replace temporary table table3
(
column_name1 number primary key,
column_name2 number NOT NULL,
column_name3 string
);
 
-- No PK defined
create or replace temporary table table4
(
column_name1 number,
column_name2 number,
column_name3 string
);
 
/********************************************************************************************************
*                                                                                                       *
* User defined table function (UDTF) to get primary keys for a table.                                   *
*                                                                                                       *
* @param  {string}:  TABLE_DDL    The DDL for the table to get the PKs. Usually use get_ddl.            *
* @return {table}:                A table with the columns comprising the table's primary key           *
*                                                                                                       *
********************************************************************************************************/
create or replace function GET_PK_COLUMNS(TABLE_DDL string)
returns table (PK_COLUMN string)
language javascript
as
$$
{
    processRow: function get_params(row, rowWriter, context){
 
        var pkCols = getPKs(row.TABLE_DDL);
        for (i = 0; i < pkCols.length; i++) {
            rowWriter.writeRow({PK_COLUMN: pkCols[i] }); 
        }
         
        function getPKs(tableDDL) {
            var keyword = "primary key";
            var ins = -1;
            var s = tableDDL.split("\n");
            for (var i = 0; i < s.length; i++) {  
                ins = s[i].indexOf(keyword);
                if (ins != -1) {
                    var colList = s[i].substring(ins + keyword.length);
                    colList = colList.replace("(", "");
                    colList = colList.replace(")", "");
                    var colArray = colList.split(",");
                    for (c = 0; c < colArray.length; c++) {
                        colArray[c] = colArray[c].trim();
                    }
                    return colArray;
                }
            }
            return [];  // No PK
        }
    }
}
$$;
 
/**************************************************************************************************************
*                                                                                                             *
*  Test execution of the UDTF.                                                                                * 
*                                                                                                             *
**************************************************************************************************************/
 
select * from table(get_pk_columns(get_ddl('table', 'table1'))) PKS;  -- Multi-column PK with named constraint
select * from table(get_pk_columns(get_ddl('table', 'table2'))) PKS;  -- Multi-column PK with no name for constraint
select * from table(get_pk_columns(get_ddl('table', 'table3'))) PKS;  -- Single column PK inline definition
select * from table(get_pk_columns(get_ddl('table', 'table4'))) PKS;  -- No PKs

答案 1 :(得分:0)

您可以使用SHOW PRIMARY KEYS命令来查找组成给定主键的列。例如:

ssh -v srv.bamboo@AB280182.AB.corp
OpenSSH_7.6p1 Ubuntu-4ubuntu0.3, OpenSSL 1.0.2n  7 Dec 2017
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to AB280182.AB.corp [172.17.14.153] port 22.
debug1: Connection established.
debug1: identity file /home/bamboo/.ssh/id_rsa type 0
debug1: key_load_public: No such file or directory
debug1: identity file /home/bamboo/.ssh/id_rsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6p1 Ubuntu-4ubuntu0.3
debug1: Remote protocol version 2.0, remote software version OpenSSH_for_Windows_8.1
debug1: match: OpenSSH_for_Windows_8.1 pat OpenSSH* compat 0x04000000
debug1: Authenticating to AB280182.AB.corp:22 as 'srv.bamboo'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:+CNyyEMTqp5kOouPA9wuJQ
debug1: Host 'AB280182.AB.corp' is known and matches the ECDSA host key.
debug1: Found key in /home/bamboo/.ssh/known_hosts:7
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 134217728 blocks
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:/ny5iiXFzfjK1DuIDgnAKZb/R/cm3KVsKU /home/bamboo/.ssh/id_rsa
debug1: Server accepts key: pkalg rsa-sha2-512 blen 279
debug1: Authentication succeeded (publickey).
Authenticated to AB280182.AB.corp ([172.17.14.153]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: Sending environment.
Microsoft Windows [Version 10.0.17134.1726]
(c) 2018 Microsoft Corporation. All rights reserved.

您还可以使用以下命令查看给定上下文(您有权访问)中所有表的主键:

CREATE OR REPLACE TABLE t1 (a INT, b INT, PRIMARY KEY (a,b));
SHOW PRIMARY KEYS IN TABLE t1;

created_on  database_name   schema_name table_name  column_name key_sequence    constraint_name comment
2020-09-24 13:48:50.208 -0700   ANDREW_DB   PUBLIC  T1  A   1   SYS_CONSTRAINT_159d987b-8160-458e-bad1-50abda169c16 
2020-09-24 13:48:50.208 -0700   ANDREW_DB   PUBLIC  T1  B   2   SYS_CONSTRAINT_159d987b-8160-458e-bad1-50abda169c16 

答案 2 :(得分:-1)

在数据库 TEST_DB 中显示主键; SELECT * FROM TABLE(RESULT_SCAN(LAST_QUERY_ID()));

相关问题