Error Code: 1822. Failed to add the foreign key constaint. Missing index for constraint

create_user INT UNSIGNED ZEROFILL cannot reference id INT, because these count as different data types for purposes of foreign key reference. Make them the same data type. The only data type difference that is permitted between columns in a foreign key relationship is length of a varchar. For example, VARCHAR(10) can reference VARCHAR(20) or vice-versa. … Read more

MySql SELECT union for different columns? [duplicate]

If you have different fields that have different meaning too, you can’t and shouldn’t return them in the same position. You can however ‘fill in the blanks’ by adding null to your fields, like this: select id, name, date, null as userid, ‘A’ as recordtype from table1 union all select id, name, null /*as date*/, … Read more

MySQL – Select from a list of numbers those without a counterpart in the id field of a table

This is a problem that is pretty common: generating a relation on the fly without creating a table. SQL solutions for this problem are pretty awkward. One example using a derived table: SELECT n.id FROM (SELECT 2 AS id UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7) AS … Read more