Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Neuen Beitrag zur Code-Library hinzufügen (https://www.delphipraxis.net/33-neuen-beitrag-zur-code-library-hinzufuegen/)
-   -   Einige nützliche SQL Statements, die ich gesammelt habe... (https://www.delphipraxis.net/133721-einige-nuetzliche-sql-statements-die-ich-gesammelt-habe.html)

sniper_w 7. Mai 2009 14:21


Einige nützliche SQL Statements, die ich gesammelt habe...
 
Bitte die Hinweise in den nachfolgenden Antworten beachten, da diese Statements nicht für alle DBMS gelten!

SQL-Code:
-- To list all the tables in a database.
SELECT * FROM information_schema.tables

--List all the views in a database
SELECT * FROM information_schema.tables WHERE table_type = 'view'

--List all the tables in 'MeinTabelle' catalog excluding some of the system tables

SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE
   TABLE_CATALOG = 'MeinTabelle'
   AND TABLE_TYPE   = 'BASE TABLE'
   AND TABLE_NAME != 'dtproperties'
   AND TABLE_NAME != 'sysconstraints'
   AND TABLE_NAME != 'syssegments'
   AND TABLE_NAME != 'sysdiagrams'
ORDER BY TABLE_NAME ASC

--List all the columns in the database that has the word '%mit%' in it.
SELECT * FROM information_schema.columns
WHERE column_name LIKE '%mit%'

--List all columns in the database that are identity fields.

SELECT INFORMATION_SCHEMA.COLUMNS.* 
from INFORMATION_SCHEMA.COLUMNS WHERE
(SELECT COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME, 'IsIdentity')) = 1

--List all columns in the database that are computed fields.

SELECT INFORMATION_SCHEMA.COLUMNS.* 
from information_schema.columns where
(SELECT COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), INFORMATION_SCHEMA.COLUMNS.COLUMN_NAME, 'IsComputed'))=1

--There are two more important information_schema views that are useful in retrieving the table constraints,
--keys and indexes. They are information_schema.table_constraints and information_schema.key_column_usage

--List all the primary key columns in the database.

SELECT K.* 
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE K
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS C ON K.CONSTRAINT_NAME = C.CONSTRAINT_NAME
WHERE C.CONSTRAINT_TYPE = 'PRIMARY KEY'

-- get the column list for all tables...
SELECT
   X.table_name,
   [Col_List] =
   (
      SELECT TOP 1
         STUFF((   SELECT ', ' + T2.COLUMN_NAME
            FROM INFORMATION_SCHEMA.COLUMNS AS T2 
            WHERE T2.TABLE_NAME = T1.TABLE_NAME FOR XML PATH ('')), 1, 2,'') AS COL_NAMES
      FROM INFORMATION_SCHEMA.COLUMNS AS T1
      WHERE T1.TABLE_NAME = X.table_name
      ORDER BY T1.ORDINAL_POSITION
   )
FROM information_schema.tables as X
WHERE X.table_type = 'base table'
ORDER BY X.table_name
[edit=TBx]Hinweis eingefügt Mfg, TBx[/edit]

Jürgen Thomas 7. Mai 2009 15:10

Re: Einige nützliche SQL Statements, die ich gesammelt habe.
 
Hallo,

diese SQL-Befehle sind durchaus nützlich, passen aber (nur?) zum MS-SQL Server. Jedes DBMS hat seine eigene Struktur der Systemtabellen, und INFORMATION_SCHEMA gehört zu MS-SQL.

Jürgen

PS. Bei Interbase/Firebird sind es RDB$-Tabellen.

PS2. Ich bin gerne bereit, die Systemtabellen für andere DBMS hier aufzuführen.

nahpets 7. Mai 2009 15:55

Re: Einige nützliche SQL Statements, die ich gesammelt habe.
 
Hallo,

information_schema wird unterstützt von

Microsoft SQL Server - ab Version 7
MySQL - ab Version 5
PostgreSQL - ab Version 7.4

weitere...?

Soweit ich weiß ist das ein Teil des ANSI/ISO SQL:2003-Standard.

Zumindest theoretisch müssten das (früher oder später) alle Datenbanken, die sich an den Standard halten, unterstützen.

Aber: Eine Gegenüberstellung der einzelnen Datenbankvarianten hätte da schon ihren Reiz.


Alle Zeitangaben in WEZ +1. Es ist jetzt 20:37 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz