Shared Primary Key for multiple tables - mysql

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.)

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`);

MySQL: Trying to return NULL values in a ER model, but not having any luck

I have created a realistic table set using the ER model for MySQL. I am attempting to do joins, but seem to either be missing something, or not doing them correctly in order to produce NULL values. Below are my created tables:
+----------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| FilmNumber | char(3) | NO | PRI | NULL | |
| FilmName | varchar(60) | YES | | NULL | |
| OpeningDate | varchar(20) | YES | | NULL | |
| TopBilledActor | char(60) | YES | | NULL | |
| Genre | varchar(20) | YES | | NULL | |
+----------------+-------------+------+-----+---------+-------+
+------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-------------+------+-----+---------+-------+
| CriticSiteNumber | char(3) | NO | PRI | NULL | |
| CriticSiteName | varchar(30) | YES | | NULL | |
| CriticName | char(50) | YES | | NULL | |
+------------------+-------------+------+-----+---------+-------+
+------------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+---------+------+-----+---------+-------+
| FilmNumber | char(3) | YES | MUL | NULL | |
| CriticSiteNumber | char(3) | YES | MUL | NULL | |
| OverallScore | int(5) | YES | | NULL | |
+------------------+---------+------+-----+---------+-------+
3 rows in set (0.00 sec)
My question is, what type of join query would I need in order to produce NULL values, or does something in my table need to be modified because each of the queries I attempt do not return those values.

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!

delete rows based on value of rows that reference them

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

Include null values in SQL query

I have the following four tables in my database:
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| complex_id | int(11) | NO | PRI | NULL | auto_increment |
| complex_name | varchar(45) | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| machine_id | int(11) | NO | PRI | NULL | auto_increment |
| complex_id | int(11) | NO | MUL | NULL | |
| machine_name | varchar(45) | NO | | NULL | |
+--------------+-------------+------+-----+---------+----------------+
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| devices_id | int(11) | NO | PRI | NULL | auto_increment |
| machine_id | int(11) | NO | MUL | NULL | |
| description | varchar(255) | NO | | NULL | |
| location | varchar(255) | YES | | NULL | |
| verification | varchar(255) | YES | | NULL | |
| rack_num | varchar(8) | YES | | NULL | |
| section_num | varchar(8) | YES | | NULL | |
| color_or_number | varchar(16) | YES | | NULL | |
| normal_position | varchar(16) | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
+------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+-------------+------+-----+---------+----------------+
| pnp_id | int(11) | NO | PRI | NULL | auto_increment |
| devices_id | int(11) | NO | MUL | NULL | |
| pnp_num | varchar(45) | NO | | NULL | |
+------------+-------------+------+-----+---------+----------------+
I am trying to get results formatted as follows (NULL values appear as blanks):
+------------+-------------+-----------------------+
| devices_id | description | pnpnum |
+------------+-------------+-----------------------+
| 1 | ex | 1234 |
| 2 | ex2 | 2345 |
| 3 | ex3 | |
| 4 | ex4 | 3456, 4567, 5678, 6879|
+------------+-------------+-----------------------+
Using the following SQL query,
SELECT *, GROUP_CONCAT(pnp.pnp_num separator ', ') pnpnum
FROM devices
JOIN pnp ON devices.devices_id = pnp.devices_id
WHERE devices.machine_id = 1
GROUP BY devices.devices_ID
ORDER BY devices.description;
my results are relatively close, however, I am unable to include a device if it has a null pnpnum.
+------------+-------------+-----------------------+
| devices_id | description | pnpnum |
+------------+-------------+-----------------------+
| 1 | ex | 1234 |
| 2 | ex2 | 2345 |
| 4 | ex4 | 3456, 4567, 5678, 6879|
+------------+-------------+-----------------------+
What is it that I am missing from my SQL statement that will allow me to include null values?
You need to use LEFT JOIN because even if there isn't a match, it will return all the results from the left table leaving the fields from the right table null.