SQlite Foreign Key Sample

Here is an example of sqlite database foreign key definition on create table statement

FOREIGN KEY (UserId) REFERENCES User(_id)
CREATE TABLE User(
  _id INTEGER PRIMARY KEY AUTOINCREMENT,
  UserName VARCHAR(50),
  PassWord  VARCHAR(50),
  CreateDate DATETIME DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE Transaction(
  _id INTEGER PRIMARY KEY AUTOINCREMENT,
  Name VARCHAR(50),
  UserId INTEGER,
  Type  INTEGER,
  CreateDate DATETIME DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (UserId) REFERENCES User(_id)
);

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.