I have a table gm_foldernames and it has only one column.names type varchar(255). That column is a foreign key to another table in the bd, table1. I want to turn that column of names into a row maintaining the foreign key structure.
i am using group_concat() to put the cells in column table into a row seperated by comma. however, when i do that i lose my foreign key. I wanna keep the foreign key structure
here is my code;
SELECT GROUP_CONCAT(names) As names FROM gm_foldernames;
this is my current table. names is foreign key referencing another table
*names
ABRUZZO
ANZA
AQABA
...
...
...
I would like maintain the foreign key structure and make the column--> rows
ABRUZZO | ANZA | AQABA | ...
with the new column names being foreign key to the other table in the db
Related
I have table A that references table B with a foreign key (int). I want all entries in table A to reference table B's name field (varchar) instead. How can I make this change across all entries in table A?
Whenever I bring up the list of column names and datatypes, changing the foreign key to a varchar in the Columns tab gives me an error, and changing the foreign key to reference B's name instead of B's id gives me an error:
Create foreign key: Selected column Name must be indexed and be of a compatible type for a Foreign Key to be created
i have two tables
intent_sample(id,sample_data,intent_id,entity_id)
entities(id,entity_name,entity_id)
Since a sample_data can have any no of entities in it, I want to store multiple values in a single column and also want to link entity_id to entities table as it foreign key.
How can we do it.
I thought of using JSON data type in mysql for entity_id but is it possible to make a column having json data type as foreign key to another table
Use a composite key
FOREIGN KEY (`id`,`entity_id`)
REFERENCES entities(`intent_id`,`entity_id`)
This will allow a relation between the foreign key in one table to the id's in another table.
I don't have all of the details so you can modify it to work with your tables.
I have this kind problem regarding sql:
Table 1: ID_1 (Primary Key) | NAME | LASTNAME | EMAIL...
Table 2: ID_2 (Primary Key) | NAME2 | LASTNAME2 | EMAIL...
Normally using constraints it is possible to make as column in TABLE 2
(NAME 2 for example) as a foreign key for NAME the first TABLE 1.
The general relation would be one-many. That's one value from column NAME in TABLE 1 used many times in column NAME2 in Table 2.
Now what I want to achieve is that making the rule one-to-one, so I cannot have duplicated values of NAME from table TABLE 1 in TABLE 2.
Does it require triggers or it can be set by constraint when creating the tables?
Steps to achieve this:
Create foreign key constraint
Create unique constraint
This can all be done while creating the table.
CREATE TABLE table2 (
...,
name2 varchar(255),
FOREIGN KEY (name2) REFERENCES table1(name),
UNIQUE (name2)
);
If you already have the table, use ALTER TABLE instead.
When trying to insert a row with value of name2 that is already in a table, an error will be raised.
Yes it can be set by constraint. You can have foreign keys and unique keys on the same field. The unique constraint will guarantee there is only one record with one reference to the primary table (from that foreign table)
I have two tables and i have made foreign key relationship between them but i have set the foregin key nullable in the table but when i insert with foreign key column null, it gives me error:
Cannot add or update child row Foreign key contrain fails:
Here is the table screen in which i made foreign key:
I have made cliq_dependent_id column nullable but it has relation org_emp_dependents table id column
you must be inserting a value in the table that does not have reference key in the parent table.
For Example,
you are inserting 5 in the foreign key column, but if you check in the parent table rows, there will be no row with the id 5.
The second chance can be that you are inserting 0 instead of null.
Check these two things, i hope so it will get you out of the issue.
I have a column that's a foreign key. Adding rows to the column is fine as long as it the row I'm adding has data that is present in the parent table. Although, some rows don't have an entry that belongs in the parent table. I'd like to keep the column with a foreign key but even if it doesn't have a parent, the column should still store it.
Are there any ways to do this within MySQL?
Regards
A foreign key constraint enforces that the value exists in the referenced table. If you try to insert a value that doesn't exist in the referenced table then it will fail.
You have two options:
Store NULL instead of the ID that doesn't exist.
Don't use a foreign key constraint.