information-schema

Finding columns that are NOT NULL in PostgreSQL

余生颓废 提交于 2019-12-21 06:46:18
问题 I had an assignment for each table to count nullable columns. Easy: SELECT table_name, count(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE is_nullable='NO' GROUP BY table_name; Now I have to modify this to count "columns that have property "NOT NULL"". Will the following code do this or will it just check weather column name is not null? CREATE TEMP TABLE A AS SELECT DISTINCT column_name, table_name AS name FROM INFORMATION_SCHEMA.COLUMNS WHERE column_name IS NOT NULL GROUP BY table_name, column

How to get the dimensionality of an ARRAY column?

孤者浪人 提交于 2019-12-20 07:27:37
问题 I'm working on a project that collects information about your schema from the database directly. I can get the data_type of the column using information_schema.columns , which will tell me if it's an ARRAY or not. I can also get the underlying type ( integer , bytea etc) of the ARRAY by querying information_schema.element_types as described here: https://www.postgresql.org/docs/9.1/static/infoschema-element-types.html My problem is that I also need to know how many dimensions the array has,

Mysql inconsistent number of rows count(*) vs table.table_rows in information_schema

不羁的心 提交于 2019-12-20 05:52:08
问题 i came across a strange phenomenon, and i hope, that someone can explain this to me: i have a some "static" tables (they change once per day). mysql> select 'appObjectGroups' as tbl, count(*) as num from appObjectGroups union select 'appObjectDependencies' as tbl, count(*) as num from appObjectDependencies union select 'appObjectUrls' as tbl, count(*) as num from appObjectUrls union select 'appObjectValues' as tbl, count(*) as num from appObjectValues union select 'appObjects;' as tbl, count(

Set empty strings ('') to NULL in the whole database

邮差的信 提交于 2019-12-18 05:20:33
问题 In my database are many text columns where values are empty strings ( '' ). The empty strings need to be set to NULL . I do not know the exact schemas, tables and columns in this database or rather I want to write a general solution which can be reused. How would I write a query / function to find all text columns in all tables in all schemas and update all columns with empty strings ( '' ) to NULL ? 回答1: The most efficient way to achieve this: Run a single UPDATE per table. Only update

MySQL Select If Table Exists

我是研究僧i 提交于 2019-12-17 20:27:38
问题 I need to run a count query on a table but only if that table exists, SELECT CASE WHEN (SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'DATABASENAME' AND TABLE_NAME = 'testtable') < 1 THEN '0' ELSE (SELECT COUNT(*) FROM testtable) END; The above query should return 0 if the table doesn't exist, but if it does it should get the count. This returns an error saying "testtable" doesn't exist, we know it doesn't exist as the information_schema count returns 0. Is this possible

unreliable information_schema schema information on SQL Server?

不羁岁月 提交于 2019-12-17 20:08:48
问题 The SQL Server documentation here says that the table_schema field of information_schema.tables is "unreliable" and that the correct way to get the schema of an object is to query sys.objects . Can anyone elaborate on how and when the schema reported by information_schema.tables can be incorrect? 回答1: It's a shame this went unanswered and just commented so partially out of rep greed and more importantly to get it out of the unanswered queue, I'll throw a few points into an answer. The wording

SQLite Schema Information Metadata

可紊 提交于 2019-12-17 04:56:10
问题 I need to get column names and their tables in a SQLite database. What I need is a resultset with 2 columns: table_name | column_name . In MySQL, I'm able to get this information with a SQL query on database INFORMATION_SCHEMA . However the SQLite offers table sqlite_master : sqlite> create table students (id INTEGER, name TEXT); sqlite> select * from sqlite_master; table|students|students|2|CREATE TABLE students (id INTEGER, name TEXT) which results a DDL construction query ( CREATE TABLE )

SQLite Schema Information Metadata

亡梦爱人 提交于 2019-12-17 04:56:01
问题 I need to get column names and their tables in a SQLite database. What I need is a resultset with 2 columns: table_name | column_name . In MySQL, I'm able to get this information with a SQL query on database INFORMATION_SCHEMA . However the SQLite offers table sqlite_master : sqlite> create table students (id INTEGER, name TEXT); sqlite> select * from sqlite_master; table|students|students|2|CREATE TABLE students (id INTEGER, name TEXT) which results a DDL construction query ( CREATE TABLE )

postgres: find all integer columns with its current max value in it

ぃ、小莉子 提交于 2019-12-13 17:14:22
问题 How to find all integer typed primary key columns with its current max value in it from all tables from all databases in Postgres instance? I want to find all the int typed primary key columns from all tables which are nearing to overflow its max value 2147483647. 回答1: CREATE OR REPLACE FUNCTION intpkmax() RETURNS TABLE(schema_name name, table_name name, column_name name, max_value integer) LANGUAGE plpgsql STABLE AS $$BEGIN /* loop through tables with a simgle integer column as primary key *

MySQL Queriable Field Constraint by RegEx

假装没事ソ 提交于 2019-12-13 07:18:40
问题 In addition to a data type of VARCHAR(255) , I'd like to add a constraint to the data by a regular expression, e.g. [a-zA-Z ] . I'd like to be able to query the regex, like how I can query meta data in the information_schema . I've seen some related posts, but not exactly what I'm looking for. I've seen: Restrict varchar() column to specific values? which shows how to use constaints, and I've seen via triggers here: Is it Possible to Enforce Data Checking in MySQL using Regular expression Can