AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Code-Bibliothek Neuen Beitrag zur Code-Library hinzufügen Einige nützliche SQL Statements, die ich gesammelt habe...
Thema durchsuchen
Ansicht
Themen-Optionen

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

Ein Thema von sniper_w · begonnen am 7. Mai 2009 · letzter Beitrag vom 7. Mai 2009
Antwort Antwort
Benutzerbild von sniper_w
sniper_w

Registriert seit: 11. Dez 2004
Ort: Wien, Österriech
893 Beiträge
 
Delphi 6 Enterprise
 
#1

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

  Alt 7. Mai 2009, 14:21
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]
Katura Haris
Es (ein gutes Wort) ist wie ein guter Baum, dessen Wurzel fest ist und dessen Zweige in den Himmel reichen.
  Mit Zitat antworten Zitat
Jürgen Thomas

Registriert seit: 13. Jul 2006
Ort: Berlin
750 Beiträge
 
#2

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

  Alt 7. Mai 2009, 15:10
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.
#D mit C# für NET, dazu Firebird
früher: Delphi 5 Pro, Delphi 2005 Pro mit C# (also NET 1.1)
Bitte nicht sauer sein, wenn ich mich bei Delphi-Schreibweisen verhaue; ich bin inzwischen an C# gewöhnt.
  Mit Zitat antworten Zitat
nahpets
(Gast)

n/a Beiträge
 
#3

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

  Alt 7. Mai 2009, 15:55
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.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 18:57 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