delete rows based on value of rows that reference them - mysql

I want to do something along the lines of this:
delete c.* from Club c
where c.id in(select club_id from ClubAttrValues where value='E5');
but when I try this I get the error
Error Code: 1451. Cannot delete or update a parent row: a foreign key constraint fails (`foroige`.`clubattrvalues`, CONSTRAINT `FKAE90CBE9EE3B7895` FOREIGN KEY (`club_id`) REFERENCES `Club` (`id`))
Is there any way to delete everything to do with the every club in Club where the value in ClubAttrValues is 'E5'. There are multiple rows in ClubAttrValues with the same club_id, which would all ideally be deleted, and club_id references id in Club.
I have tried setting ON DELETE CASCADE in MySQL workbench but even this seems to make no difference.
The Club table looks like this:
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| created | datetime | YES | | NULL | |
| deleted | tinyint(1) | NO | | NULL | |
| modified | datetime | YES | | NULL | |
| address1 | varchar(255) | NO | | NULL | |
| address2 | varchar(255) | YES | | NULL | |
| country | varchar(255) | YES | | NULL | |
| county | varchar(255) | NO | | NULL | |
| email | varchar(255) | YES | | NULL | |
| fax | varchar(255) | YES | | NULL | |
| mobile | varchar(255) | YES | | NULL | |
| name | varchar(255) | NO | | NULL | |
| postcode | varchar(255) | YES | | NULL | |
| telephone | varchar(255) | YES | | NULL | |
| town | varchar(255) | YES | | NULL | |
| type | varchar(255) | NO | | NULL | |
| parent_id | bigint(20) | YES | MUL | NULL | |
| lft | bigint(20) | NO | | NULL | |
| rgt | bigint(20) | NO | | NULL | |
+-----------+--------------+------+-----+---------+----------------+
and my ClubAttrValues table:
+---------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
| value | text | YES | | NULL | |
| club_id | bigint(20) | YES | MUL | NULL | |
+---------+--------------+------+-----+---------+----------------+

Before deleting try:
SET FOREIGN_KEY_CHECKS=0
Set it against to 1 when you are done.

It works when you set on delete cascade for the foreign key column and then
delete c
from Club c
inner join ClubAttrValues a on a.club_id = c.id
where a.value = 'E5'
See this SQLFiddle demo

Related

Can't add foreign key with alter table query

I'm trying to add a foreign key to my table (oc_booking) but when I do the alter table query it returns "ERROR 1215 (HY000): Cannot add foreign key constraint" and I don't know why.
I tried using SQL (terminal) and HeidiSQL (a tool similar to MySQL Workbench).
This is the reference table (oc_user):
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| user_id | int(11) | NO | PRI | NULL | auto_increment |
| user_group_id | int(11) | NO | | NULL | |
| username | varchar(20) | NO | | NULL | |
| password | varchar(40) | NO | | NULL | |
| salt | varchar(9) | NO | | NULL | |
| firstname | varchar(32) | NO | | NULL | |
| lastname | varchar(32) | NO | | NULL | |
| email | varchar(96) | NO | | NULL | |
| image | varchar(255) | NO | | NULL | |
| code | varchar(40) | NO | | NULL | |
| ip | varchar(40) | NO | | NULL | |
| status | tinyint(1) | NO | | NULL | |
| date_added | datetime | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
This is the table where I want to add a foreign key constraint (oc_booking):
+----------------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-----------+------+-----+-------------------+-----------------------------+
| appointment_id | int(11) | NO | PRI | NULL | auto_increment |
| id_supplier | int(11) | NO | MUL | NULL | |
| id_user | int(11) | NO | | NULL | |
| date | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+----------------+-----------+------+-----+-------------------+-----------------------------+
And this is my query:
ALTER TABLE oc_booking ADD FOREIGN KEY (id_user) REFERENCES oc_user(`user_id`);

Need a SQL command for these tables

I have the following 2 tables (student and attendance):
mysql> describe student;
+----------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+--------------+------+-----+---------+----------------+
| student_id | int(11) | NO | PRI | NULL | auto_increment |
| student_email | varchar(255) | YES | | NULL | |
| student_phone_number | varchar(255) | YES | | NULL | |
| parent_first_name | varchar(255) | NO | | NULL | |
| parent_last_name | varchar(255) | NO | | NULL | |
| parent_email | varchar(255) | NO | | NULL | |
| parent_phone_number | varchar(255) | NO | | NULL | |
| first_name | varchar(255) | NO | | NULL | |
| last_name | varchar(255) | NO | | NULL | |
| days_absent | int(11) | YES | | NULL | |
| days_tardy | int(11) | YES | | NULL | |
| class_id | int(11) | NO | MUL | NULL | |
+----------------------+--------------+------+-----+---------+----------------+
mysql> describe attendance;
+-----------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------+------+-----+---------+-------+
| student_id | int(11) | NO | PRI | NULL | |
| class_id | int(11) | NO | PRI | NULL | |
| attendance_date | date | NO | PRI | NULL | |
| absent | tinyint(1) | YES | | NULL | |
| tardy | tinyint(1) | YES | | NULL | |
| note | text | YES | | NULL | |
+-----------------+------------+------+-----+---------+-------+
I want to check the attendance and email of all the children who are absent or tardy for the day. Is there a SQL statement I can use that can, for example, select the students from attendance with an absent/tardy value = 1, then using that student_id specified in the attendance table, pull all the student info from the student table where attendance.student_id = student.student_id?
just try exists
select student_email,student_phone_number from student std
where exists(
select 1 from attendance atd where std.student_id = atd.student_id
and (atd.absent =1 or atd.tardy =1)
)
just make sure you select all the info from student table and exists all the filter in attendance talbe. the condition is student_id.
Also if you need info from attendance talbe, then use join.

Shared Primary Key for multiple tables

So I have 3 tables:
Pokemon:
+---------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+---------+-------+
| National_ID | int(11) | NO | PRI | NULL | |
| Picture | longblob | YES | | NULL | |
| Name | varchar(15) | NO | | NULL | |
| Generation_ID | int(11) | NO | | NULL | |
| Type1 | varchar(8) | NO | | NULL | |
| Type2 | varchar(8) | YES | | NULL | |
+---------------+-------------+------+-----+---------+-------+
Stats:
+-----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+-------+
| National_ID | int(11) | NO | PRI | NULL | |
| Health_Points | int(11) | NO | | NULL | |
| Attack | int(11) | NO | | NULL | |
| Defense | int(11) | NO | | NULL | |
| Special_Attack | int(11) | NO | | NULL | |
| Special_Defense | int(11) | NO | | NULL | |
| Speed | int(11) | NO | | NULL | |
| Ability1 | varchar(20) | NO | | NULL | |
| Ability2 | varchar(20) | YES | | NULL | |
+-----------------+-------------+------+-----+---------+-------+
Misc:
+--------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+-------+
| National_ID | int(11) | NO | PRI | NULL | |
| Evolves_From | varchar(15) | YES | | NULL | |
| Species | varchar(20) | NO | | NULL | |
| Height_inch | int(11) | NO | | NULL | |
| Weight_lbs | int(11) | NO | | NULL | |
| Capture_Rate | int(11) | NO | | NULL | |
+--------------+-------------+------+-----+---------+-------+
My problem is that the National_ID in the Stats table points to the National_ID in the Pokemon table, but I cannot do the same for the Misc table.
Whenever I try to add a foreign key in Misc to point to the Pokemon table, I get a duplication error. Help please! Thank you
From the error message, it looks like you already have a foreign key constraint with name 'FK_National_ID'. Try changing it to 'FK_Pokemon_National_ID' or something and it should work.
Here is the SQL Fiddle.
Every FOREIGN KEY declaration that a table & list of column names REFERENCES a table & list of column names needs a unique name. (If you don't give one explicitly then the DBMS will make one up.)

how to add a foreign key using an existing column in mysql

I have following table called transactions and I want to add foreign key using column account_id. But when I execute the query (see below) 0 rows affected. I am not getting what is wrong with the query.
I have two tables called transactions and accounts. Accounts table has an id as primary key and an account has_many transactions.
transactions table
+---------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| account_number | int(11) | YES | | NULL | |
| m_number | varchar(255) | YES | | NULL | |
| registration_number | varchar(255) | YES | | NULL | |
| page_number | varchar(255) | YES | | NULL | |
| entry_date | datetime | YES | | NULL | |
| narration | varchar(255) | YES | | NULL | |
| voucher_number | varchar(255) | YES | | NULL | |
| debit_credit | float | YES | | NULL | |
| profit | float | YES | | NULL | |
| account_code | int(11) | YES | | NULL | |
| balance | float | YES | | NULL | |
| branch | varchar(255) | YES | | NULL | |
| account_id | int(11) | YES | MUL | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+---------------------+--------------+------+-----+---------+----------------+
16 rows in set (0,01 sec)
mysql> ALTER TABLE transactions ADD FOREIGN KEY (account_id) REFERENCES accounts(id);
Query OK, 0 rows affected (0,03 sec)
Records: 0 Duplicates: 0 Warnings: 0
accounts table
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| account_number | int(11) | YES | | NULL | |
| account_code | int(11) | YES | | NULL | |
| branch | varchar(255) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
| father_name | varchar(255) | YES | | NULL | |
| nic_number | varchar(255) | YES | | NULL | |
| mohalla | varchar(255) | YES | | NULL | |
| village | varchar(255) | YES | | NULL | |
| nominee | varchar(255) | YES | | NULL | |
| relationship | varchar(255) | YES | | NULL | |
| opening_balance | float | YES | | NULL | |
| opening_date | datetime | YES | | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
After running the query shouldn't the column say F_K or something like PRI ?
Any help would be great. Thanks!

sql joining three tables using inner joins

in my database I have 3 tables named items, manufacturers and items_manufacturers. manufacturers has a HAS:MANY relation with items_manufacturers
My items table
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| code | varchar(25) | NO | MUL | NULL | |
| item_category | varchar(100) | NO | | NULL | |
| item_desc | varchar(500) | NO | | NULL | |
| reorder_point | int(11) | NO | | NULL | |
| unit | varchar(45) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
My manufacturers table
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| code | varchar(25) | NO | | NULL | |
| name | varchar(250) | NO | | NULL | |
| address | varchar(750) | NO | | NULL | |
| contact_no | varchar(50) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
My items_manufacturers table
+-----------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------------+------+-----+---------+----------------+
| id | bigint(20) | NO | PRI | NULL | auto_increment |
| item_id | bigint(20) | NO | MUL | NULL | |
| manufacturer_id | bigint(20) | NO | MUL | NULL | |
| unit_cost | decimal(20,2) | NO | | NULL | |
| vendor_id | bigint(20) | NO | | NULL | |
+-----------------+---------------+------+-----+---------+----------------+
In my result table I want items_id, items_desc, name of manufacturer from manufacturers table and manufacturer_id. The relation I have is
items.id=items_manufacturers.item_id and
manufacturers.id=items_manufacturers.manufacturer_id.
I tried using inner joins for three tables but not working.
The query I tried
select
items_manufacturers.id,
items.item_desc,
item_manufacturers.manufacturer_id,
manufacturer.name
from items_manufacturers
INNER JOIN items ON items_manufacturers.item_id=items.id
INNER JOIN manufacturers ON items_manufacturers.manufacturer_id=manufacturers.id
Anybody kindly help me with this, I am stuck up from a long time
I used this following code and got the result you were trying to get. This code may solve your problem:
select a.name,b.manufacturer_id,c.id,c.item_desc
from manufacturers as a
inner join
item_manufacturers as b
on b.manufacturer_id=a.id
inner join item as c
on c.id=b.item_id