Results 1 to 10 of 10
  1. #1
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Problem with Foreign Key in Database

    Hello,

    I want to make Two table in my Data base one table is with Primary Key and the another with the Foreign Key

    Java Code:
    String createTable1 = "CREATE TABLE  eumonis.DistributionGridOperator " 
    		          + "(operatorId VARCHAR(25)  PRIMARY KEY NOT NULL," +
    		          		"name VARCHAR(75) NOT NULL)";
    
    	 String createTable2 = "CREATE TABLE  eumonis.PowerPlant " 
    		          + "(plantId VARCHAR(25) PRIMARY KEY NOT NULL," 
    		            +"manufacturerId VARCHAR(25) NOT NULL," 
    		            +"distOpId VARCHAR(25)  NOT NULL,"
    		            + "TransOpId VARCHAR(25)  NOT NULL,"
    		            + "LocationId VARCHAR(25)  NOT NULL,"
    		            + "StateId VARCHAR(25) NOT NULL,"
    		            + "LevelId VARCHAR(25) NOT NULL,"
    		            + "SourceId VARCHAR(25) NOT NULL)" +
    		            " FOREIGN KEY (distOpId) REFERENCES eumonis.DistributionGridOperator(operatorId))";
    now when i run the code i get the following error:
    Java Code:
    SQLException: You have an error in your SQL syntax;
    check the manual that corresponds to your MySQL server version for the right syntax to use near 
    'FOREIGN KEY (distOpId) REFERENCES eumonis.DistributionGridOperator(operatorId))' at line 1
    how to fix it?
    Last edited by vector_ever; 08-10-2013 at 02:31 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Foreign Key in Database

    What does this have to do with Java?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Foreign Key in Database

    Nothing, but i tried to write mysql with java

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default Re: Foreign Key in Database

    I wonder what this phrase means: "check the manual that corresponds to your MySQL server version for the right syntax"

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Foreign Key in Database

    The full error shows:
    Java Code:
    SQLException: You have an error in your SQL syntax;
    check the manual that corresponds to your MySQL server version for the right syntax to use near 
    'FOREIGN KEY (distOpId) REFERENCES eumonis.DistributionGridOperator(operatorId))' at line 1

  6. #6
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Re: Foreign Key in Database

    Ay way it must be mentioned, that the first Table "DistributionGridOperator " have been already created, and i try just to create the second Table "PowerPlant" but it failed and i got the error

  7. #7
    vector_ever is offline Senior Member
    Join Date
    Jul 2013
    Posts
    187
    Rep Power
    2

    Default Problem with Foreign Key in Database

    Hello,

    I want to make Two table in my Data base one table is with Primary Key and the another with the Foreign Key

    Java Code:
    String createTable1 = "CREATE TABLE  eumonis.DistributionGridOperator " 
    		          + "(operatorId VARCHAR(25)  PRIMARY KEY NOT NULL," +
    		          		"name VARCHAR(75) NOT NULL)";
    
    	 String createTable2 = "CREATE TABLE  eumonis.PowerPlant " 
    		          + "(plantId VARCHAR(25) PRIMARY KEY NOT NULL," 
    		            +"manufacturerId VARCHAR(25) NOT NULL," 
    		            +"distOpId VARCHAR(25)  NOT NULL,"
    		            + "TransOpId VARCHAR(25)  NOT NULL,"
    		            + "LocationId VARCHAR(25)  NOT NULL,"
    		            + "StateId VARCHAR(25) NOT NULL,"
    		            + "LevelId VARCHAR(25) NOT NULL,"
    		            + "SourceId VARCHAR(25) NOT NULL)" +
    		            " FOREIGN KEY (distOpId) REFERENCES eumonis.DistributionGridOperator(operatorId))";
    now when i run the code i get the following error:
    Java Code:
    SQLException: You have an error in your SQL syntax;
    check the manual that corresponds to your MySQL server version for the right syntax to use near 
    'FOREIGN KEY (distOpId) REFERENCES eumonis.DistributionGridOperator(operatorId))' at line 1
    Ay way it must be mentioned, that the first Table "DistributionGridOperator " have been already created, and i try just to create the second Table "PowerPlant" but it failed and i got the error
    how to fix it?

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,234
    Rep Power
    19

    Default Re: Problem with Foreign Key in Database

    Please go through the Forum Rules, particularly the second paragraph.

    I've merged two threads here.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: Problem with Foreign Key in Database

    I know this isn't Java, but take a CLOSE look at your SQL. That ")" before the constraint declaration closes your create table SQL statement, does it not?

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: Problem with Foreign Key in Database

    I was just going to say "count your brackets"...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 3
    Last Post: 12-17-2012, 10:28 AM
  2. Foreign Key
    By tristars in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 12-16-2011, 09:31 AM
  3. jdbc foreign keys problem
    By axenos in forum JDBC
    Replies: 7
    Last Post: 03-15-2011, 11:07 AM
  4. innodb and foreign keys problem
    By Pierced1 in forum JDBC
    Replies: 0
    Last Post: 06-29-2009, 02:59 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •