information-schema

MySQL Select If Table Exists

怎甘沉沦 提交于 2019-11-28 12:44:25
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 in MySQL? You can try this : SET @val := CASE (SELECT COUNT(*) FROM information_schema.TABLES WHERE

Behaviour of NOT LIKE with NULL values

落花浮王杯 提交于 2019-11-28 11:06:17
I want to fetch all columns of a table except of columns of type serial. The closest query to this problem I was able to come up with this one: SELECT column_name FROM information_schema.columns WHERE table_name = 'table1' AND column_default NOT LIKE 'nextval%' But the problem is its also excluding/filtering rows having empty values for column_default.I don't know why the behaviour of Postgres is like this. So I had to change my query to something like this: SELECT column_name FROM information_schema.columns WHERE table_name = 'table1' AND ( column_default IS NULL OR column_default NOT LIKE

How do I find a default constraint using INFORMATION_SCHEMA?

风格不统一 提交于 2019-11-28 03:44:01
I'm trying to test if a given default constraint exists. I don't want to use the sysobjects table, but the more standard INFORMATION_SCHEMA. I've used this to check for tables and primary key constraints before, but I don't see default constraints anywhere. Are they not there? (I'm using MS SQL Server 2000). EDIT: I'm looking to get by the name of the constraint. Robert Calhoun As I understand it, default value constraints aren't part of the ISO standard, so they don't appear in INFORMATION_SCHEMA. INFORMATION_SCHEMA seems like the best choice for this kind of task because it is cross-platform

SQL Server: How to tell if a database is a system database?

浪子不回头ぞ 提交于 2019-11-28 00:51:54
I know that so far (until MSSQL 2005 at least), system databases are master, model, msdb and tempdb. Thing is, as far as I can tell, this is not guaranteed to be preserved in the future. And neither the sys.databases view nor the sys.sysdatabases view tell me if a database is considered as a system database. Is there someplace where this information (whether a database is considered a system database or not) can be obtained? Jimmy Just dived into Microsoft.SqlServer.Management.Smo.Database object (which is provided by Microsoft itself!) They simply do this using following statement: CAST(case

How can I test if a column exists in a table using an SQL statement

南楼画角 提交于 2019-11-27 20:21:44
Is there a simple alternative in PostgreSQL to this statement produced in Oracle? select table_name from user_tab_columns where table_name = myTable and column_name = myColumn; I am then testing whether the query returns anything so as to prove the column exists. I am aware that using psql I can find these out individually but this is required to produce a result in a program I am writing to validate that a requested attribute field exists in my database table. Try this : SELECT column_name FROM information_schema.columns WHERE table_name='your_table' and column_name='your_column'; Accepted

MySQL disable all triggers

一笑奈何 提交于 2019-11-27 14:36:58
For test correctness of query I need disable all triggers in db. I see that in information_schema exists table TRIGGERS. Is possible temporarily disable all triggers using this table? E.g. like: update TRIGGERS set TRIGGERS_SCHEMA='myschema_new' where TRIGGERS_SCHEMA='myschema' and after finish all test return all triggers like: update TRIGGERS set TRIGGERS_SCHEMA='myschema' where TRIGGERS_SCHEMA='myschema_new' May be this can corrupt db or after triggers will not works? I didn't found about it in documentation. You can't disable triggers directly and I wouldn't recommend doing what you're

Find a specific column entry in an unknown table in a database?

自闭症网瘾萝莉.ら 提交于 2019-11-27 07:59:52
问题 I'm aware of this topic ( Find a specific column in an unknown table in a database? ) and my problem is quite similar. The query I need is quite similar to this one (I think): SELECT TABLE_NAME, COLUMN_NAME, DATA_TYPE, IS_NULLABLE, COLUMN_DEFAULT FROM INFORMATION_SCHEMA.COLUMNS WHERE column_name LIKE '%watcher%' But what I need is a query where the column name is unknown, but I know what the content will be and I want to find out what the name of table/column is. (I know this sounds strange :

Information schema and Primary Keys

岁酱吖の 提交于 2019-11-27 07:45:43
问题 How do I just print out a 'primary key' for the column with the primary key? I get 'primary key' for all the columns if the table has a primary key, instead of the one column with the primary key and the other columns as blank in keyType. SELECT c.TABLE_NAME, c.COLUMN_NAME, c.DATA_TYPE, c.Column_default, c.character_maximum_length, c.numeric_precision, c.is_nullable, CASE WHEN u.CONSTRAINT_TYPE = 'PRIMARY KEY' THEN 'primary key' ELSE '' END AS KeyType FROM INFORMATION_SCHEMA.COLUMNS as c LEFT

Behaviour of NOT LIKE with NULL values

淺唱寂寞╮ 提交于 2019-11-27 06:00:23
问题 I want to fetch all columns of a table except of columns of type serial. The closest query to this problem I was able to come up with this one: SELECT column_name FROM information_schema.columns WHERE table_name = 'table1' AND column_default NOT LIKE 'nextval%' But the problem is its also excluding/filtering rows having empty values for column_default.I don't know why the behaviour of Postgres is like this. So I had to change my query to something like this: SELECT column_name FROM

How to check if a Constraint exists in Sql server?

£可爱£侵袭症+ 提交于 2019-11-27 05:51:40
I have this sql: ALTER TABLE dbo.ChannelPlayerSkins DROP CONSTRAINT FK_ChannelPlayerSkins_Channels but apparently, on some other databases we use, the constraint has a different name. How do I check if there's a constraint with the name FK_ChannelPlayerSkins_Channels . try this: SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS WHERE CONSTRAINT_NAME ='FK_ChannelPlayerSkins_Channels' -- EDIT -- When I originally answered this question, I was thinking "Foreign Key" because the original question asked about finding "FK_ChannelPlayerSkins_Channels". Since then many people have commented on