Thursday, July 29th 2010, 1:27pm UTC+2
You are not logged in.
|
|
PHP Source code |
1 2 3 4 5 6 7 |
CREATE TABLE Directory (
Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
Name VARCHAR(40) NOT NULL,
DirectoryId INTEGER NULL
CONSTRAINT FK_DirectoryId REFERENCES Directory(Id) ON DELETE CASCADE
CONSTRAINT C_Directory_Loop CHECK (Id != DirectoryId)
);
|
|
|
PHP Source code |
1 2 3 4 |
INSERT INTO Directory (Name) VALUES ('foo');
INSERT INTO Directory (Name, DirectoryId) VALUES ('sub foo', 1);
DELETE FROM Directory WHERE Id = 1; // FEHLER Foreign Key Constraint sollte greifen
DELETE FROM Directory WHERE Id = 2; // OK
|
|
|
PHP Source code |
1 2 |
INSERT INTO Directory (Name, DirectoryId) VALUES ('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!
|
|
|
PHP Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
sqlite> INSERT INTO Directory (Name) VALUES ('foo');
sqlite> INSERT INTO Directory (Name, DirectoryId) VALUES ('sub foo', 1);
sqlite> DELETE FROM Directory WHERE Id = 1;
sqlite> SELECT * FROM Directory;
2|sub foo|1
sqlite> .dump Directory
BEGIN TRANSACTION;
CREATE TABLE Directory (
Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Name VARCHAR(40) NOT NULL,
DirectoryId INTEGER NULL CONSTRAINT FK_DirectoryId REFERENCES Directory(Id) ON DELETE CASCADE CONSTRAINT C_Directory_Loop CHECK (Id != DirectoryId)
);
INSERT INTO "Directory" VALUES(2,'sub foo',1);
COMMIT;
|
|
|
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(40) NOT NULL
);
CREATE TABLE Directory_Has_SubDirectory (
Id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
DirectoryId INTEGER NOT NULL CONSTRAINT FK_DirectoryId REFERENCES Directory(Id) ON DELETE CASCADE,
SubDirectoryId INTEGER NOT NULL CONSTRAINT FK_SubDirectoryId REFERENCES Directory(Id) ON DELETE CASCA
DE,
CONSTRAINT C_Directory_Loop CHECK (DirectoryId != SubDirectoryId)
);
|
|
|
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 (DirectoryId, SubDirectoryId) VALUES (1, 2);
-- Datensatz löschen
DELETE FROM Directory WHERE Id = 1; // CONSTRAINT ERROR
|
|
|
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
sqlite> CREATE TABLE verzeichnis (
...> vid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
...> name VARCHAR(40) NOT NULL,
...> direntid INTEGER NOT NULL DEFAULT 0 REFERENCES vid ON INSERT RESTRICT,
...> FOREIGN KEY(vid) REFERENCES direntid ON DELETE CASCADE ON UPDATE CASCADE
...> );
sqlite> INSERT INTO verzeichnis ( name ) VALUES ( "Erste Zeile" );
sqlite> INSERT INTO verzeichnis ( name, direntid ) VALUES ( "Zweite Zeile", 1 );
sqlite> .dump verzeichnis
BEGIN TRANSACTION;
CREATE TABLE verzeichnis (
vid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name VARCHAR(40) NOT NULL,
direntid INTEGER NOT NULL DEFAULT 0 REFERENCES vid ON INSERT RESTRICT,
FOREIGN KEY(vid) REFERENCES direntid ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "verzeichnis" VALUES(1,'Erste Zeile',0);
INSERT INTO "verzeichnis" VALUES(2,'Zweite Zeile',1);
COMMIT;
sqlite> DELETE FROM verzeichnis WHERE direntid = 1;
sqlite> .dump verzeichnis
BEGIN TRANSACTION;
CREATE TABLE verzeichnis (
vid INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name VARCHAR(40) NOT NULL,
direntid INTEGER NOT NULL DEFAULT 0 REFERENCES vid ON INSERT RESTRICT,
FOREIGN KEY(vid) REFERENCES direntid ON DELETE CASCADE ON UPDATE CASCADE
);
INSERT INTO "verzeichnis" VALUES(1,'Erste Zeile',0);
COMMIT;
sqlite>
|