MySQL Error Code 1215: “Cannot add foreign key constraint”

Why am I unable to create a constraint? The error message doesn’t help much. You just get the following line when you run this SQL statement:

ALTER TABLE user_class ADD FOREIGN KEY (user_id) REFERENCES user(id);

Error Code: 1215
Cannot add foreign key constraint

If you try running some of these SQL statements from mysql ( https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html#foreign-keys-adding ), still same error occurred without a reason. You may have to double check a few things within related tables and their fields.

1) The table or index the constraint refers to does not exist yet (usual when loading sql dumps).

2) The table or index in the constraint references misuses quotes.

3) The local key, foreign table or column in the constraint references have a typo.

4) The column the constraint refers to is not of the same type or width as the foreign column

5) The foreign object is not a KEY of any kind

6) The foreign key is a multi-column PK or UK, where the referenced column is not the leftmost one

7) Different charsets/collations among the two table/columns

8) Must be InnoDB

9) Using syntax shorthands to reference the foreign key

10) Using SET DEFAULT for a constraint action


Difference between Java Servlet and JSP

Servlet is html in java whereas JSP is java in html. Servlets run faster compared to JSP. … JSP is a webpage scripting language that can generate dynamic content while Servlets are Java programs that are already compiled which also creates dynamic web content. In MVC, jsp acts as a view and servlet acts as a controller.

Servlet example: Hello.java

response.setContentType("text/html");
    PrintWriter writer = response.getWriter();
    
    writer.println("<html>");
    writer.println("<head>");
    writer.println("<title>This is my .java file to .class file. Sample Application Servlet Page</title>");
    writer.println("</head>");
    writer.println("<body bgcolor=white>");
    
    writer.println("<table border=\"0\">");
    writer.println("<tr>");
    writer.println("<td>");
    writer.println("<img src=\"images/tomcat.gif\">");
    writer.println("</td>");
    writer.println("<td>");
    writer.println("<h1>Sample Application Servlet</h1>");
    writer.println("This is the output of a servlet that is part of");
    writer.println("the Hello, World application.");
    writer.println("</td>");
    writer.println("</tr>");
    writer.println("</table>");

JSP example: hello.jsp

<table border="0">
<tr>
<td align=center>
<img src="images/tomcat.gif">
</td>
<td>
<h1><%= new String("Sample Application JSP Page") %></h1>
This is the output of a JSP page that is part of the Hello, World
application.
</td>
</tr>
</table>

Convert .class to .java

cd to the folder that contains .class file, type the command below (without .class extension). The class will be de-compiled to java code

C:\Path-to-Class-File\classes\mypackage>javap -c Hello

You’ll see the code of .java file that look like this in the terminal.

Compiled from "Hello.java"
public final class mypackage.Hello extends javax.servlet.http.HttpServlet{
public mypackage.Hello();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method javax/servlet/http/HttpServlet."<init>":()V

   4:   return

public void doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.Http
ServletResponse)   throws java.io.IOException, javax.servlet.ServletException;
...

How to write an Object to a file

Java object can be written into a file for future reference. This process is called Serialization (translating data structures or object state into a format that can be store). In order to do that, we have to implement the Serializable interface, and use ObjectOutputStream to write object into a file.

FileOutputStream fout = new FileOutputStream("c:\\temp\\address.txt");
ObjectOutputStream oos = new ObjectOutputStream(fout);
oos.writeObject(address);

Adding JUnit and Hamcrest for Testing in Java

package test.string;

import java.lang.String;
import static org.junit.Assert.assertThat;
import org.junit.Test;

public class StringFormatTestCase {
    @Test
    public void testNumberFormattingWithLeadingZeros() {
        final String formatted = String.format("%04d", 1);
        assertThat(formatted, equalTo("0001"));
    }

    @Test
    public void testDoubleFormattingWithTwoDecimalPoints() {
        final String formatted = String.format("%.2f", 12.324234d);
        assertThat(formatted, equalTo("12.32"));
    }
}

Errors

Error:(3, 33) java: package org.junit does not exist
Error:(3, 2) java: static import only from classes and interfaces
Error:(5, 25) java: package org.junit does not exist
Error:(6, 12) java: package org.junit does not exist
Error:(12, 10) java: cannot find symbol
    symbol:   class Before
    location: class edu.kit.ipd.swt1.StringFormatTestCase 
    Error:(17, 11) java: cannot find symbol
[...]

In order to fix it we had to Open Module Settings for that project and manually add jar Dependencies junit-4.12.jar and hamcrest-core-1.3.jar which are contained in the IntelliJ installation lib directory (C://Program Files/JetBrains/lib/xxx.