Thursday, July 29th 2010, 1:27pm UTC+2

You are not logged in.

  • Login
  • Register

1

Friday, February 29th 2008, 11:41am

SQLite3 - Keine Foreign Keys?

Hi,

die Foreign Key Constraint greift nicht. Weiß jemand woran das liegt oder hat SQLite3 diese Constraint noch gar nicht implementiert?

PHP Source code

1
2
3
4
5
6
7
CREATE TABLE Directory (
        Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
        Name VARCHAR(40NOT NULL,
        DirectoryId INTEGER NULL
                CONSTRAINT FK_DirectoryId REFERENCES Directory(IdON DELETE CASCADE
                CONSTRAINT C_Directory_Loop CHECK (Id != DirectoryId)
);


Beispiel Szenario:

PHP Source code

1
2
3
4
INSERT INTO Directory (NameVALUES ('foo');
INSERT INTO Directory (NameDirectoryIdVALUES ('sub foo'1);
DELETE FROM Directory WHERE Id 1// FEHLER Foreign Key Constraint sollte greifen
DELETE FROM Directory WHERE Id 2// OK
Greetz Andi

Mod Dewey

CREAKTIF - Comes soon

Google ist dein Freund :D
  • Go to the top of the page

undefined

Super Moderator

Posts: 4,248

Location: Germany

2

Friday, February 29th 2008, 2:17pm

PHP Source code

1
2
INSERT INTO Directory (NameDirectoryIdVALUES ('sub foo'1); ## Normal hätte er schon hier Meckern müssen weil 1 schon vergeben ist!
DELETE FROM Directory WHERE Id 1// Die Meldung kommt definitive zu spät weil zwei 1 ID enthalten sind!

Einen Integer auf default NULL zu setzen ist Gift ;)
Undefined Behavior (undefiniertes Verhalten) bedeutet meistens etwas ungültiges.
PHP Katepart - Speichenrechner - .htpasswd - RPM XDG Tool - Kcmnvview - QTidy
  • Go to the top of the page

3

Friday, February 29th 2008, 2:20pm

Hm nein! Die Werte sind doch in verschiedenen Spalten!
Id und DirectoryId

Was den Integer betrifft: Ändere ich sofort! :)
Greetz Andi

Mod Dewey

CREAKTIF - Comes soon

Google ist dein Freund :D
  • Go to the top of the page

undefined

Super Moderator

Posts: 4,248

Location: Germany

4

Friday, February 29th 2008, 2:29pm

Opps stimmt habe die Indexes vertauscht..
Habe es gerade mal bei mir ausprobiert und da geht es.

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
sqliteINSERT INTO Directory (NameVALUES ('foo');
sqliteINSERT INTO Directory (NameDirectoryIdVALUES ('sub foo'1);
sqliteDELETE FROM Directory WHERE Id 1;
sqliteSELECT FROM Directory;
2|sub foo|1
sqlite> .dump Directory
BEGIN TRANSACTION;
CREATE TABLE Directory (
  Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENTName VARCHAR(40NOT NULL,
  DirectoryId INTEGER NULL CONSTRAINT FK_DirectoryId REFERENCES Directory(IdON DELETE CASCADE CONSTRAINT C_Directory_Loop CHECK (Id != DirectoryId)
);
INSERT INTO "Directory" VALUES(2,'sub foo',1);
COMMIT;
Undefined Behavior (undefiniertes Verhalten) bedeutet meistens etwas ungültiges.
PHP Katepart - Speichenrechner - .htpasswd - RPM XDG Tool - Kcmnvview - QTidy
  • Go to the top of the page

5

Friday, February 29th 2008, 2:49pm

Hm aber wenn du den Tupel mit der Id = 1 löschen kannst, dann sollte er dir dabei einen Fehler melden. Weil man den Tupel mit dem PK nicht löschen kann wenn noch ein Tupel mit dem FK vorhanden ist. Hmpf

"Das gibt heute noch Gesichtsfalten..." 8o
Greetz Andi

Mod Dewey

CREAKTIF - Comes soon

Google ist dein Freund :D
  • Go to the top of the page

undefined

Super Moderator

Posts: 4,248

Location: Germany

6

Friday, February 29th 2008, 3:00pm

Ich gehe mal davon aus weil du NULL verwendest.
In der Docu steht nichts von NULL
http://www.sqlite.org/lang_createtable.html
Undefined Behavior (undefiniertes Verhalten) bedeutet meistens etwas ungültiges.
PHP Katepart - Speichenrechner - .htpasswd - RPM XDG Tool - Kcmnvview - QTidy
  • Go to the top of the page

7

Friday, February 29th 2008, 3:09pm

Hab mal das Design überarbeitet.

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
CREATE TABLE Directory (
        Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
        Name VARCHAR(40NOT NULL
);

CREATE TABLE Directory_Has_SubDirectory (
        Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
        DirectoryId INTEGER NOT NULL CONSTRAINT FK_DirectoryId REFERENCES Directory(IdON DELETE CASCADE,
        SubDirectoryId INTEGER NOT NULL CONSTRAINT FK_SubDirectoryId REFERENCES Directory(IdON DELETE CASCA
DE,
        CONSTRAINT C_Directory_Loop CHECK (DirectoryId != SubDirectoryId)
);


Beispiel Szenario:

PHP Source code

1
2
3
4
5
6
INSERT INTO Directory VALUES (1'Internet');
INSERT INTO Directory VALUES (2'Community');
INSERT INTO Directory_Has_SubDirectory (DirectoryIdSubDirectoryIdVALUES (12);

-- Datensatz löschen
DELETE FROM Directory WHERE Id 1// CONSTRAINT ERROR


Beim Löschen kommt hier jedoch kein CONSTRAINT ERROR!
Greetz Andi

Mod Dewey

CREAKTIF - Comes soon

Google ist dein Freund :D
  • Go to the top of the page

undefined

Super Moderator

Posts: 4,248

Location: Germany

8

Friday, February 29th 2008, 4:02pm

Weil deine Zeiger nicht stimmen.
Aber Irgendwie habe ich das gefühlt das SQlite damit nicht klar kommt.

PHP Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
SQLite version 3.5.1
Enter ".help" for instructions
sqliteCREATE TABLE verzeichnis (
   ...>   vid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
   ...>   name VARCHAR(40NOT NULL,
   ...>   direntid INTEGER NOT NULL DEFAULT 0 REFERENCES vid ON INSERT RESTRICT,
   ...>   FOREIGN KEY(vidREFERENCES direntid ON DELETE CASCADE ON UPDATE CASCADE
   ...> );
sqliteINSERT INTO verzeichnis name VALUES "Erste Zeile" );
sqliteINSERT INTO verzeichnis namedirentid VALUES "Zweite Zeile");
sqlite> .dump verzeichnis
BEGIN TRANSACTION;
CREATE TABLE verzeichnis (
  vid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  name VARCHAR(40NOT NULL,
  direntid INTEGER NOT NULL DEFAULT 0 REFERENCES vid ON INSERT RESTRICT,
  FOREIGN KEY(vidREFERENCES direntid ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "verzeichnis" VALUES(1,'Erste Zeile',0);
INSERT INTO "verzeichnis" VALUES(2,'Zweite Zeile',1);
COMMIT;
sqliteDELETE FROM verzeichnis WHERE direntid 1;
sqlite> .dump verzeichnis
BEGIN TRANSACTION;
CREATE TABLE verzeichnis (
  vid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  name VARCHAR(40NOT NULL,
  direntid INTEGER NOT NULL DEFAULT 0 REFERENCES vid ON INSERT RESTRICT,
  FOREIGN KEY(vidREFERENCES direntid ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "verzeichnis" VALUES(1,'Erste Zeile',0);
COMMIT;
sqlite>
Undefined Behavior (undefiniertes Verhalten) bedeutet meistens etwas ungültiges.
PHP Katepart - Speichenrechner - .htpasswd - RPM XDG Tool - Kcmnvview - QTidy
  • Go to the top of the page