I am using SQLite and DB Browser for SQLite. I know that if you want to insert a foreign key using the GUI, you just have to scroll to the right and double click to insert the foreign key to the corresponding field. The problem is when you want to use a multiple foreign key. How or where should i put the multiple foreign key constrain using the GUI ?
my actual sql is (returns an exception 'foreign key mismatch - "diseases" referencing "beehives"...'):
CREATE TABLE "diseases" (
"id" INTEGER NOT NULL,
"id_beehive" INTEGER NOT NULL,
"id_apiary" INTEGER NOT NULL,
"disease" TEXT NOT NULL,
"treatment" TEXT NOT NULL,
"start_treat_date" DATE NOT NULL,
"end_treat_date" DATE,
PRIMARY KEY("id"),
FOREIGN KEY("id_beehive") REFERENCES "beehives"("number") ON UPDATE CASCADE,
FOREIGN KEY("id_apiary") REFERENCES "beehives"("id_apiary") ON UPDATE CASCADE
);
what i want to do using the gui is:
CREATE TABLE "diseases" (
"id" INTEGER NOT NULL,
"id_beehive" INTEGER NOT NULL,
"id_apiary" INTEGER NOT NULL,
"disease" TEXT NOT NULL,
"treatment" TEXT NOT NULL,
"start_treat_date" DATE NOT NULL,
"end_treat_date" DATE,
PRIMARY KEY("id"),
FOREIGN KEY("id_beehive","id_apiary") REFERENCES "beehives"("number","id_apiary") ON UPDATE CASCADE
);
beehives sql:
CREATE TABLE "beehives" (
"number" INTEGER NOT NULL,
"id_apiary" INTEGER NOT NULL DEFAULT -2,
"date" DATE,
"type" TEXT,
"favorite" BOOLEAN DEFAULT 'false',
PRIMARY KEY("number","id_apiary"),
FOREIGN KEY("id_apiary") REFERENCES "apiaries"("id") ON DELETE SET NULL
);
After researching, it is not possible for the moment.
Everytime you modify the structure of the table, what DB Browser for SQLite do on the background is renaming the table, creating a new table with the updated structure, move the data to this new table and delete the old table. So in the GUI it looks like its only updating.
Unfortunatly, to create a composite foreign key or modify a simple FK to a composite FK , you can only do it manualy from the SQL window.
example :
FOREIGN KEY("id_beehive","id_apiary") REFERENCES "beehives"("number","id_apiary") ON
UPDATE CASCADE
I have 2 tables CarParking and ParkingArea. On ParkingArea I need to save the MaxPricePerDay, and on CarParking I have to save the cost. I try to write constraint that the cost is under or equal to MaxPricePerDay base on the AID columnn.
How can I do that?
CREATE TABLE "CarParking" (
"StartTime" TIMESTAMP NOT NULL,
"EndTime" TIMESTAMP NOT NULL,
"Cost" INTEGER NOT NULL,
"CID" INTEGER NOT NULL,
"AID" INTEGER NOT NULL,
FOREIGN KEY("CID") REFERENCES "Car"("CID")
ON DELETE CASCADE,
FOREIGN KEY("AID") REFERENCES "ParkingArea"("AID")
ON DELETE SET NULL,
CONSTRAINT time_car CHECK(StartTime<=EndTime),
PRIMARY KEY("StartTime","CID")
);
CREATE TABLE "ParkingArea" (
"MaxPricePerDay" INTEGER NOT NULL,
"PricePerHour" INTEGER NOT NULL,
"Name" TEXT NOT NULL,
"AID" INTEGER UNIQUE NOT NULL,
"Neighborhood" INTEGER NOT NULL,
PRIMARY KEY("AID"),
FOREIGN KEY ("Neighborhood") REFERENCES "Neighborhood" ("NID")
ON DELETE CASCADE
);
Since CHECK constraints can't have subqueries, one way is using a trigger instead.
CREATE TRIGGER check_max_price BEFORE INSERT ON CarParking
BEGIN
SELECT raise(ABORT, 'Exceeded MaxPricePerDay')
FROM ParkingArea
WHERE aid = new.aid AND new.Cost > MaxPricePerDay;
END;
is a starting point; you'll have to tweak the calculation to see if the cost is too much as appropriate.
If your cost can be updated after a row is inserted, you'll want a similar trigger for UPDATE.
CREATE TABLE Client_master (
Client_no varchar(6) PRIMARY KEY,
Name varchar(15) NOT NULL,
City varchar(15),
Pincode number(8),
State varchar(15),
Bal_due Number(10,2),
CHECK(Client_no LIKE 'C%'));
INSERT INTO
Client_master(Client_no,Name,City,Pincode,State,Bal_due)
VALUES('C00001','Ivan Bayross','Bombay','400054','Maharashtra',15000);
1 row created.
CREATE TABLE Sales_order(
Order_no varchar(6) PRIMARY KEY REFERENCES Client_master (Client_no),
Order_date date,
Client_no varchar(6),
Dely_type char(1) DEFAULT 'f',
Billed_yn char(1),
Salesman_no varchar(6),
Dely_date date,
Order_status varchar(10),
CHECK(Order_no LIKE 'O%'),
CHECK(Order_status IN ('inprocess','backorder','cancelled')),
CHECK(Dely_date>Order_date));
INSERT INTO Sales_order(Order_no, Order_date, Client_no, Dely_type, Billed_yn, Salesman_no, Dely_date, Order_status)
VALUES('C19001','12-jan-96','C00001','f','n','S0001','20-jan-96','inprocess');
INSERT INTO
*
ERROR at line 1:
ORA-02291: integrity constraint (SYSTEM.SYS_C007155) violated - parent key not
found
Please help me insert data into Child table.
What's this SYSTEM.SYS_C007155 error? Why this error message "parent key is not found"?
The parent key not found is because you have a foreign key in your sales_order table which is pointing to the client_master, and there is no matching key value - exactly what it says. But it's because you've done something odd:
Sales_order(Order_no varchar(6) PRIMARY KEY REFERENCES Client_master (Client_no)
You've made the order_no the primary key for this table, but also made it a foreign key to the client_no on the other table. Your insert uses order_no of 'C19001', which doesn't match the client_no you previously inserted into the parent table.
You almost certainly wanted sales_order.client_no to be a foreign key to client_master.client_no, so you would have the references... against that column:
CREATE TABLE Sales_order(Order_no varchar(6) PRIMARY KEY,
Order_date date,
Client_no varchar(6) REFERENCES Client_master (Client_no),
...
You have a further problem as the check constraint you have against the order_no is CHECK(Order_no LIKE 'O%') but as mentioned the value you're putting in is 'C19001', which doesn't match the pattern. Presumably you meant the insert to be for 'O19001'; if not then the constraint is defined incorrectly. I actually hit that check constraint before the primary key constrain in 11gR2, so you may just have changed that while posting the question.
You can look in the user_constraints and user_cons_columns views to see what a constraint is doing, but you'll find it easier if you name your constraints instead of letting Oracle give them default names like SYS_C007155:
Specify a name for the constraint. If you omit this identifier, then Oracle Database generates a name with the form SYS_Cn. Oracle stores the name and the definition of the integrity constraint in the USER_, ALL_, and DBA_CONSTRAINTS data dictionary views (in the CONSTRAINT_NAME and SEARCH_CONDITION columns, respectively).
For example:
CREATE TABLE Client_master (
Client_no varchar(6),
Name varchar(15) NOT NULL,
City varchar(15),
Pincode number(8),
State varchar(15),
Bal_due Number(10,2),
CONSTRAINT Client_master_pk PRIMARY KEY (Client_no),
CONSTRAINT Client_master_chk_no CHECK(Client_no LIKE 'C%'));
INSERT INTO Client_master(Client_no,Name,City,Pincode,State,Bal_due)
VALUES('C00001','Ivan Bayross','Bombay','400054','Maharashtra',15000);
1 row inserted.
CREATE TABLE Sales_order (
Order_no varchar(6),
Order_date date,
Client_no varchar(6),
Dely_type char(1) DEFAULT 'f',
Billed_yn char(1),
Salesman_no varchar(6),
Dely_date date,
Order_status varchar(10),
CONSTRAINT Sales_order_pk PRIMARY KEY (Order_no),
CONSTRAINT Sales_order_fk_client FOREIGN KEY (Client_no)
REFERENCES Client_master (Client_no),
CONSTRAINT Sales_order_chk_no CHECK (Order_no LIKE 'O%'),
CONSTRAINT Sales_order_shk_status CHECK
(Order_status IN ('inprocess','backorder','cancelled')),
CONSTRAINT Sales_order_chk_dates CHECK(Dely_date>Order_date));
INSERT INTO Sales_order(Order_no, Order_date, Client_no, Dely_type, Billed_yn, Salesman_no, Dely_date, Order_status)
VALUES('O19001','12-jan-96','C00001','f','n','S0001','20-jan-96','inprocess');
1 row inserted.
If you do have a violation, e.g. with your original order_no value, you'd see a more useful message:
INSERT INTO Sales_order(Order_no, Order_date, Client_no, Dely_type, Billed_yn, Salesman_no, Dely_date, Order_status)
VALUES('C19001','12-jan-96','C00001','f','n','S0001','20-jan-96','inprocess');
ORA-02290: check constraint (YOUR_SCHEMA.SALES_ORDER_CHK_NO) violated
so you can have a better idea which constraint is violated, and what that means, without having to loo in the data dictionary. SALES_ORDER_CHK_NO is easier to interpret than SYS_C007155 Use names that make sense for you of course.
You can name inline constraints too:
CREATE TABLE Client_master (
Client_no varchar(6) CONSTRAINT Client_master_pk PRIMARY KEY,
...
CREATE TABLE Sales_order (
Order_no varchar(6) CONSTRAINT Sales_order_pk PRIMARY KEY,
Order_date date,
Client_no varchar(6) CONSTRAINT Sales_order_fk_client REFERENCES Client_master (Client_no),
...
but it might be clearer and easier to maintain with them all grouped together at the end.
CREATE TABLE IF NOT EXISTS type (
tid INTEGER NOT NULL,
uuid VARCHAR NOT NULL,
name VARCHAR NOT NULL,
CONSTRAINT PK PRIMARY KEY (tid),
CONSTRAINT UNQ_0 UNIQUE (uuid),
CONSTRAINT UNQ_1 UNIQUE (name)
);
CREATE INDEX IDX_type_0 ON type (tid,uuid,name);
CREATE TABLE IF NOT EXISTS object (
oid VARCHAR NOT NULL,
timestamp VARCHAR NOT NULL,
tid INTEGER NOT NULL,
CONSTRAINT FK_tid FOREIGN KEY (tid) REFERENCES type(tid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT UNQ_0 UNIQUE (oid)
);
CREATE INDEX IDX_object_0 ON object (oid,timestamp,tid);
CREATE TABLE IF NOT EXISTS object_user_owner (
uid INTEGER NOT NULL,
oid VARCHAR NOT NULL,
CONSTRAINT FK_uid FOREIGN KEY (uid) REFERENCES user(uid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT FK_oid FOREIGN KEY (oid) REFERENCES object(oid) ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT UNQ_0 UNIQUE (oid,uid)
);
CREATE INDEX IDX_object_user_owner_0 ON object_user_owner (uid,oid);
CREATE TABLE IF NOT EXISTS user (
uid INTEGER NOT NULL,
uuid VARCHAR NOT NULL,
name VARCHAR NOT NULL,
password VARCHAR NOT NULL,
salt VARCHAR NOT NULL,
timestamp VARCHAR NOT NULL,
lastaccess VARCHAR NOT NULL,
CONSTRAINT PK PRIMARY KEY (uid),
CONSTRAINT UNQ_0 UNIQUE (uuid),
CONSTRAINT UNQ_1 UNIQUE (name)
);
CREATE INDEX IDX_user_0 ON user (uid,uuid,name);
The above three sqlite3 tables contain foreign keys. The problem is the deletion of a key in the upper table type. When I try do delete I get 'FOREIGN KEY constraint failed' Error. Deleting a from the lowest table object_user_owner before deleting a type works. I think sqlite does not check any recursive cascade constraints. Does anyone have experienced this too or is anything wrong with my design?
one column value reference from two tables is it support SQLITE
see the below table structure
account_id reference from two tables
FOREIGN KEY(account_id) REFERENCES account(id),
FOREIGN KEY(account_id) REFERENCES category(id)
CREATE TABLE transaction (
id NUMERIC PRIMARY KEY,
user_id NUMERIC NOT NULL,
account_id NUMERIC NOT NULL,
category_id NUMERIC NOT NULL,
amount DOUBLE NOT NULL, date VARCHAR(25) NOT NULL,
description VARCHAR(25),
FOREIGN KEY(account_id) REFERENCES account(id),
FOREIGN KEY(account_id) REFERENCES category(id)
);
This is possible.
All constraints must be met, i.e., each account_id value must appear in both parent tables.