MySQL foreign key problem - mysql

I'm getting that error running on MySQL 5.5.8
Mysql2::Error: 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 'WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
EN' at line 6:
CREATE TRIGGER fk_brands_products_insert
BEFORE INSERT ON brands
FOR EACH ROW BEGIN
SELECT
RAISE(ABORT, "constraint violation: fk_brands_products")
WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
END;
What could be wrong?

I suspect the problem is there is no FROM clause in your select statement.

Are you sure you can raise errors inside a query like that? I can't find it anywhere. I think the proper way would be to select a COUNT or EXISTS and return the result of that INTO a variable. Then, after the query, raise an error if the result doesn't meet your expectations.
Something like this:
SELECT count(id) INTO IDCOUNT FROM products WHERE id = NEW.brand_id;
Wouldn't it be better by the way to just add a real constraint? Or do you use a storage type that doesn't support that?

Related

Simple SELECT SQL statement not working

SELECT user FROM members WHERE admin=1;
SELECT user FROM members WHERE id=1;
The first query does not work because there's an error in my syntax. The second works but they are virtually identical. Both 'admin' and 'id' are INT(11), but 'id' is the primary key and set to auto-increment. What's wrong here?
The error I receive is:
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 'admin=1' at line 1
When you use keywords or reserved words don't forget to add quotes or brackets.
SELECT [user] FROM members WHERE [admin]=1;
SELECT [user] FROM members WHERE id=1;
I apologize for the confusion everyone, my fault. I did this and it works.
SELECT user FROM members WHERE members.group=2;

MySQL syntax error regarding WHERE NOT EXISTS

I have a Chef recipe for creating Unix user IDs and deploying them across multiple nodes, to guarantee uniqueness and prevent devs from having to track them themselves. They merely name the application and it is granted a unique ID if an ID for that application doesn't already exist. If one does, it is simply returned to the script and user accounts are created on the webservers with the appropriate value.
I have a mysql database with a single table, called application_id_table which has two columns, id and application_name. id is autoincrementing and application name cannot be null (and must be unique).
Removing the Ruby from my script and making a couple of substitutions, my sql looks like this:
INSERT INTO application_id_table(application_name) VALUES('andy_test')
WHERE NOT EXISTS (select 1 from application_id_table WHERE
application_name = 'andy_test');
when run, I receive the syntax parsing error:
ERROR 1064 (42000): 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 'WHERE NOT EXISTS (select 1 from
application_id_table WHERE application_name = 'a'
I recall seeing that the values statement does not allow a where clause but I don't wish to use a select statement to populate the values as I'm populating them from variables supplied from within Ruby/Chef. Anyone have an idea how to accomplish this?
You want to use insert . . . select:
INSERT INTO application_id_table(application_name)
SELECT aname
FROM (SELECT 'andy_test' as aname) t
WHERE NOT EXISTS (select 1 from application_id_table ait WHERE ait.application_name = t.aname);
You should be able to plug your variable directly into the select statement, the same you would would with the VALUES statement.
Try this:
INSERT INTO application_id_table(application_name)
select 'andy_test'
WHERE NOT EXISTS (select 1 from application_id_table WHERE application_name = 'andy_test');

SQL syntax error on sql 5.6.17

IF NOT EXISTS ( SELECT name FROM person)
SELECT id FROM connection
I get:
1064 - 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 'IF NOT EXISTS ( SELECT name FROM person) SELECT id FROM connection' at line 1
What's wrong?
I also tried
IF NOT EXISTS(SELECT name FROM person)
BEGIN
SELECT id FROM connection
END
and get the same error.
That's not a correct way. Rather try like
SELECT id FROM connection
WHERE NOT EXISTS ( SELECT name FROM person)
IF can only be used in stored procedures. To do this in a regular query, do:
SELECT id FROM connection
WHERE NOT EXISTS (SELECT name FROM person)
However, I wonder if this is really what you want. This will return all connection IDs if the person is empty, and return nothing if the person table has any rows. It doesn't relate the rows in the two tables to each other -- you would use a JOIN or correlated subquery for that.

MySQL You can't specify target table for update in FROM clause

When I run this query
update Apply
set major='CSE'
where major='EE' and sID in (select sID from Student
where GPA >= all (select GPA from Student
where sID in (select sID from Apply
where major='EE')));
it returns an error: ERROR 1093 (HY000): You can't specify target table 'Apply' for update in FROM clause. But it succeeds in another MySQL.
if I add "as tmp" to the end of the code, it returns an error: ERROR 1064 (42000): 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 'as tmp' at line 6
Can somebody tell me the reason and how can I modify the code?
Thank you very much!
Problem: You're referencing the table Apply twice in two different contexts.
Solution: Give them different aliases, e.g. Apply as a1 and Apply as a2
(Maybe this also needs to be done for Student)

Modifing select query to delete query

The following select query works fine:
SELECT * FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
However, when I try to delete the rows retrieved here, it fails
DELETE FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
What is wrong here?
The error message is:
ERROR 1064 (42000): 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 'job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LI' at line 1
You need to specify you are deleting from the alias table, so use:
DELETE job FROM JBPM_JOB job WHERE job.ACTION_ IN (SELECT ID_ from JBPM_ACTION WHERE ACTIONEXPRESSION_ LIKE '%#{reminderAction.addAsyncProcessReminder%warning%');
i have tested the query in sql server. works fine but there is possible that the values you are deleting have some relationship with other table like PK and FK.
if they have then you have to delete the records from those tables too..........