Apologies if I am mistaken, but is there any way to create a table inside a column in MySql?
Brief: I have a table named test_table which contains a column named name test_column. Now I want to create a table inside test_column. Is this possible?
You would create a "child" table with an id that is referenced in the column of the main table. You wouldn't create a "table" in a column.
For example
Table 1
columm_pk int
column_fk int
table 2
column_pk (this is what goes in table 1)
other columns as needed.
then you just join the tables based on that fk id. You can have multiple fk column in the first table that link to different child tables. You can also look in to SET data types in MySql although I wouldn't recommend them.
btw, If your question is MySql specific then you shouldn't use the oracle tags.
No nested tables in MySql, but there is a SET datatype that you can use in a table
http://dev.mysql.com/doc/refman/5.0/en/set.html
That approach is not possible. What you are looking for is a second table that is linked using a field in the first table.
Example:
test_table:
ID | column1 | some more columns
test_table2:
table1_ID | column1| column2...
You can then access them using JOIN commands. For example:
SELECT *
FROM test_table t1
INNER JOIN test_table2 t2
ON t1.ID = t2.table1_ID
This way you can have multiple rows for each ID in table 1, which has the effect you are looking for.
This is not possible in MySQL.
Related
I have 2 sql separated tables in my database:
ds_users, containing: group_id
and
ds_users_data_members, containing: data_gender
I would like to set / Update the group_id to 6 for all data_gender equal to 2.
All this morning i tried to solve this issue , without success.
Please help. Thank you very much
I am assuming there must be a relation between those two tables. Without any relationship you cannot update record in one table by checking condition in another table.
let's say ds_users table has column user_id which is also exist in ds_users_data_members table.
so, you can write following query to update all records in ds_users for data_gender=2 in ds_users_data_members table
SQL SERVER EXAMPLE
UPDATE T
SET group_id=6
FROM ds_users T INNER JOIN ds_users_data_members T1 ON T.user_id=T1.user_id
WHERE T1.data_gender=2
MySQL EXAMPLE
UPDATE ds_users T INNER JOIN ds_users_data_members T1 ON T.`user_id`=T1.`user_id`
SET T.`group_id`=6
WHERE T1.`data_gender`=2;
You can replace the column name of user_id what you have given in your table.
I have a set of tables that I am conflating together. I want to be able to go back and access the 'raw' data later if I need to add more. The way I'm doing this by adding a reference column to the conflation table that will contain the table name of the table from which the data came from.
How can I access the names of my tables as I query from them?
EDIT: Details:
The table I'm creating looks like:
CREATE TABLE combined_things WITH OIDS AS
(SELECT
thing1.name
thing1.shape
FROM
public.thing1_source_table
UNION
SELECT
thing2.name
thing2.shape
FROM
public.thing2_source_table);
And I want to add the "source" field:
ALTER TABLE combined_things ADD COLUMN source_id character varying(100);
ALTER TABLE comnined_things SET COLUMN source_id = {table_name}
And I don't know how to pull the {table_name}
You could add them as a string constant when you create the table
CREATE TABLE combined_things WITH OIDS AS
(SELECT
thing1.name,
thing1.shape,
CAST('public.thing1_source_table' AS CHAR(100)) source_id
FROM
public.thing1_source_table
UNION
SELECT
thing2.name,
thing2.shape,
'public.thing2_source_table'
FROM
public.thing2_source_table);
Note that there is no way of casting the column to varchar
How to add or insert specific column from one table to other ? I tried writing like this
ALTER TABLE info_apie_zaideja
ADD SELECT info_apie_match.Rank AFTER 'Nick'
FROM info_apie_match;
or this
UPDATE info_apie_zaideja
ADD COLUMN SELECT info_apie_match.Rank AFTER 'Nick'
FROM info_apie_match;
but that did not work. Oh, and the table where I want to insert column is view table if that helps somehow. All answers will be appreciated.
You need to do this in two steps. First alter the table to add the new column:
ALTER TABLE info_apie_zaideja
ADD COLUMN Rank INT AFTER Nick;
Then fill it in by copying from corresponding rows in the other table:
UPDATE info_apie_zaideja AS z
JOIN info_apie_match AS m ON z.id = m.zaideja_id
SET z.Rank = m.Rank
I had to guess at the column that relates the two tables. Correct the ON clause to match your actual table relations.
Also, consider whether you really need the column in both tables. With this redundancy, you'll need to make sure that whenever you update one table, the other one is updated as well. Instead, you could just use a JOIN whenever you need the value from the other table.
Part 1:
In MySQL suppose I have Table A which has more columns than Table B. I want to transfer values from Table B to Table A where the id row in A matches the id Row in B and update the values in table A from the values in table B.
Part 2:
Table B is a superset of table A, so how does one insert ids and their corresponding values from table B into table A while also updating id's that are in table A.
Like FreshPrinceOfSO already mentioned in the comments, you won't get code for free here.
But here are at least the steps. Two possibilities. Either you split the work up in two statements, one update then one insert statement. Or you could work with
INSERT ... ON DUPLICATE KEY UPDATE ...
You would have to have an unique index on the table for this to work.
For the first solution mentioned you'd inner join the tables for the update first, that's trivial. Then for the insert you'd use a select with a left join and with is null checking for entries that are not already in the table.
Good luck...
I have a scenario that requires a value in a row of a table to be updated automatically whenever a row has been added or deleted in another table. I'm not sure how to do it.BTW I'm using phpmyadmin in order to manage my database. Thanks in advance.
pages Table
------------
page_no
no_of_choices
choices Table
-------------
page_no
choice_no
When I add a choice with choice number 1 and page_no, then the table page which has the row, page_no=1 should be updated with no_of_choices=no_of_choices+1
You can use triggers.
For example:
CREATE TRIGGER `test1`
AFTER INSERT ON `tbl1`
FOR EACH ROW SET NEW.upd_fld = new_value
Similarly could be done for delete.
You can also create triggers from phpMyAdmin
TABLE A: page_no, no_of_choices
TABLE B: page_no, choice_no...
With a relational database you very rarely want to have duplicate data. If something breaks at some point, you won't know which to trust - the rows in Table B, or the no_of_choices in Table A. A better solution is to do one of the following (depending on which table you are querying):
SELECT COUNT(no_of_choices) FROM B WHERE page_no = 1
or
SELECT A.*, COUNT(choice_no) AS choice_no FROM A LEFT JOIN B USING(page_no)
You get the same result, but now you have one record to go off of, so you won't have inconsistent data.