Can´t INSERT values with Foreign Key in MySQL - mysql

I have a MySQL (5.1.42 on OsX) running.
I added a Foreign Key with this sql statement:
ALTER TABLE `Portal`.`Mitarbeiter_2_BlackBerry`
ADD CONSTRAINT `fk_Blackberry`
FOREIGN KEY (`id` )
REFERENCES `Portal`.`Blackberry` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
, ADD INDEX `fk_Blackberry` (`id` ASC)
But when i try to insert values in that table with this sql statement:
INSERT INTO Mitarbeiter_2_BlackBerry SET uebergabeAm = '2009-12-01 13:00:00', fk_Blackberry = (SELECT id FROM Blackberry WHERE id = '1')
I got the following Error:
Error Code: 1054
Unknown column 'fk_BlackBerry' in 'field list'
Anybody an idea what could be wrong?
Thanks for any hint :-)
Lars.

You need to put the value in the column id not the constraint fk_Blackerry. And if you know the value is 1, just insert 1- you don't need the subquery.

As per the Syntax Which you you can refer from here.
http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
fk_Blackerry is Symbol, not the column. Column on which foreign key constraint is id, So the updated query should be
INSERT INTO Mitarbeiter_2_BlackBerry SET uebergabeAm = '2009-12-01 13:00:00', id = (SELECT id FROM Blackberry WHERE id = '1')

Related

SQL INSERT if username doesn't exist

what is wrong with this query:
INSERT INTO `customers`(`username`, `password`)
VALUES (:username, :password)
WHERE NOT EXISTS (
SELECT `username` FROM `customers` WHERE `username`=:username
);
What's wrong with the query? It generates a syntax error.
If you want to prevent duplicates, then use a unique constraint on the table:
ALTER TABLE customers ADD CONSTRAINT unq_customers_username UNIQUE (username);
Then if you attempt to insert a duplicate value using
INSERT INTO `customers`(`username`, `password`)
VALUES (:username, :password);
You will get an error of a constraint violation.
Often in this case, you want to actually update the password in the existing row. You can do that using the MySQL extension ON DUPLICATE KEY UPDATE:
INSERT INTO `customers`(`username`, `password`)
VALUES (:username, :password)
ON DUPLICATE KEY UPDATE password = :password;

sql syntax: I want to limit colmn value into a specific list

I have a table named project.
In that table a column named project_statu.
So what I wanna do is limit that column into a specific list as (
'finished',
'in progress',
'not finished'
).
What I tried but didn't work:
update table project check project_statut in ('finished','not finished','in progress');
You have to create your table using Check Constraint
CREATE TABLE Project
(
P_Id number,
project_status varchar(50) NOT NULL,
CONSTRAINT chk_st CHECK (project_status='finished' or project_status= 'in progress' or project_status='not finished')
)
Update
For more help please visit W3schools
Hope it will help.
There are two simple ways in MySQL. The first is to create a ProjectStatuses table and a foreign key constraint:
create table ProjectStatuses (ProjectStatus varchar(255));
insert into ProjectStatuses(ProjectStatus)
values ('finished'), ('in progress'), ('not finished');
Then use a foreign key constraint:
alter table project
add constraint fk_project_projectstatuses
foreign key project_status references ProjectStatuses(ProjectStatus);
Another method is MySQL is to use the enum type. You can read about that here.

INSERT INTO... SELECT in same table generates foreign key error

I am having trouble with the following insert query.
INSERT INTO CM_LABEL_CALENDAR (
label_id,
label_name,
order_seq,
meal_id,
hyperlink
)
SELECT
label_id,
label_name,
order_seq,
(meal_id + 315),
hyperlink
FROM
CM_LABEL_CALENDAR
WHERE
(meal_id BETWEEN '1466' AND '1521');
When I try to execute it I get the following error:
Lookup Error - MySQL Database Error: Cannot add or update a child row: a foreign key constraint fails (TEST_PBMS.CM_LABEL_CALENDAR, CONSTRAINT CM_LABEL_CALENDAR_ibfk_1 FOREIGN KEY (meal_id) REFERENCES CM_MEAL_CALENDAR (meal_id))
I've tried looking for an answer but couldn't find one.
There is a foreign key constraint between CM_LABEL_CALENDAR(meal_id) and CM_MEAL_CALENDAR(meal_id)
You are getting this error because you are trying to insert values in the meal_id column that do not exist in the CM_MEAL_CALENDAR table.

Not able to insert data in a table having foreign key using mysql

i have made 2 tables
2nd table consists of foreign key which references primary key of first table
2nd table consists of 3 fields id(primary key),order no,fid
i am using the following command
insert into table2 (order no,fid)
values(1,(select id from table1 where name='abc');
error in sql syntaxenter code here
ORDER is a reserved word. You must quote it using backticks: `order no`.
Also, you should probably use MySQL's INSERT ... SELECT syntax:
INSERT INTO table2 (`order no`, fld)
SELECT 1,id FROM table1 WHERE name = 'abc'
you lack extra closing parenthesis
insert into table2 (`order no`,fid)
values(1,(select id from table1 where name='abc' LIMIT 1));
insert into `table2` (`order no`,`fid`) values(1,(select id from table1 where name='abc');
Edited...
could be the problem line:
he error is Cannot add or update a child row: a foreign key constraint fails (abc/demo1, CONSTRAINT fid FOREIGN KEY (id) REFERENCES demo (id))
it shud be (abc/demo1,CONSTRAINT fid FOREIGN KEY (fid) REFERENCES demo (id))

MySQL foreign key problem rails

I'm trying to add foreign keys to my table using the following code:
constraint_name = "fk_#{from_table}_#{to_table}"
execute %{
CREATE TRIGGER #{constraint_name}_insert
BEFORE INSERT ON #{from_table}
FOR EACH ROW BEGIN
SELECT
RAISE(ABORT, "constraint violation: #{constraint_name}")
WHERE
(SELECT id FROM #{to_table} WHERE
id = NEW.#{from_column}) IS NULL
END
}
I'm getting the following error:
Mysql2::Error: Not allowed to return a result set from a trigger:
CREATE TRIGGER fk_brands_products_insert
BEFORE INSERT ON brands
FOR EACH ROW BEGIN
SELECT
RAISE(ABORT, "constraint violation: fk_brands_products")
FROM brands
WHERE
(SELECT id FROM products WHERE id = NEW.brand_id) IS NULL;
END;
What is wrong with my SQL script?
You can add a MySQL Foreign Key constraint like this:
ALTER TABLE #{from_table}
ADD CONSTRAINT #{constraint_name} FOREIGN KEY
(#{from_column}) REFERENCES #{to_table}(id)
This will constrain from_column on from_table to values in id in #{to_table}.
P.S: If you don't want to name the CONSTRAINT you can do this:
ALTER TABLE #{from_table}
ADD FOREIGN KEY
(#{from_column}) REFERENCES #{to_table}(id)
As the error states, a trigger is not allowed to return a result set. Even though you are raising a error, MySql doesn't like the SELECT statement. Try using a IF:
IF EXISTS(SELECT id FROM #{to_table} WHERE
id = NEW.#{from_column}) = 0 THEN
RAISE(ABORT, "constraint violation: #{constraint_name}");
END IF;