我有一个MySQL数据库。表格之一是:
sqlString = "CREATE TABLE Parents ("
+ "ChildID SMALLINT NOT NULL,"
+ "Name VARCHAR(50) NOT NULL,"
+ "Address VARCHAR(100),"
+ "MobilePhone LONG,"
+ "WorkPhone LONG,"
+ "PRIMARY KEY (ChildID, Name),"
+ "FOREIGN KEY(ChildID) REFERENCES Children(ChildID))";
stmt.executeUpdate(sqlString);
我首先执行此更新:
sqlString = "INSERT INTO Parents (ChildID, Name) VALUES (1, 'Mom')";
它工作正常。但是,当我需要向父级添加更多信息时,只能从此得到无法解释的SQLSyntaxError:
sqlString = "UPDATE Parents SET Address='Fakestreet 1'WHERE ChildID=1 AND Name='Mom'";
如果删除AND条件,它的工作原理很完美。我已经竭尽所能解决它。
为什么会出现语法错误?
我猜问题是我的WHERE子句中的2列是Parents的主键。我是否应该以某种方式使用外键来添加有关孩子父母的信息?声明实际密钥是否有实际用途?我看到的唯一方式是在DBMS内部使用。
最佳答案
在where子句“ ChildID = 1AND”中,在AND之前给空格