Announcement

Collapse
No announcement yet.

Fehler bei Foreign Key Syntax

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Fehler bei Foreign Key Syntax

    Hallo Leute,

    ich versuche eine kleine Datenbank aufzusetzen um Betriebsdaten von Geräten katalogisiert zu speichern . Ich bin aber ein absoluter neuling. und habe ein Problem mit dem Foreign-Key.
    mein sqript sieht so aus:



    create table registration_tab

    (
    registrationID bigint not null,
    clockstamp smalldatetime,
    deviceID smallint,

    Primary Key (registrationID)
    foreign key (deviceID) references device_tab(deviceID
    )

    create table device_tab

    (
    deviceID smallint not null,
    serialnumber bigint,
    devicetypeID smallint,


    Primary Key (devivceID)
    foreign key (devicetypeID) references devicetype_tab(devtypeID)
    )

    create table devicetype_tab

    (
    devicetypeID smallint not null,
    wrdescription text,


    Primary Key (devicetypeID)
    )

    create table channellist_tab

    (
    channelID int,
    devicetypeID smallint,

    Primary Key (channelID, devicetypeID)
    foreign key (channelID) references channel_tab(channelID)
    foreign key (devicetypeID) references devicetype_tab(devicetypeID)
    )

    create table channel_tab

    (
    channelID int not null,
    channelname text,
    unit text,
    multi smallint,


    Primary Key (channelID)
    )

    create table regvalue_tab

    (
    registrationvalueID bigint not null,
    value bigint,
    registrationID bigint,
    channelID int,


    Primary Key (registrationvalueID)
    foreign key (registrationID) references registration_tab(registrationID)
    foreign key (channelID) references channel_tab(ChannelID)
    )

    und das Ergebnisfenster haut immer diesen Fehler raus:

    Server: Nachr.-Nr. 156, Schweregrad 15, Status 1, Zeile 11
    Falsche Syntax in der Nähe des foreign-Schlüsselwortes.
    Server: Nachr.-Nr. 156, Schweregrad 15, Status 1, Zeile 23
    Falsche Syntax in der Nähe des foreign-Schlüsselwortes.
    Server: Nachr.-Nr. 156, Schweregrad 15, Status 1, Zeile 43
    Falsche Syntax in der Nähe des foreign-Schlüsselwortes.
    Server: Nachr.-Nr. 156, Schweregrad 15, Status 1, Zeile 69
    Falsche Syntax in der Nähe des foreign-Schlüsselwortes.


    Was mache ich da falsch?
    Ich habe die FK-Syntax aus einem O´Reilly-Buch.

    Kann mir da einer von euch helfen.Bitte nicht rüffeln, aber ich bin wirklich newest newbie.
    Würde mich über eine Antwort freuen. Vielleicht kann mir ja auch einer die Syntax mal kurz erklärem
    Danke.

    LG

    Kasu

  • #2
    Hallo,

    beim MS SQL Server kann man 3 verschiedene Wege für das Anlegen eines Fremdschlüsseln verwenden. In der Regel werden zuerst die übergeordneten Tabellen erstellt, so dass beim Deklarieren des Fremdschlüsseln die referenzierten Tabellen bereits bekannt sind. Das folgende Beispiel stellt 2 der Alternativen vor:

    <div style="font-family: Consolas; font-size: 10pt; color: black; background: white;"><p style="margin: 0px;"><span style="color: blue;">CREATE TABLE </span>Windows</p><p style="margin: 0px;">( </p><p style="margin: 0px;">&nbsp; id&nbsp;&nbsp;&nbsp; &nbsp; <span style="color: blue;">INT </span>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">NOT NULL IDENTITY PRIMARY KEY</span>,</p><p style="margin: 0px;">&nbsp; version NVARCHAR(30) <span style="color: blue;">NOT NULL</span></p><p style="margin: 0px;">)</p><p style="margin: 0px;">GO</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;"><span style="color: blue;">CREATE TABLE </span>SQLServer</p><p style="margin: 0px;">( </p><p style="margin: 0px;">&nbsp; id&nbsp;&nbsp;&nbsp; &nbsp; <span style="color: blue;">INT </span>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">NOT NULL IDENTITY PRIMARY KEY</span>,</p><p style="margin: 0px;">&nbsp; version NVARCHAR(35) <span style="color: blue;">NOT NULL</span></p><p style="margin: 0px;">)</p><p style="margin: 0px;">GO</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;"><span style="color: green;">-- Fremschlüssel-Alternative A) REFERENCES</span></p><p style="margin: 0px;"><span style="color: blue;">CREATE TABLE </span>Kombinationen</p><p style="margin: 0px;">(</p><p style="margin: 0px;">&nbsp; id&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span style="color: blue;">INT NOT NULL IDENTITY PRIMARY KEY</span>,</p><p style="margin: 0px;">&nbsp; wid&nbsp;&nbsp;&nbsp; &nbsp; <span style="color: blue;">INT NOT NULL REFERENCES </span>Windows(id),</p><p style="margin: 0px;">&nbsp; sid&nbsp;&nbsp;&nbsp; &nbsp; <span style="color: blue;">INT NOT NULL REFERENCES </span>SQLServer(id),</p><p style="margin: 0px;">&nbsp; getestet <span style="color: blue;">BIT NOT NULL DEFAULT </span>1</p><p style="margin: 0px;">)</p><p style="margin: 0px;">GO</p><p style="margin: 0px;">&nbsp;</p><p style="margin: 0px;"><span style="color: green;">-- Fremschlüssel-Alternative B) CONSTRAINT xyz FOREIGN KEY</span></p><p style="margin: 0px;"><span style="color: blue;">CREATE TABLE </span>Kombinationen2</p><p style="margin: 0px;">(</p><p style="margin: 0px;">&nbsp; id&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; <span style="color: blue;">INT NOT NULL IDENTITY CONSTRAINT </span>pkKomb <span style="color: blue;">PRIMARY KEY</span>,</p><p style="margin: 0px;">&nbsp; wid&nbsp;&nbsp;&nbsp; &nbsp; <span style="color: blue;">INT NOT NULL CONSTRAINT </span>fkKombWin <span style="color: blue;">FOREIGN KEY</span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">REFERENCES </span>Windows(id),</p><p style="margin: 0px;">&nbsp; sid&nbsp;&nbsp;&nbsp; &nbsp; <span style="color: blue;">INT NOT NULL CONSTRAINT </span>fkKombSQL <span style="color: blue;">FOREIGN KEY </span></p><p style="margin: 0px;">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <span style="color: blue;">REFERENCES </span>SQLServer(id),</p><p style="margin: 0px;">&nbsp; getestet <span style="color: blue;">BIT NOT NULL CONSTRAINT </span>dfKombGetestet <span style="color: blue;">DEFAULT </span>1</p><p style="margin: 0px;">)</p><p style="margin: 0px;">GO</p></div>

    Ob die abgekürzte oder ausführliche Schreibweise von Vorteil ist, hängt von den späteren Aktionen ab. Immer dann, wenn der MS SQL Server bei der abgekürzten Schreibweise den CONSTRAINT-Namen selbst vergibt, muss dieser immer dann erst nachträglich ermittelt werden, wenn eine ALTER TABLE-Anweisung das Verhalten der Tabelle ändern soll. Mit der ausführlichen Schreibweise hat der Entwickler zwar am Anfang etwas mehr Arbeit – aber dafür wird es bei der nächsten Umstrukturierung der Datenbank umso einfacher

    Der 3. Weg besteht darin, den Fremdschlüsseln nachträglich hinzuzufügen.

    Comment


    • #3
      Danke.
      Dann werde ich wohl die ausführliche nehmen.
      Mit den Tabellen hast Du auch recht. Die sind in der falschen Reihenfolge.;-)

      Gruß

      Kasu

      Comment

      Working...
      X