{"id":1173,"date":"2019-04-29T02:11:46","date_gmt":"2019-04-28T14:11:46","guid":{"rendered":"http:\/\/www.helenanderson.co.nz\/?p=1173"},"modified":"2020-05-10T20:18:15","modified_gmt":"2020-05-10T08:18:15","slug":"search-sys-tables","status":"publish","type":"post","link":"https:\/\/www.helenanderson.co.nz\/search-sys-tables\/","title":{"rendered":"Search with system tables"},"content":{"rendered":"\n
\n\n\n\n

Your database<\/a> doesn’t just contain your data.<\/p>\n\n\n\n

It also contains data about your data in System Tables.<\/p>\n\n\n\n


\n\n\n\n

In SQL Server<\/a> these are often referred to as System Tables and views. They can be found in the master database, which holds data about the database. And in the system views within each database for specific information about each database<\/a>.<\/p>\n\n\n\n

Examples of System Views<\/h4>\n\n\n\n

sys.objects<\/strong> – shows each object, type and created date<\/p>\n\n\n\n

sys.indexes<\/strong> – shows each index and type<\/p>\n\n\n\n

information_schema.columns<\/strong> – shows each column, it’s position and datatype<\/p>\n\n\n\n


\n\n\n\n

In PostgreSQL, a similar collection of tables can be found in the information_schema and PostgreSQL catalogue.<\/p>\n\n\n\n

Examples of Catalog Objects<\/h4>\n\n\n\n

information_schema.tables<\/strong> – each object, type and created date<\/p>\n\n\n\n

pg_index<\/strong> – shows each index and type<\/p>\n\n\n\n

information_schema.columns<\/strong> – shows each column, it’s position and datatype<\/p>\n\n\n\n


\n\n\n\n

Useful scripts<\/h2>\n\n\n\n

To illustrate how useful these can be, and which views and tables you need, here are six scripts in SQL Server and PostgreSQL.<\/p>\n\n\n\n

Count columns<\/a>
Count rows<\/a>
Show data types<\/a>
Search for a column name<\/a>
Show all tables in a schema<\/a>
Show number of tables in each schema<\/a><\/strong><\/p><\/blockquote>\n\n\n\n

Tested using SQL Server Standard<\/a> and Aurora (Postgres)<\/a><\/em><\/p><\/blockquote>\n\n\n\n


\n\n\n\n

<\/a><\/p>\n\n\n\n

Count Columns<\/h2>\n\n\n\n

This query returns a list of tables, in alphabetical order, with a count of the columns. Add your schema or database name to the code and run the query.<\/p>\n\n\n\n


\n\n\n\n
-- SQL Server\n\nselect\n  table_schema, \n  table_name, \n  count(*) as column_count\nfrom information_schema.columns\nwhere table_catalog = 'mydatabase' -- put your DB here\ngroup by \n  table_schema, \n  table_name\norder by \n  table_schema, \n  table_name<\/pre>\n\n\n\n
\n\n\n\n
-- PostgreSQL\n\nselect \n  table_name, \n  count(column_name)\nfrom \n  information_schema.columns\nwhere \n  table_schema = 'myschema' -- put your schema here\ngroup by \n  table_name\norder by \n  table_name;<\/pre>\n\n\n\n
\n\n\n\n

<\/a><\/p>\n\n\n\n

Count Rows<\/h2>\n\n\n\n

This query returns a list of tables, in alphabetical order, with a count of the rows. In the case of SQL Server, this column will contain the schema and table name. Add your schema or database name to the code and run the query.<\/p>\n\n\n\n


\n\n\n\n
-- SQL Server\n\nuse mydatabase -- put your DB here\ngo\n\nselect\nquotename(schema_name(sobj.schema_id)) \n+ '.' + quotename(sobj.name) as table_name,\nsum(sptn.rows) as row_count\nfrom\n  sys.objects as sobj\ninner join \n  sys.partitions as sptn on sobj.object_id = sptn.object_id\nwhere\n  sobj.type = 'U'\n  and sobj.is_ms_shipped = 0x0\n  and index_id < 2 -- 0:Heap, 1:Clustered\ngroup by \n  sobj.schema_id, \n  sobj.name\norder by \n  table_name<\/pre>\n\n\n\n
\n\n\n\n
-- PostgreSQL\n\nselect\n  schemaname,\n  relname,\n  n_live_tup\nfrom \n  pg_stat_user_tables\nwhere \n  schemaname = 'myschema'\norder by \n  relname;<\/pre>\n\n\n\n
\n\n\n\n

<\/a><\/p>\n\n\n\n

Show data types<\/h2>\n\n\n\n

This query returns a list of tables, in alphabetical order, with their column names, data types and lengths. In the case of SQL Server, this also has a column for the schema name. Add your schema or database name to the code and run the query.<\/p>\n\n\n\n


\n\n\n\n
-- SQL Server\n\nuse mydatabase -- put your DB here\ngo\n\nselect \n  c.table_schema,  \n  c.table_name, \n  c.column_name, \n  c.data_type, \n  c.character_maximum_length\nfrom \n  information_schema.columns c\njoin sys.objects o on c.table_name = o.name\nwhere \n  type = 'u' -- u is for user created tables\norder by \n  c.table_schema<\/pre>\n\n\n\n
\n\n\n\n
-- PostgreSQL\n\nselect \n  column_name, \n  data_type \nfrom \n  information_schema.columns \nwhere \n  table_schema = 'myschema';\n<\/pre>\n\n\n\n
\n\n\n\n

<\/a><\/p>\n\n\n\n

Search for a column name<\/h2>\n\n\n\n

This query returns a list of column names that match the search criteria in the WHERE clause. Add your schema or database name to the code and run the query.<\/p>\n\n\n\n


\n\n\n\n
-- SQL Server\n\nuse mydatabase -- put your DB here\ngo\n\nselect\n  scol.name as column_name,\n  tab.name as table_name\nfrom\n  sys.columns scol\njoin sys.tables tab on scol.object_id = tab.object_id\nwhere \n  scol.name = 'mycolumn' --put your column name here<\/pre>\n\n\n\n
\n\n\n\n
-- PostgreSQL\n\nselect \n  column_name,\n  table_name\nfrom \n  information_schema.columns \nwhere \n  column_name = 'mycolumn' --put your column name here\n  and table_schema = 'myschema'  -- put your schema here<\/pre>\n\n\n\n
\n\n\n\n

<\/a><\/p>\n\n\n\n

Show all tables in a schema<\/h2>\n\n\n\n

This query returns a list of tables, in alphabetical order, from the schema or database requested. Add your schema or database name to the code and run the query.<\/p>\n\n\n\n


\n\n\n\n
-- SQL Server\n\nuse mydatabase -- put your DB here\ngo\n\nselect \n  [table] = s.name + N'.' + t.name\nfrom \n  sys.tables AS t\ninner join sys.schemas as s on t.schema_id = s.schema_id\norder by \n  [table]<\/pre>\n\n\n\n
\n\n\n\n
-- PostgreSQL\n\nselect\n  table_name \nfrom \n  information_schema.tables \nwhere \n  table_schema = 'myschema'  -- put your schema here\norder by \n  table_name<\/pre>\n\n\n\n
\n\n\n\n

<\/a><\/p>\n\n\n\n

Show number of tables in each schema<\/h2>\n\n\n\n

This query returns a list of tables, in alphabetical order, with their last modified and created date. Add your schema or database name to the code and run the query.<\/p>\n\n\n\n


\n\n\n\n
-- SQL Server\n\nuse mydatabase -- put your DB here\ngo\n\nselect\n  schema_name(schema_id) as schema_name,\n  count(name) as table_count\nfrom     \n  sys.tables\ngroup by  \n  schema_name(schema_id)\norder by  \n  schema_name(schema_id)<\/pre>\n\n\n\n
\n\n\n\n
-- PostgreSQL\n\nselect \n  schemaname, \n  count(tablename)\nfrom \n  pg_tables \ngroup by \n  schemaname\norder by \n  schemaname<\/pre>\n\n\n\n
\n\n\n\n

Photo by Jenna Hamra<\/a><\/strong> from Pexels<\/a><\/strong> <\/p>\n","protected":false},"excerpt":{"rendered":"

Your database doesn\u2019t just contain your data. It also contains data about your data.<\/p>\n","protected":false},"author":1,"featured_media":2575,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":""},"categories":[214],"tags":[198,199],"yoast_head":"\nSearch with system tables | Helen Anderson<\/title>\n<meta name=\"description\" content=\"Your database doesn't just contain your data. It also contains data about your data. Here's how to use System Tables to search.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.helenanderson.co.nz\/search-sys-tables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Search with system tables | Helen Anderson\" \/>\n<meta property=\"og:description\" content=\"Your database doesn't just contain your data. It also contains data about your data. Here's how to use System Tables to search.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.helenanderson.co.nz\/search-sys-tables\/\" \/>\n<meta property=\"og:site_name\" content=\"Helen Anderson\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-28T14:11:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-10T08:18:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.helenanderson.co.nz\/wp-content\/uploads\/2019\/04\/search-sql-system-tables.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"853\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@helenanders26\" \/>\n<meta name=\"twitter:site\" content=\"@helenanders26\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"4 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/13.237.200.153\/#website\",\"url\":\"https:\/\/13.237.200.153\/\",\"name\":\"Helen Anderson\",\"description\":\"Data Analyst | Technical Writer\",\"publisher\":{\"@id\":\"https:\/\/13.237.200.153\/#\/schema\/person\/4677a271385757403307fb29bd14d7bf\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/13.237.200.153\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-NZ\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.helenanderson.co.nz\/search-sys-tables\/#primaryimage\",\"inLanguage\":\"en-NZ\",\"url\":\"https:\/\/www.helenanderson.co.nz\/wp-content\/uploads\/2019\/04\/search-sql-system-tables.jpg\",\"width\":1280,\"height\":853,\"caption\":\"search-sql-system-tables\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.helenanderson.co.nz\/search-sys-tables\/#webpage\",\"url\":\"https:\/\/www.helenanderson.co.nz\/search-sys-tables\/\",\"name\":\"Search with system tables | Helen Anderson\",\"isPartOf\":{\"@id\":\"https:\/\/13.237.200.153\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.helenanderson.co.nz\/search-sys-tables\/#primaryimage\"},\"datePublished\":\"2019-04-28T14:11:46+00:00\",\"dateModified\":\"2020-05-10T08:18:15+00:00\",\"description\":\"Your database doesn't just contain your data. It also contains data about your data. Here's how to use System Tables to search.\",\"inLanguage\":\"en-NZ\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.helenanderson.co.nz\/search-sys-tables\/\"]}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/www.helenanderson.co.nz\/search-sys-tables\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.helenanderson.co.nz\/search-sys-tables\/#webpage\"},\"author\":{\"@id\":\"https:\/\/13.237.200.153\/#\/schema\/person\/4677a271385757403307fb29bd14d7bf\"},\"headline\":\"Search with system tables\",\"datePublished\":\"2019-04-28T14:11:46+00:00\",\"dateModified\":\"2020-05-10T08:18:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.helenanderson.co.nz\/search-sys-tables\/#webpage\"},\"publisher\":{\"@id\":\"https:\/\/13.237.200.153\/#\/schema\/person\/4677a271385757403307fb29bd14d7bf\"},\"image\":{\"@id\":\"https:\/\/www.helenanderson.co.nz\/search-sys-tables\/#primaryimage\"},\"keywords\":\"analysis,sql\",\"articleSection\":\"Data Analysis\",\"inLanguage\":\"en-NZ\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/13.237.200.153\/#\/schema\/person\/4677a271385757403307fb29bd14d7bf\",\"name\":\"Helen Anderson\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/13.237.200.153\/#personlogo\",\"inLanguage\":\"en-NZ\",\"url\":\"https:\/\/www.helenanderson.co.nz\/wp-content\/uploads\/2019\/11\/helen-anderson-profile-selects-FA-1000.jpg\",\"width\":1000,\"height\":1000,\"caption\":\"Helen Anderson\"},\"logo\":{\"@id\":\"https:\/\/13.237.200.153\/#personlogo\"},\"description\":\"Hi, I'm Helen. I'm a data analyst, technical writer, and AWS Data Hero. I interpret the story behind the numbers, build data applications, and grow analyst and developer communities - currently at Kiwibank.\",\"sameAs\":[\"http:\/\/www.helenanderson.co.nz\/\",\"https:\/\/www.instagram.com\/helenanders26\/\",\"https:\/\/www.linkedin.com\/in\/helenanders26\/\",\"https:\/\/twitter.com\/helenanders26\",\"https:\/\/www.youtube.com\/channel\/UCttVhJizwkhgmMlDBMUE0wQ\"]}]}<\/script>\n","_links":{"self":[{"href":"https:\/\/www.helenanderson.co.nz\/wp-json\/wp\/v2\/posts\/1173"}],"collection":[{"href":"https:\/\/www.helenanderson.co.nz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.helenanderson.co.nz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.helenanderson.co.nz\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.helenanderson.co.nz\/wp-json\/wp\/v2\/comments?post=1173"}],"version-history":[{"count":22,"href":"https:\/\/www.helenanderson.co.nz\/wp-json\/wp\/v2\/posts\/1173\/revisions"}],"predecessor-version":[{"id":3154,"href":"https:\/\/www.helenanderson.co.nz\/wp-json\/wp\/v2\/posts\/1173\/revisions\/3154"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.helenanderson.co.nz\/wp-json\/wp\/v2\/media\/2575"}],"wp:attachment":[{"href":"https:\/\/www.helenanderson.co.nz\/wp-json\/wp\/v2\/media?parent=1173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.helenanderson.co.nz\/wp-json\/wp\/v2\/categories?post=1173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.helenanderson.co.nz\/wp-json\/wp\/v2\/tags?post=1173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}