SQLTableExportedKeys[conn,table]
returns information about foreign keys that reference the primary key of table.
SQLTableExportedKeys
SQLTableExportedKeys[conn,table]
returns information about foreign keys that reference the primary key of table.
Details and Options
- To use SQLTableExportedKeys, you first need to load DatabaseLink using Needs["DatabaseLink`"].
- The following options can be given:
-
"Catalog" None database catalog to use "Schema" None database schema to use "ShowColumnHeadings" False whether to return headings with the results
Examples
Basic Examples (1)
Needs["DatabaseLink`"]If you find that the examples in this section do not work as shown, you may need to install or restore the example database with the DatabaseLink`DatabaseExamples` package, as described in Using the Example Databases.
conn = OpenSQLConnection["demo"];Create a table with a primary key:
SQLCreateTable[conn, SQLTable["PEOPLE"], {SQLColumn["id", "DataTypeName" -> "Integer", "PrimaryKey" -> True], SQLColumn["Name", "DataTypeName" -> "VARCHAR", "DataLength" -> 255],
SQLColumn["Age", "DataTypeName" -> "Integer"]
}]Create another table. The "\"\!\(\*StyleBox[\"PersonId\", \"InlineCode\"]\)\!\(\*StyleBox[\"\\\"\", \"InlineCode\"]\)" column will be used for a foreign key constraint referencing the "PEOPLE" table's primary key:
SQLCreateTable[conn, SQLTable["EMPLOYEES"], {SQLColumn["id", "DataTypeName" -> "Integer", "PrimaryKey" -> True],
SQLColumn["PersonId", "DataTypeName" -> "Integer"], SQLColumn["Department", "DataTypeName" -> "VARCHAR", "DataLength" -> 255],
SQLColumn["Tenure", "DataTypeName" -> "Integer"]
}]Add the foreign key constraint:
SQLExecute[conn, "ALTER TABLE EMPLOYEES ADD CONSTRAINT FK_PersonId FOREIGN KEY (PersonId) REFERENCES PEOPLE(id)"]The foreign key appears as an index in the "EMPLOYEES" table:
SQLTableIndexInformation[conn, "EMPLOYEES", "ShowColumnHeadings" -> True]//TableFormThe primary key column in the "PEOPLE" table now appears as an exported key:
SQLTableExportedKeys[conn, "PEOPLE", "ShowColumnHeadings" -> True]//TableFormSQLDropTable[conn, "EMPLOYEES"]SQLDropTable[conn, "PEOPLE"]CloseSQLConnection[conn];