First of all thanks for the help and the words of support.
As I am learning MySQL from the web I know I'm going to make many errors and thanks in advance for your patience and understanding.
I'm making the project based in Java which is then creating a MySQL Database and Tables.
Generally it is going well, until I have encountered this problem. It has given me the biggest headache so far. I read many articles on the error 150. I have read error 150 on MySQL site and I think that I have fulfilled the ten reasons why a error 150 occurs.
The call that I'm making is calling is creating each Table. Here are some of the Tables which are covering the problem that I'm having.
This is the WorkCalendar Table
private String workCalandar = "CREATE TABLE WorkCalendar ("
+ "WorkCalendarIdNo INT(64) NOT NULL AUTO_INCREMENT,"
+ "WorkCalendarDate Date,"
+ "WorkCalendarDayCount INT(64),"
+ "WorkDayTypeIdNo INT(64),"
+ "PRIMARY KEY(WorkCalendarIdNo),"
+ "FOREIGN KEY(WorkDayTypeIdNo) REFERENCES WorkDayType(WorkDayTypeIdNo)"
+ ")";
This is the Department Table
private String department = "CREATE TABLE Department ("
+ "DeptIdNo INT(64) NOT NULL AUTO_INCREMENT,"
+ "DeptName VARCHAR(25),"
+ "PRIMARY KEY(DeptIdNo)"
+ ")";
This is the Specialist Table
private String specialist = "CREATE TABLE Specialist ("
+ "SpecIdNo INT(64) NOT NULL AUTO_INCREMENT,"
+ "PrefixIdNo INT(64),"
+ "SpecFirstName VARCHAR(30),"
+ "SpecSurname VARCHAR(35),"
+ "SpecDisplayName VARCHAR(72),"
+ "DeptIdNo INT(64),"
+ "PRIMARY KEY(SpecIdNo),"
+ "FOREIGN KEY(PrefixIdNo) REFERENCES Prefix(PrefixIdNo),"
+ "FOREIGN KEY(DeptIdNo) REFERENCES Department(DeptIdNo)"
+ ")";
This is the NewReferral Table
private String newReferral = "CREATE TABLE NewReferral("
+ "NewReferralIdNo INT(64) NOT NULL AUTO_INCREMENT,"
+ "PatientNumber VARCHAR(12),"
+ "NewReferralDate Date,"
+ "DeptIdNo INT(64),"
+ "SpecIdNo INT(64),"
+ "NewReferralMatched BOOLEAN,"
+ "WorkCalendarIdNo INT(64),"
+ "PRIMARY KEY(NewReferralIdNo),"
+ "FOREIGN KEY(DeptIdNo) REFERENCES Department(DeptIdNo),"
+ "FOREIGN KEY(SpecIdNo) REFERENCES Specialist(SpecIdNo),"
+ "FOREIGN KEY(WorkCalendarIdNo) REFERENCES WorkCalendar(WorkCalendarIdNo)"
+ ")";
The error is Can't create table 'basque30.newreferral' (errno: 150)
I have tracked the error down to the following line
+ "FOREIGN KEY(SpecIdNo) REFERENCES Specialist(SpecIdNo),"
Could someone to point out the error of my ways and advise me how to resolve it.
Make sure the table referenced already exists.
Error no: 150 -- There is a wrong primary key reference in your code.
It's due to a reference FOREIGN KEY(SpecIdNo) REFERENCES Specialist(SpecIdNo)
field does not exist.
As you mentioned
I have tracked the error down to the following line
+ "FOREIGN KEY(SpecIdNo) REFERENCES Specialist(SpecIdNo),"
↑
Make sure table Specialist exists in order to refer as foreign key in another table.
Check the order of table creation.
Not related, but might help in future.
If you have admin permission on the server, you may want to start by running the MySQL command
SHOW INNODB STATUS
for MySQL 5.5
SHOW ENGINE INNODB STATUS
immediately after receiving the error. This command displays log info and error details.
Related
I'm using JDBC in eclipse IDE , i want to put two foreign keys in my table 3 , one is referencing to the primary key in table 1 and one is referencing to the primary key in table 2. When i only put one foreign key constrains for any referencing table1 or table 2 , it works fine but when i include two it gives me sql exception as stated below:
java.sql.SQLSyntaxErrorException: 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(T2) references
Table2(T2) )' at line 1
String createString =
// TABLE 1
"CREATE TABLE " + this.tableName + " ( " +
"T1 varchar(50) NOT NULL PRIMARY KEY )";
// TABLE 2
"CREATE TABLE " + this.tableName + " ( " +`enter code here`
"T2 varchar(50) NOT NULL PRIMARY KEY )";
// TABLE 3
"CREATE TABLE " + this.tableName + " ( " +
"T1 varchar(50) " +
"T2 varchar(50) " +
"foreign key(T1) references Table1 (T1)" +
"foreign key(T2) references Table2(T2) )";
First, this is actually a MySQL question, unrelated to Java/JDBC. Secondly, and more importantly, you don't appear to be using the correct syntax, which would be...
CREATE TABLE TableName (
T1 varchar(50),
T2 varchar(50),
foreign key(T1) references Table1(T1),
foreign key(T2) references Table2(T2)
);
Formatted for your code, it would look like this:
String createString = "CREATE TABLE " + this.tableName + " ( " +
" T1 varchar(50)," +
" T2 varchar(50)," +
" foreign key(T1) references Table1(T1)," +
" foreign key(T2) references Table2(T2));";
You were missing commas after each item in the items list for your CREATE TABLE statement.
To build the References direct in MS-Access is no Problem.
To do it with UCanAccess results in a "net.ucanaccess.jdbc.UcanaccessSQLException:...".
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection connection = DriverManager.getConnection("jdbc:ucanaccess://e:/TestDB.accdb;memory=true");
Statement statement = connection.createStatement();
//
String tableToBeReferenced = "PersonsTable";
String tableWithTheReferences = "RelationShipsTable";
try {// Tidy up
statement.execute("DROP TABLE " + tableWithTheReferences);
} catch (Exception exeption) {}
try {// Tidy up
statement.execute("DROP TABLE " + tableToBeReferenced);
} catch (Exception exeption) {}
statement.execute("CREATE TABLE " + tableToBeReferenced + "(ID autoincrement NOT NULL PRIMARY KEY,"//
+ "Name VARCHAR(255)"//
+ ")");
statement.execute("CREATE TABLE " + tableWithTheReferences + "(ID LONG NOT NULL PRIMARY KEY,"//
+ "RelationShip VARCHAR(255) NOT NULL DEFAULT 'FRIENDS',"//
+ "Person1Id LONG NOT NULL,"//
+ "Person2Id LONG NOT NULL)");
// reference #1
statement.execute("ALTER TABLE " + tableWithTheReferences + //
" ADD CONSTRAINT FOREIGN_KEY_1 FOREIGN KEY (Person1Id) REFERENCES " //
+ tableToBeReferenced + "(ID) ON DELETE CASCADE");
// reference #2
statement.execute("ALTER TABLE " + tableWithTheReferences + //
" ADD CONSTRAINT FOREIGN_KEY_2 FOREIGN KEY (Person2Id) REFERENCES " //
+ tableToBeReferenced + "(ID) ON DELETE CASCADE");
If I create only the first Reference it works.
If I create only the second Reference it works.
But when I try to build both References it fails.
I am able to reproduce the issue under UCanAccess 4.0.3. Neither HSQLDB nor Jackcess has a problem with creating two independent FK relationships between the same two tables, so it looks like it might be a bug in UCanAccess. I will report the issue to the UCanAccess development team and update this answer with any news.
Update:
A fix for this issue has been implemented and will be included in the UCanAccess 4.0.4 release.
I think it will not work since you have "ON DELETE CASCADE" for both your foreign keys.
I have Created a database with this code Some days ago
sql = "Create Database if not exists My_Test_Project";
stmnt.executeUpdate(sql);
And Created Some tables at that time. Now I'm creating two new tables in it one with this query
sql = "CREATE TABLE if not exists My_Test_Project.Sales_Invoice_Help "
+ "(inv_help_id INTEGER,"
+ "item VARCHAR(255),"
+ "qty INTEGER,"
+ "rate DECIMAL (7, 2),"
+ "total DECIMAL (7, 2),"
+ "sale_inv_id INTEGER,"
+ " PRIMARY KEY (inv_help_id),FOREIGN KEY (sale_inv_id) REFERENCES Sales_Invoice (sale_inv_id))";
stmnt.executeUpdate(sql);
And when I Executed my program it throw Exception
SEVERE: null
java.sql.SQLException: No database selected
But at the same time this query executed successfully
sql = "CREATE TABLE if not exists My_Test_Project.Sales_Invoice "
+ "(sale_inv_id INTEGER not NULL, "
+ "date VARCHAR(255), "
+ "acc_name VARCHAR(255),"
+ "due_date VARCHAR(255),"
+ "customer_name VARCHAR(255),"
+ "receipt_no VARCHAR(255),"
+ "freight_charges INTEGER,"
+ "deliver_to VARCHAR(255),"
+ "deliver_date VARCHAR(255),"
+ "total INTEGER,"
+ "discount INTEGER,"
+ "g_total INTEGER ,"
+ " PRIMARY KEY (sale_inv_id))";
stmnt.executeUpdate(sql);
Note: Sales_Invoice table is first in sequence and in code too.
I do not know why its throwing exception. Can you please guide me.
I would believe in this line you will need to add the Database name:
REFERENCES Sales_Invoice (sale_inv_id)
So it should now be:
REFERENCES My_Test_Project.Sales_Invoice (sale_inv_id)
I've got a DatabaseHelper class where I create the tables.
The first table (recordings) is working fine, but the second one is throwing an Exception when I try to work with it (Insert/Select...).
here is my code from the DatabaseHelper:
public void onCreate(SQLiteDatabase db) {
String createTable="CREATE TABLE recordings" +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
" name VARCHAR(255) NOT NULL," +
" datestart VARCHAR(255) NOT NULL" +
")";
String createTable2="CREATE TABLE recording_image" +
"(_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"imageurl VARCHAR(255) NOT NULL," +
"imagetime INTEGER NOT NULL," +
"prot_id INTEGER NOT NULL," +
"FOREIGN KEY(prot_id) REFERENCES recordings(_id) ON DELETE CASCADE" +
")";
db.execSQL(createTable);
db.execSQL(createTable2);
}
the Exception is:
no such table: recording_image.
What could cause this problem?
Yes, try to clear data of the application, and you can also look to the version of the db, If the version of the db you want to create are higher than the current db on the phone you have to delete and create again all the db.
please see : SQLiteOpenHelper
Hope that will help you :)
Problem solved. Just reinstalling the App or changing the Version number of the Database helped.
The first 2 go through without a hitch, but when I try to do the last one, I get this error message:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot add or update a child row: a foreign key constraint fails (corejava., CONSTRAINT #sql-8b8_50_ibfk_2 FOREIGN KEY (call_Number) REFERENCES book (call_Number))
There has got to be a way around this right?
Please see corrected code below: For any other newbies out there, my transaction call_Number INSERT field had incorrect values (tiny, minor, incorrect values!!!!)
s.executeUpdate (
"CREATE TABLE transaction ("
+ "patron_ID CHAR(10) NOT NULL,"
+ "call_Number CHAR(10) NOT NULL, check_Out_Date DATE NOT NULL, check_In_Date DATE NOT NULL,"
+ "PRIMARY KEY (patron_ID, call_Number),"
+ "overdue CHAR(15), total_Charge FLOAT)");
count3 = s.executeUpdate (
"INSERT INTO transaction"
+ " VALUES"
+ "('P222200000','MY.111.000','2011-06-06','2011-06-12','yes',5.00),"
+ "('P222200001','MY.111.001','2011-07-06','2011-07-12','no',5.00),"
+ "('P222200002','SF.111.002','2011-08-06','2011-08-12','yes',5.00),"
+ "('P222200003','SF.111.003','2011-09-06','2011-09-12','no',5.00),"
+ "('P222200004','AV.111.004','2011-10-06','2011-10-12','yes',5.00),"
+ "('P222200005','AV.111.005','2011-11-06','2011-11-12','no',5.00),"
+ "('P222200006','CO.111.006','2011-12-06','2011-12-12','yes',5.00),"
+ "('P222200007','CO.111.007','2011-01-06','2011-01-12','no',5.00),"
+ "('P222200008','IN.111.008','2011-02-06','2011-02-12','yes',5.00),"
s.executeUpdate(
"ALTER TABLE transaction ADD FOREIGN KEY (patron_ID) references " + dbName + ".patron (patron_ID)");
s.executeUpdate (
"ALTER TABLE patron ADD FOREIGN KEY (call_Number) references " + dbName + ".book (call_Number)");
s.executeUpdate (
"ALTER TABLE transaction ADD FOREIGN KEY (call_Number) references " + dbName + ".book (call_Number)");