简体   繁体   中英

How do i get information about foreign keys in SQLite?

The sqlite_master table seems to lack information. How can i get some extra information about my tables like constraint information etc. Any examples? Or is there some usefull library out there that i`ve never heard off?

Use

PRAGMA foreign_key_list(table);

which returns the foreign keys for a table.

Then, you can use

PRAGMA foreign_key_check; 
PRAGMA foreign_key_check(table);

to list the violations, instead of doing it yourself.

The sql column of sqlite_master contains the SQL used to create tables and thus includes foreign key information.

SELECT tbl_name,sql FROM sqlite_master WHERE sql LIKE('%REFERENCES%')

Would locate the rows for tables with foreign keys.

  • REFERENCES , rather than FOREIGN , will include FOREIGN KEY s defined at the column level (as column-constraints), which only uses the foreign-key clause and as such does not include FOREIGN KEY .

However, PRAGMA foreign_key_list(????) , where ???? is the name of the table to inspect, provides the information in a more usable format (as per example below).

eg extracting from sqlite_master could result in (line breaks added) :-

CREATE TABLE Match(id_Match INTEGER PRIMARY KEY AUTOINCREMENT, 
    id_Court INTEGER REFERENCES Court(id_Court), 
    id_Player INTEGER REFERENCES Player(id_Player), 
    id_Player1 INTEGER REFERENCES Player(id_Player))

whilst using PRAGMA foreign_key_list(Match) results in :-

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM