0

Using the C API, I don't see a way to determine the foreign key constraints for a named table?

Given this example:

CREATE TABLE artist(
  artistid    INTEGER PRIMARY KEY, 
  artistname  TEXT
);
CREATE TABLE track(
  trackid     INTEGER,
  trackname   TEXT, 
  trackartist INTEGER,
  FOREIGN KEY(trackartist) REFERENCES artist(artistid)
);

sqlite3_table_column_metadata() will tell you it's a primary key, autoincrement, etc. but how do I get the foreign key constraints?

 FOREIGN KEY(trackartist) REFERENCES artist(artistid)

I want to be able to get a list back for table "track" that there are foreign keys back to table Artist column artistid?

I don't see an api to do this? I need to do this programmaticlly upon opening the database, for purposes of aggregation.

Thanks.

After using PRAGMA foreign_key_list(Valuation);

I got back:

PRAGMA foreign_key_list(Valuation);
0|0|Stock|StockId|Id|NO ACTION|NO ACTION|NONE

I understand I need to split on the vertical bar, but what are the first two columns? 0|0 ?

4

1 回答 1

1

Please note that (foreign) keys can consist of multiple columns, so it would not make sense to return this as column information.

To get information about a table's foreign keys, use this:

PRAGMA foreign_key_list(table-name);

This pragma returns one row for each foreign key constraint created by a REFERENCES clause in the CREATE TABLE statement of table "table-name".

于 2013-08-01T08:06:13.997 回答