Adding column from one table to the other table in mysql - mysql

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.

Related

SQL Insert with Inner Join trying to insert into wrong column

I have an existing table of products, and I'm trying to insert new attributes from other tables into my main product table.
Here's my query:
INSERT INTO company_attributes (amp)
SELECT company_attr_amperage.amp
FROM company_attr_amperage
INNER JOIN company_attributes
ON company_attr_amperage.product_id = company_attributes.product_id;
The error that I get is: Field 'product_id' doesn't have a default value.
I'm not trying to insert into the product_id column, I'm trying to insert into the amp column (as specified on row 1 of the query)
The amp column exists on the company_attributes table, but right now, every value is NULL
Thanks
You may just want to update the value in existing rows. If so:
UPDATE company_attributes ca JOIN
company_attr_amperage caa
ON caa.product_id = ca.product_id
SET ca.amp = caa.amp;
INSERT's job is to put new rows into a table, in your case company_attributes. Those new rows must be valid. Your table definition says that there's no default value for a column called product_id in that table. So, MySQL doesn't know what to put into that column when you don't provide it with your INSERT operation.
So, when you insert a row you must provide a value for that column.
There's no way to put a value into a column without creating a row for it, or UPDATEing an existing row.

Merging duplicate rows that have one different value

This is an correction of this question now I properly understand what I need to do.
I have a table with films and the dates they were shown on along with other info in other columns, in MySQL.
So relevant columns are...
FilmID
FilmName
DateShown
The dates are stored as Unix timestamps.
I currently have multiple instances of films that were shown on different dates yet all other information is the same.
I need to copy the dates of the duplicate films into a new table matching them up to the film ID. Then I need to remove the duplicate film rows from the original table.
So I have created a new table, Film_Dates with the columns
FilmDateID
FilmID
Date
Can anyone help with the actual sql to do this.
Thank you.
to start with:
insert into filmdateid (filmid, `date`)
select filmid, dateshown
from films
and that should populate your new table.
alter ignore table films
add unique (filmid)
This will enforce uniqueness for filmid, and drop all duplicates, keeping just the one row. If this fails with a 'duplicate entry' error, you will need to run this command, and then try the alter again.
set session old_alter_table=1
As it seems mysql is moving away from being able to do it this way.
Lastly, you need to get rid of your dateshown column.
alter table films
drop column dateshown
Please make sure you have a backup before you attempt any of this. Always best to be safe.
since filmid is not duplicated, only filmname, there are some extra steps
first, create the filmdates table:
create table filmdates as
select filmname, dateshown
from films;
Then add a filmid column:
alter table filmdates add column filmid integer;
And a unique index on (filmname, dateshown)
alter ignore table filmdates add unique(filmname, dateshown);
Then we add a unique index on films(filmname) - since its the only value that really gets duplicated.
alter ignore table films add unique(filmname);
Now that we're setup, we populate the filmid column in the new table, with maching values from the old.
update films f
inner join filmdates fd
on f.filmname = fd.filmname
set fd.filmid = f.filmid;
Now we just need to cleanup, and get rid of the redundant columns (films.dateshown and filmdates.filmname).
alter table filmdates drop column filmname;
alter table films drop column dateshown;
demo here

Map Values from Column in Table A to Corresponding Column in Table B

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

Automatic Update for database rows based on changes in another table

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.

create a table inside a column in MYSQL

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.