1

I have two tables in my database which the primary key in Tbl_Cart is a foreign key in Tbl_Goods,I simply created the Tbl_Cart Table but when I want to create Tbl_Goods I execute the following query and SQLite Browser stops and I have to close it immediately!

I couldn't recognize the reason!

P.S. I want to use this Database in an Android Aplication

Here is the query:

CREATE TABLE IF NOT EXISTS Tbl_Goods (
Good_ID INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  DEFAULT 1,
FOREIGN KEY(Cart_ID) REFERENCES Tbl_Cart(Cart_ID), 
Good_Name VARCHAR(50),
Good_UnitPrice TEXT,
Quantity INTEGER);

I'll be glad if you help me!

2 Answers 2

1

First create Cart_ID then set foreign key at last.

CREATE TABLE IF NOT EXISTS Tbl_Goods (
Good_ID INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  DEFAULT 1,
Cart_ID INTEGER,
Good_Name VARCHAR(50),
Good_UnitPrice TEXT,
Quantity INTEGER,
FOREIGN KEY(Cart_ID) REFERENCES Tbl_Cart(Cart_ID));
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry I have a question,what is the difference between FOREIGN KEY(Cart_ID) REFERENCES Tbl_Cart(Cart_ID)); and Cart_ID INTEGER REFERENCES Tbl_Cart(Cart_ID),
0

Or if you want a column constraint

CREATE TABLE IF NOT EXISTS Tbl_Goods (
Good_ID INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  DEFAULT 1,
Cart_ID INTEGER REFERENCES Tbl_Cart(Cart_ID),
Good_Name VARCHAR(50),
Good_UnitPrice TEXT,
Quantity INTEGER );

Comments