我正在尝试将Lucidchart的ERD导入工具与BigQuery一起使用,该工具没有为其编写标准查询(但它确实具有针对Oracle,SQL Server,MySQL和PostgreSQL的查询)。
Lucidchart的支持者说我应该尝试SQL Server查询,
SELECT
'sqlserver' dbms,
t.TABLE_CATALOG,
t.TABLE_SCHEMA,
t.TABLE_NAME,
c.COLUMN_NAME,
c.ORDINAL_POSITION,
c.DATA_TYPE,
c.CHARACTER_MAXIMUM_LENGTH,
n.CONSTRAINT_TYPE,
k2.TABLE_SCHEMA,
k2.TABLE_NAME,
k2.COLUMN_NAME
FROM
INFORMATION_SCHEMA.TABLES t
LEFT JOIN
INFORMATION_SCHEMA.COLUMNS c
ON
t.TABLE_CATALOG=c.TABLE_CATALOG
AND t.TABLE_SCHEMA=c.TABLE_SCHEMA
AND t.TABLE_NAME=c.TABLE_NAME
LEFT JOIN (INFORMATION_SCHEMA.KEY_COLUMN_USAGE k
JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS n
ON
k.CONSTRAINT_CATALOG=n.CONSTRAINT_CATALOG
AND k.CONSTRAINT_SCHEMA=n.CONSTRAINT_SCHEMA
AND k.CONSTRAINT_NAME=n.CONSTRAINT_NAME
LEFT JOIN
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS r
ON
k.CONSTRAINT_CATALOG=r.CONSTRAINT_CATALOG
AND k.CONSTRAINT_SCHEMA=r.CONSTRAINT_SCHEMA
AND k.CONSTRAINT_NAME=r.CONSTRAINT_NAME)
ON
c.TABLE_CATALOG=k.TABLE_CATALOG
AND c.TABLE_SCHEMA=k.TABLE_SCHEMA
AND c.TABLE_NAME=k.TABLE_NAME
AND c.COLUMN_NAME=k.COLUMN_NAME
LEFT JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE k2
ON
k.ORDINAL_POSITION=k2.ORDINAL_POSITION
AND r.UNIQUE_CONSTRAINT_CATALOG=k2.CONSTRAINT_CATALOG
AND r.UNIQUE_CONSTRAINT_SCHEMA=k2.CONSTRAINT_SCHEMA
AND r.UNIQUE_CONSTRAINT_NAME=k2.CONSTRAINT_NAME
WHERE
t.TABLE_TYPE='BASE TABLE';
我修复了第一个问题,即在表之前需要bigquery数据集名称,即`dataset`.INFORMATION_SCHEMA.TABLES;但是,`dataset`.INFORMATION_SCHEMA.TABLE_CONSTRAINTS似乎不存在。
是否可以在SELECT语句中选择此信息?
更新:
以下是示例输出的几行(标题即将推出):
postgresql,quizbowl,public,database_migrations,applied,2,timestamp with time zone,,,,,
postgresql,quizbowl,public,database_migrations,name,1,character varying,,,,,
postgresql,quizbowl,public,load_data_versions,applied,3,timestamp with time zone,,,,,
postgresql,quizbowl,public,load_data_versions,checksum,2,character varying,,,,,
postgresql,quizbowl,public,load_data_versions,name,1,character varying,,,,,
postgresql,quizbowl,public,event_team,create_date,4,timestamp with time zone,,,,,
postgresql,quizbowl,public,event_team,create_user_id,3,integer,,FOREIGN KEY,public,user_account,user_id