Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Cannot create a foreign key reference (probably my fault)

Cannot create a foreign key reference (probably my fault)

Scheduled Pinned Locked Moved Database
databasequestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I create a new SQL CE database and try to add two tables with a key linkiing them. The first table is created successfully with the statement:

    CREATE TABLE Classes(ClassId INTEGER NOT NULL PRIMARY KEY, Class NVARCHAR(64) NOT NULL )

    and I add 3 rows to it successfully. I then try to create the second table with a foreign key thus:

    CREATE TABLE Accounts(Account NVARCHAR(64) NOT NULL, Telephone NVARCHAR(128), ClassId INTEGER FOREIGN KEY REFERENCES Classes(ClassId) )

    but this fails with the message:

    The constraint specified is not valid

    What am I doing wrong?

    S L 2 Replies Last reply
    0
    • L Lost User

      I create a new SQL CE database and try to add two tables with a key linkiing them. The first table is created successfully with the statement:

      CREATE TABLE Classes(ClassId INTEGER NOT NULL PRIMARY KEY, Class NVARCHAR(64) NOT NULL )

      and I add 3 rows to it successfully. I then try to create the second table with a foreign key thus:

      CREATE TABLE Accounts(Account NVARCHAR(64) NOT NULL, Telephone NVARCHAR(128), ClassId INTEGER FOREIGN KEY REFERENCES Classes(ClassId) )

      but this fails with the message:

      The constraint specified is not valid

      What am I doing wrong?

      S Offline
      S Offline
      Simon_Whale
      wrote on last edited by
      #2

      Have a read of the first answer in this StackOverflow Question[^] on the same subject. The first answer suggests that you remove the FOREIGN KEY part of your create table statement

      Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

      L 1 Reply Last reply
      0
      • L Lost User

        I create a new SQL CE database and try to add two tables with a key linkiing them. The first table is created successfully with the statement:

        CREATE TABLE Classes(ClassId INTEGER NOT NULL PRIMARY KEY, Class NVARCHAR(64) NOT NULL )

        and I add 3 rows to it successfully. I then try to create the second table with a foreign key thus:

        CREATE TABLE Accounts(Account NVARCHAR(64) NOT NULL, Telephone NVARCHAR(128), ClassId INTEGER FOREIGN KEY REFERENCES Classes(ClassId) )

        but this fails with the message:

        The constraint specified is not valid

        What am I doing wrong?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        Richard MacCutchan wrote:

        What am I doing wrong?

        Specifying the constraint, below code should work for sqlite3; The question is about SQL CE :)

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        L 1 Reply Last reply
        0
        • S Simon_Whale

          Have a read of the first answer in this StackOverflow Question[^] on the same subject. The first answer suggests that you remove the FOREIGN KEY part of your create table statement

          Every day, thousands of innocent plants are killed by vegetarians. Help end the violence EAT BACON

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Thanks for the reply, my syntax was wrong, Eddy found the answer.

          1 Reply Last reply
          0
          • L Lost User

            Richard MacCutchan wrote:

            What am I doing wrong?

            Specifying the constraint, below code should work for sqlite3; The question is about SQL CE :)

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Thanks Eddy, your suggestion did work (for SQL CE). I have been staring at this for over an hour and could not see the subtlety of the optional parameters in the documentation[^]. [edit] For the record the correct syntax is:

            CREATE TABLE Accounts(Account NVARCHAR(64) NOT NULL, Telephone NVARCHAR(128), ClassId INTEGER, FOREIGN KEY (ClassId) REFERENCES Classes(ClassId) )

            [edit]

            L 1 Reply Last reply
            0
            • L Lost User

              Thanks Eddy, your suggestion did work (for SQL CE). I have been staring at this for over an hour and could not see the subtlety of the optional parameters in the documentation[^]. [edit] For the record the correct syntax is:

              CREATE TABLE Accounts(Account NVARCHAR(64) NOT NULL, Telephone NVARCHAR(128), ClassId INTEGER, FOREIGN KEY (ClassId) REFERENCES Classes(ClassId) )

              [edit]

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Richard MacCutchan wrote:

              your suggestion did work (for SQL CE).

              Cool - one of those rare cases where both implemented the same sql-standard. Makes me curious how easy (or hard) it is to switch from one to the other.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups