ADO.NET Provider for Amazon Redshift

Build 23.0.8839

Foreign Keys

This section describes how to access information about foreign keys by retrieving the ForeignKeys schema collection.

Retrieving Foreign Key Information

To retrieve the ForeignKeys schema collection, call the GetSchema method of the RedshiftConnection class. You can restrict foreign key information by the catalog, schema, or table name.

Access the results in the DataTable returned. The following example lists the foreign keys for the "sales_db"."public".Orders table.

C#

String connectionString = "User=admin;Password=admin;Database=dev;Server=examplecluster.my.us-west-2.redshift.amazonaws.com;Port=5439;";

using (RedshiftConnection conn = new RedshiftConnection(connectionString)) {
  conn.Open();
  DataTable databaseSchema = conn.GetSchema("ForeignKeys", new string[] {"sales_db","public","Orders"});
  foreach (DataRow row in databaseSchema.Rows) {
    Console.WriteLine(row["CONSTRAINT_NAME"]);
    Console.WriteLine(row["TABLE_NAME"]);
  }
}

VB.NET

Dim connectionString As String = "User=admin;Password=admin;Database=dev;Server=examplecluster.my.us-west-2.redshift.amazonaws.com;Port=5439;"

Using conn As New RedshiftConnection(connectionString)
  conn.Open()
  Dim databaseSchema As DataTable = conn.GetSchema("ForeignKeys",  New String() {"sales_db","public","Orders")
    For Each row As DataRow In databaseSchema.Rows
    Console.WriteLine(row("CONSTRAINT_NAME"))
    Console.WriteLine(row("TABLE_NAME"))
  Next

End Using

Columns Returned

The ForeignKeys schema collection returns the following information about the foreign keys in Amazon Redshift.

Column NameData TypeDescription
CONSTRAINT_CATALOGSystem.StringThe database containing the foreign key.
CONSTRAINT_SCHEMASystem.StringThe schema containing the foreign key.
CONSTRAINT_NAMESystem.StringThe name of the foreign key.
CONSTRAINT_TYPESystem.StringReturns FOREIGN KEY.
TABLE_CATALOGSystem.StringThe database of the table containing the foreign key.
TABLE_SCHEMASystem.StringThe schema of the table containing the foreign key.
TABLE_NAMESystem.StringThe name of the table containing the foreign key.
IS_DEFERRABLESystem.StringWhether the foreign key is deferrable. This value is YES or NO.
INITIALLY_DEFERREDSystem.StringWhether the foreign is initially deferrable. This value is YES or NO.

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 23.0.8839