Is there a way to export schema(s), table(s), columns and their dependencies as a diagram in Snowflake?
It'd be great to achieve initial sorts of an ERD, such as this one in the picture: https://www.snowflake.com/blog/using-snowflake-information-schema/
Best wishes,
I
enter image description here
I looked for features/functionalities in the cloud and desktop clients. However, I found nothing similar.
I wonder if I could wrap a script in DBT to solve problems like these.
I've had some success on this with through LucidCharts' ERD tool (can't remember right now if it requires the paid version or not) but generally you'd run something like the snowflake converted version of their postgres script:
use database my_database;
use warehouse snowflake_wh;
SELECT
'postgresql' AS dbms,
t.table_catalog,
t.table_schema,
t.table_name,
c.column_name,
c.ordinal_position,
c.data_type,
c.character_maximum_length,
null as constraint_type,
null as "k2.table_schema",
null as "k2.table_name",
null as "k2.column_name"
FROM
information_schema.COLUMNS c
left join information_schema.TABLES t on t.TABLE_CATALOG = c.TABLE_CATALOG
AND t.TABLE_SCHEMA = c.TABLE_SCHEMA
AND t.TABLE_NAME = c.TABLE_NAME
WHERE
t.table_type = 'BASE TABLE'
AND t.table_schema NOT IN('information_schema');
And then upload back into the tool as a csv export.
Result:
Which, I have not experimented heavily with making it play nicely with views & references, so far just base tables and then doing the column references myself.
Related
We have a lot of tables in Snowflake, none had Primary Keys (I've just added them in) but now I'm feeling overwhelmed trying to figure out how to create Foreign Keys without going through each table to see the link between data in two tables? Is there a script that I can create to even do this?
P.S. Below is one created to check existing PK's and if it doesn't exist it shows the
command to run to create, if this helps anyone:
select distinct
CONCAT(
'ALTER TABLE "',table_catalog, '"."',
table_schema,'"."',
table_name,
'" ADD PRIMARY KEY (',
column_name,
');'
)
from information_schema.columns c
where table_schema = 'SCHEMA_NAME_HERE'
and column_name = 'ID'
and not exists (
select 1 from
information_schema.table_constraints
where constraint_schema = c.table_schema
and table_name = c.table_name
and constraint_type = 'PRIMARY KEY'
)
The typical reason for adding constraints to Snowflake are to simplify JOINs in BI tools and general documentation.
If you have ERWin or similar tool, and you data has good naming conventions, reverse engineering typically has an option to infer constraints from column names.
Best solution is to pull the meta-data from the original source (assuming it existed there).
On occasion, I'm interested in getting a list of columns in one of the tables or views in my SQL Server 2008 R2 database. It's useful, for example, if you're building database documentation without using an expensive off-the-shelf product.
What's an easy way to get this information?
In SQL Server 2008 R2 (among other versions), there are system views provided automatically with every database. As long as you are connected to the database where your table resides, you can run a query like this:
DECLARE #TableViewName NVARCHAR(128)
SET #TableViewName=N'MyTableName'
SELECT b.name AS ColumnName, c.name AS DataType,
b.max_length AS Length, c.Precision, c.Scale, d.value AS Description
FROM sys.all_objects a
INNER JOIN sys.all_columns b
ON a.object_id=b.object_id
INNER JOIN sys.types c
ON b.user_type_id=c.user_type_id
LEFT JOIN sys.extended_properties d
ON a.object_id=d.major_id AND b.column_id=d.minor_id AND d.name='MS_Description'
WHERE a.Name=#TableViewName
AND a.type IN ('U','V')
Of course, this is just a starting point. There are many other system views and columns available in every database. You can find them through SQL Server Management Studio under Views > "System Views
Another way is querying the INFORMATION_SCHEMA.columns view as detailed here:
Information_Schema - COLUMNS
This will give you information for all the columns in the current database (and what table/view they belong to) including their datatypes, precision, collation and whether they allow nulls etc
Usefully as well, these views are maintained in multiple DBMS programs too, so you could potentially use the same or similar query to get the same information regarding a MySQL database as you can a SQL Server DB, which could be useful if you are developing on multiple platorms.
sp_columns returns detailed information about each of the columns in the table. SO Answer
sp_columns #tablename
sp_help returns detailed information about the entire table including the columns and constraints. SO Answer
sp_help #tablename
To get a list of Columns of a view with some other information about the column you can use the following:
SELECT * FROM sys.columns c, sys.views v
WHERE c.object_id = v.object_id
AND v.name = 'view_Name'
GO
And if you only want the list of Column Name use this.
SELECT c.name
FROM sys.columns c, sys.views v
WHERE c.object_id = v.object_id
AND v.name = 'view_UserAssessphers'
GO
exec sp_helptext <your view name>
Also works for the view only, blachniet's answer is best if you need details on the columns in the table.
In a new query window, type the name of the view/table, highlight it, and press Alt-F1. This will run sp_help, like blachniet suggested.
simple list of column names without any further information.
SELECT COLUMN_NAME FROM TABLENAME.INFORMATION_SCHEMA.COLUMNS;
Replace TABLENAME with your tables name.
Is there any way to find particular column in Linked Server's database within all tables.
I guess solution lies in
EXEC sp_columns_ex
SELECT t.name as TableName, c.name as ColumnName
FROM servernamehere.databasenamehere.sys.columns c
INNER JOIN servernamehere.databasenamehere.sys.tables t ON c.object_id = t.object_id
WHERE c.name like '%yoursearchhere%'
How about this:
EXECUTE [MyLinkedServer].[MyLinkedDB].dbo.sp_executesql
N'SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE ...'
And fill in the where clause depending on what you want to search for?
An alternative would be creating a view that selects from INFORMATION_SCHEMA.COLUMNS and then you query that instead.
Reference for Information_Schema.Columns
It would depend on which database your linked server is pointing to. For example, if it is Oracle, you would use Oracle syntax, if it is SQL Server, Sql Server Syntax.
The fact that you are querying the schema through a Linked server shouldnt matter.
I have to add a few columns to a table and I also need to add these columns to all the views that use this table.
Is it possible to get a list of all the views in a database that use a certain table?
This should do it:
SELECT *
FROM INFORMATION_SCHEMA.VIEWS
WHERE VIEW_DEFINITION like '%YourTableName%'
To find table dependencies you can use the sys.sql_expression_dependencies catalog view:
SELECT
referencing_object_name = o.name,
referencing_object_type_desc = o.type_desc,
referenced_object_name = referenced_entity_name,
referenced_object_type_desc =so1.type_desc
FROM sys.sql_expression_dependencies sed
INNER JOIN sys.views o ON sed.referencing_id = o.object_id
LEFT OUTER JOIN sys.views so1 ON sed.referenced_id =so1.object_id
WHERE referenced_entity_name = 'Person'
You can also try out ApexSQL Search a free SSMS and VS add-in that also has the View Dependencies feature. The View Dependencies feature has the ability to visualize all SQL database objects’ relationships, including those between encrypted and system objects, SQL server 2012 specific objects, and objects stored in databases encrypted with Transparent Data Encryption (TDE)
Disclaimer: I work for ApexSQL as a Support Engineer
If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).
It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??
I find this works better:
SELECT type, *
FROM sys.objects
WHERE OBJECT_DEFINITION(object_id) LIKE '%' + #ObjectName + '%'
AND type IN ('V')
ORDER BY name
Filtering VIEW_DEFINTION inside INFORMATION_SCHEMA.VIEWS is giving me quite a few false positives.
SELECT VIEW_NAME
FROM INFORMATION_SCHEMA.VIEW_TABLE_USAGE
WHERE TABLE_NAME = 'Your Table'
select your table -> view dependencies -> Objects that depend on
Simplest way to find used view or stored procedure for the tableName using below query -
exec dbo.dbsearch 'Your_Table_Name'
I want Microsoft SQL server queries corresponding to the following Oracle queries
//get schema of a table
desc tablename;
//get the names of all tables
select * from tab;
You have access to that info through metadata tables. Check this link out.
INFORMATION_SCHEMA.Tables -> gives you access to table names
INFORMATION_SCHEMA.Columns -> gives you access to column names
Here is another link with a complete list of catalog tables.
INFORMATION_SCHEMA.CHECK_CONSTRAINTS
INFORMATION_SCHEMA.COLUMN_DOMAIN_USAGE
INFORMATION_SCHEMA.COLUMN_PRIVILEGES
INFORMATION_SCHEMA.COLUMNS
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE
INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE
INFORMATION_SCHEMA.DOMAIN_CONSTRAINTS
INFORMATION_SCHEMA.DOMAINS
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
INFORMATION_SCHEMA.PARAMETERS
INFORMATION_SCHEMA.REFERENCIAL_CONSTRAINTS
INFORMATION_SCHEMA.ROUTINE_COLUMNS
INFORMATION_SCHEMA.ROUTINES
INFORMATION_SCHEMA.SCHEMA_DATA
INFORMATION_SCHEMA.TABLE_CONSTRAINTS
INFORMATION_SCHEMA.TABLE_PRIVILEGES
INFORMATION_SCHEMA.TABLES
INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
INFORMATION_SCHEMA.VIEW_TABLE_USAGE
INFORMATION_SCHEMA.VIEWS
Keep in mind though, that you will probably need special permission to access those tables/views.
The other thing you might try as an alternative is using ODBC, Java, .NET or any other programming language or library to access metadata information. They have complete access to that through their APIs.
Table desctiption:
sp_help table_name
All tables in the current database:
select * from sysobjects where xtype='U'
And you can use sysobjects, syscolumns, sysindexes etc. tables to get the information about database structure.