I am creating a java application and I have connected to mysql database. I tried creating a table with foreign key and I got error Cannot add foreign key constraint below are my tables
execute("CREATE TABLE " + BBBBB + "("
+ " id int(11) NOT NULL AUTO_INCREMENT,\n"
+ " consultantID varchar(225) NOT NULL,\n"
+ " name varchar(225) NOT NULL,\n"
+ " gender varchar(225) NOT NULL,\n"
+ " age varchar(225) NOT NULL,\n"
+ " email varchar(225) NOT NULL,\n"
+ " mobile varchar(225) NOT NULL,\n"
+ " address varchar(225) NOT NULL,\n"
+ " PRIMARY KEY (id)"
+ ") ENGINE=InnoDB DEFAULT CHARSET=latin1"
);
execute("CREATE TABLE " + AAAAA + "("
+ " id int(11) NOT NULL AUTO_INCREMENT,\n"
+ " patientID varchar(225) NOT NULL,\n"
+ " name varchar(225) NOT NULL,\n"
+ " gender varchar(225) NOT NULL,\n"
+ " age varchar(225) NOT NULL,\n"
+ " insurance varchar(225),\n"
+ " mobile varchar(225) NOT NULL,\n"
+ " address varchar(225) NOT NULL,\n"
+ " PRIMARY KEY (id)"
+ ") ENGINE=InnoDB DEFAULT CHARSET=latin1"
);
execute("CREATE TABLE " + CCCCC + "("
+ " id int(11) NOT NULL AUTO_INCREMENT,\n"
+ " patientID varchar(225) NOT NULL,\n"
+ " consultantID varchar(225) NOT NULL,\n"
+ " time varchar(225) NOT NULL,\n"
+ " date varchar(225) NOT NULL,\n"
+ " renewCount INTEGER default 0,\n"
+ " FOREIGN KEY (patientID) REFERENCES AAAAA(pID),\n"
+ " FOREIGN KEY (consultantID) REFERENCES BBBBB(cID),\n"
+ " PRIMARY KEY (id)"
+ ") ENGINE=InnoDB DEFAULT CHARSET=latin1"
I got an error as stated above. additional codes would be added on request. Thanks
in the third statement, pID and cID don't seem to exist, they should be patientID and consultantID
to create the tables you are using the variables AAAAA and BBBBB, but in the third statement you are referring to them as the string constants "AAAAA" and "BBBBB", which might not be correct, you should always use the variables, for example:
" ... REFERENCES " + BBBBB + "(cID) ... "
Related
Trying to pass this JSON in the body of a get request in rest assured. Is creating a JSON object a way to go ?
{
"pharmacyListId": null,
"pharmacyListName": "PTA",
"customerSetTypeId": 1,
"customerId": null,
"pharmacyId": null,
"providerAffiliateId": null,
"providerPlanId": null,
"providerChain": null,
"providerNetworkId": null,
"pharmacyZip": null,
"pharmacyZipExtension": null,
"pharmacyStateCode": "MI",
"fillDate": "2021-01-01",
"relationshipId": null,
"organizationId": null,
"paymentCentreId": null,
"providerNpi": null,
"remitReconId": null,
"countryCode": null,
"memberState": null,
"memberZipCode": null,
"memberZipCodeExtension": null
}
You can create a string for your json :
String Json = "{\n" +
" \"pharmacyListId\": null,\n" +
" \"pharmacyListName\": \"PTA\",\n" +
" \"customerSetTypeId\": 1,\n" +
" \"customerId\": null,\n" +
" \"pharmacyId\": null,\n" +
" \"providerAffiliateId\": null,\n" +
" \"providerPlanId\": null,\n" +
" \"providerChain\": null,\n" +
" \"providerNetworkId\": null,\n" +
" \"pharmacyZip\": null,\n" +
" \"pharmacyZipExtension\": null,\n" +
" \"pharmacyStateCode\": \"MI\",\n" +
" \"fillDate\": \"2021-01-01\",\n" +
" \"relationshipId\": null,\n" +
" \"organizationId\": null,\n" +
" \"paymentCentreId\": null,\n" +
" \"providerNpi\": null,\n" +
" \"remitReconId\": null,\n" +
" \"countryCode\": null,\n" +
" \"memberState\": null,\n" +
" \"memberZipCode\": null,\n" +
" \"memberZipCodeExtension\": null\n" +
"}";
and with rest-assured you can have something like that :
Response response = given()
.body(Json)
.when()
.get("http://restAdress")
.then()
.extract().response();
First of all you need to clarify the case. Despite the standard does not put explicit restriction to sending payload with GET request there is a very high probability that your API server or HTTP server that hosts your API code would ignore that payload.
If that is still the case, the simplest way to send your json with RestAssured is:
RestAssured
.with()
.proxy("localhost", proxyServer.getPort())
.body("{\n" +
" \"pharmacyListId\": null,\n" +
" \"pharmacyListName\": \"PTA\",\n" +
" \"customerSetTypeId\": 1,\n" +
" \"customerId\": null,\n" +
" \"pharmacyId\": null,\n" +
" \"providerAffiliateId\": null,\n" +
" \"providerPlanId\": null,\n" +
" \"providerChain\": null,\n" +
" \"providerNetworkId\": null,\n" +
" \"pharmacyZip\": null,\n" +
" \"pharmacyZipExtension\": null,\n" +
" \"pharmacyStateCode\": \"MI\",\n" +
" \"fillDate\": \"2021-01-01\",\n" +
" \"relationshipId\": null,\n" +
" \"organizationId\": null,\n" +
" \"paymentCentreId\": null,\n" +
" \"providerNpi\": null,\n" +
" \"remitReconId\": null,\n" +
" \"countryCode\": null,\n" +
" \"memberState\": null,\n" +
" \"memberZipCode\": null,\n" +
" \"memberZipCodeExtension\": null\n" +
"}\n")
.contentType(ContentType.JSON)
.get("http://YOUR_ENDPOINT");
You can use gson for clean code
Depedency:
implementation 'com.google.code.gson:gson:2.9.0'
Use gson:
import com.google.gson.JsonObject;
Sample code:
JsonObject body = new JsonObject();
body.addProperty("pharmacyListId", (String) null);
body.addProperty("pharmacyListName", "PTA");
body.addProperty("customerSetTypeId", 1);
body.addProperty("customerId", (String) null);
body.addProperty("pharmacyId", (String) null);
body.addProperty("providerAffiliateId", (String) null);
body.addProperty("providerPlanId", (String) null);
body.addProperty("providerChain", (String) null);
Output:
{
"pharmacyListId": null,
"pharmacyListName": "PTA",
"customerSetTypeId": 1,
"customerId": null,
"pharmacyId": null,
"providerAffiliateId": null,
"providerPlanId": null,
"providerChain": null
}
I hope this helps.
as i am working on a minecraft server some plugin does not make a certain table itself.
For who is interested: https://www.spigotmc.org/resources/craftconomy.2395/
But in the github files i found the java script where it does generate the needed table.
public final String createTableMySQL = "CREATE TABLE IF NOT EXISTS " + getPrefix() + TABLE_NAME + " (" +
" `" + BALANCE_FIELD + "` double DEFAULT NULL," +
" `" + WORLD_NAME_FIELD + "` varchar(255)," +
" `username_id` int(11)," +
" `" + CURRENCY_FIELD + "` varchar(50)," +
" PRIMARY KEY (" + WORLD_NAME_FIELD + ", username_id, currency_id)," +
" CONSTRAINT `"+getPrefix()+"fk_balance_account`" +
" FOREIGN KEY (username_id)" +
" REFERENCES " + getPrefix() + AccountTable.TABLE_NAME + "(id) ON UPDATE CASCADE ON DELETE CASCADE," +
" CONSTRAINT `"+getPrefix()+"fk_balance_currency`" +
" FOREIGN KEY (" + CURRENCY_FIELD + ")" +
" REFERENCES " + getPrefix() + CurrencyTable.TABLE_NAME +"(name) ON UPDATE CASCADE ON DELETE CASCADE" +
") ENGINE=InnoDB CHARSET=utf8;";
Could someone help me to make a SQL query out of this, which just can be dropped into PHPMyadmin?
Thanks already for reading.
I already solved it myself, no reason for reaction anymore.
Im attempting to create 2 tables, but I dont fully understand where Im going wrong. It's obviously a syntax error, and Im new to SQL in general, can someone explain to me what Im doing wrong here?
String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
"uuid CHAR(36) NOT NULL," +
" team_name VARCHAR(255) NOT NULL," +
" BOOLEAN manager NOT NULL," +
" BOOLEAN team_chat NOT NULL," +
" PRIMARY KEY (uuid))";
String query2 = "CREATE TABLE IF NOT EXISTS " + secondTableName + " (" +
"team_name VARCHAR(255) NOT NULL," +
" team_password VARCHAR(255) NOT NULL" +
" BOOLEAN friendly_fire NOT NULL," +
" VARCHAR(255) hq," +
" VARCHAR(255) rally," +
" PRIMARY KEY (team_name))";
This is my stacktrace:
WARN com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'manager NOT NULL, BOOLEAN team_chat NOT NULL, PRIMARY KEY (uuid))' at line 1
Code used to execute:
private void createTables(String tableName, String secondTableName) throws SQLException {
try {
checkConnection();
} catch (Exception e) {
e.printStackTrace();
}
try {
String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
"uuid CHAR(36) NOT NULL," +
" team_name VARCHAR(255) NOT NULL," +
" BOOLEAN manager NOT NULL," +
" BOOLEAN team_chat NOT NULL," +
" PRIMARY KEY (uuid))";
String query2 = "CREATE TABLE IF NOT EXISTS " + secondTableName + " (" +
"team_name VARCHAR(255) NOT NULL," +
" team_password VARCHAR(255) NOT NULL" +
" BOOLEAN friendly_fire NOT NULL," +
" VARCHAR(255) hq," +
" VARCHAR(255) rally," +
" PRIMARY KEY (team_name))";
PreparedStatement preparedStatement = connection.prepareStatement(query);
PreparedStatement preparedStatement2 = connection.prepareStatement(query2);
preparedStatement.execute();
preparedStatement2.execute();
main.getLogger().log(Level.INFO, "Tables " + tableName + " and " + secondTableName + " were successfully created.");
} catch (Exception e) {
e.printStackTrace();
} finally {
close();
}
}
The problem is presence of MAX in the table definition. VARCHAR expects a number as an argument so you either need to define MAX as variable and pass it (from Java) or just replace MAX with a number, e.g.:
String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
"uuid CHAR(36) NOT NULL," +
" team_name VARCHAR( + " + MAX + ") NOT NULL," +
" BOOLEAN manager NOT NULL," +
" BOOLEAN team_chat NOT NULL," +
" PRIMARY KEY (uuid))";
OR
String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
"uuid CHAR(36) NOT NULL," +
" team_name VARCHAR(255) NOT NULL," +
" BOOLEAN manager NOT NULL," +
" BOOLEAN team_chat NOT NULL," +
" PRIMARY KEY (uuid))";
If you do not know the max length then you can use LONGTEXT as type.
Also, we need to fix the ordering of type and column names, it needs to be manager BOOLEAN and not BOOLEAN manager, e.g.:
String query = "CREATE TABLE IF NOT EXISTS " + tableName + " (" +
"uuid CHAR(36) NOT NULL," +
" team_name VARCHAR(255) NOT NULL," +
" manager BOOLEAN NOT NULL," +
" team_chat BOOLEAN NOT NULL," +
" PRIMARY KEY (uuid))";
String query2 = "CREATE TABLE IF NOT EXISTS " + secondTableName + " (" +
"team_name VARCHAR(255) NOT NULL," +
" team_password VARCHAR(255) NOT NULL" +
" friendly_fire BOOLEAN NOT NULL," +
" hq VARCHAR(255)," +
" rally VARCHAR(255)," +
" PRIMARY KEY (team_name))";
The column name has to come before the data type. So
BOOLEAN manager NOT NULL,
BOOLEAN team_chat NOT NULL,
should be
manager BOOLEAN NOT NULL,
team_chat BOOLEAN NOT NULL,
Can any one help identify where the problem is with this sql create statement. Am trying to excute using JDBC driver but it returns an error:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'CREATE TABLE IF NOT EXISTSasms.customerAccount ( trn_id INT NOT NULL AUTO' at line 1.
Am using mysql db
String createAccount = "DROP TABLE IF EXISTS `asms`.`customerAccount`"
+ "CREATE TABLE IF NOT EXISTS`asms`.`customerAccount` ( "
+ " `trn_id` INT NOT NULL AUTO_INCREMENT, "
+ " `trn_date` DATE NOT NULL, "
+ " `narration` VARCHAR(200) NULL, "
+ " `value_date` VARCHAR(50) NULL, "
+ " `debit` DOUBLE NULL, `credit` DOUBLE NULL,"
+ " `ledger_balance` DOUBLE NULL DEFAULT 0.0, "
+ " `credit_account_no` VARCHAR(50) NULL, "
+ " `customerAccountcol` VARCHAR(100)NULL, "
+ " `tra_ref_number` VARCHAR(50) NULL, "
+ " `trn_type` VARCHAR(10) NULL, "
+ " `action_type` VARCHAR(10) NULL, "
+ "`approve_insu_funds` VARCHAR(10) NULL,"
+ " PRIMARY KEY (`trn_id`), "
+ " UNIQUE INDEX `debit_UNIQUE` (`debit` ASC),"
+ " UNIQUE INDEX `credit_UNIQUE` (`credit` ASC),"
+ " UNIQUE INDEX `ledger_balance_UNIQUE` (`ledger_balance` ASC))ENGINE = InnoDB AUTO_INCREMENT = 00001";
This should be 2 queries, 1st one drops the table, 2nd one creates the new one
1st drop the table if exists
String dropTableQuery= "DROP TEMPORARY TABLE IF EXISTS `asms`.`customerAccount`";
//execute the query now
then do the create query
String createAccount ="CREATE TABLE IF NOT EXISTS`asms`.`customerAccount` ( "
+ " `trn_id` INT NOT NULL AUTO_INCREMENT, "
+ " `trn_date` DATE NOT NULL, "
+ " `narration` VARCHAR(200) NULL, "
+ " `value_date` VARCHAR(50) NULL, "
+ " `debit` DOUBLE NULL, `credit` DOUBLE NULL,"
+ " `ledger_balance` DOUBLE NULL DEFAULT 0.0, "
+ " `credit_account_no` VARCHAR(50) NULL, "
+ " `customerAccountcol` VARCHAR(100)NULL, "
+ " `tra_ref_number` VARCHAR(50) NULL, "
+ " `trn_type` VARCHAR(10) NULL, "
+ " `action_type` VARCHAR(10) NULL, "
+ "`approve_insu_funds` VARCHAR(10) NULL,"
+ " PRIMARY KEY (`trn_id`), "
+ " UNIQUE INDEX `debit_UNIQUE` (`debit` ASC),"
+ " UNIQUE INDEX `credit_UNIQUE` (`credit` ASC),"
+ " UNIQUE INDEX `ledger_balance_UNIQUE` (`ledger_balance` ASC))ENGINE = InnoDB AUTO_INCREMENT = 00001";
//and ofc. execute the query
I have written a query to insert values into a database in Android:
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + KEY_SITUATION_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY_NAME + " TEXT NOT NULL,"
+ KEY_LATTIUDE +" NOT NULL," + KEY_LONGITUDE + " NOT NULL," + " );");
However, when I execute it, an error is thrown. Can anyone point out the error in it?
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + KEY_SITUATION_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY_NAME + " TEXT NOT NULL,"
+ KEY_LATTIUDE +" INTEGER NOT NULL," + KEY_LONGITUDE + " INTEGER NOT NULL" + " );");
Provide a Datatype to KEY_LATITUDE and to KEY_LONGITUDE.
And also you have kept a (,) at last which was not needed...
the correct code is:
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + KEY_SITUATION_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY_NAME + " TEXT NOT NULL," + KEY_LATTIUDE +" NOT NULL," + KEY_LONGITUDE + " NOT NULL," + " )");
db.execSQL("CREATE TABLE " + TABLE_NAME + "( " + KEY_SITUATION_NAME
+ " TEXT NOT NULL, " + KEY_CATEGORY_NAME + " TEXT NOT NULL,"
+ KEY_LATTIUDE +" NOT NULL," + KEY_LONGITUDE + " NOT NULL);");