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.
Related
INSERT INTO trees (preview)
select galleries.preview
from galleries,trees
where trees.id=galleries.idTree;
I am trying to move a column from a table to another, I have set an empty column with the same data type as the original.
Where idTree is equal to id to the destination table (in the source table idTree is foreign key reference to id on trees that is the destination).
The "select" works and give me back the right set of values (at least ordered by id and all) but the insert into part, do nothings and the field on trees is still empty. what am I doing wrong ?
INSERT INTO.. will create a new row; but you are looking to update the existing rows in trees table which has id. Try with UPDATE query instead:
UPDATE trees
JOIN galleries ON galleries.idTree = trees.id
SET trees.preview = galleries.preview
I have two tables
Table 1 Schema:
Table1:: Product_Report Column:: PId, PReportName, PReportSize
And Second Table Schema:
Table2:: Product Column:: PId, PName, PSize, PCategory
In both table PId is PrimaryKey. I want to add PReportCategory as new column in Product_Report (Table1) in that value of PReportCategory is selected from PCategory of Product (Table2)
Something like:
INSERT into Product_Report (PReportCategory) VALUES Select PCategory from Product where (I guess here need help)
If I am right to path I am following then I guess in where condition I need help if not then please help me to construct query
Thank You
Q: "I want to add PReportCategory as new column in Product_Report"
ALTER TABLE Product_Report
ADD PReportCategory BIGINT DEFAULT NULL COMMENT 'ref Product.PCategory'
(Use an appropriate datataype in place of BIGINT, I added that as a placeholder datatype for the column.)
Q: "... value of PReportCategory is selected from PCategory of Product"
UPDATE Product_Report t
JOIN Product s
ON s.PId = t.PId
SET t.PReportCategory = s.PCategory
This would leave NULL values for PReportCategory in rows in Product_Report that don't have a matching row found in Product.
It's not at all clear what problem this is attempting to solve, i.e. why it's necessary to add this column and populate it. But this example of MySQL syntax demonstrates how we would 1) add a column to a table and 2) populate that column from values in rows in another table, matched based on the values of PId
I have been having some frustration attempting to add data values to this table students. I have all the other data values and have dropped and created the column student_id. However, when trying to add the data with this query:
insert into students(student_id) values('1'),('2'),('3'),('4'),('5');
The data does not insert correctly, as it creates new columns below the first 5 which contain data.
It must be because of my not null values, but I can't not have the not null identifier.
Is there a query command that allows me to change data within already existing value-filled columns? I have been unsuccessful in finding this so far.
Here are some images to explain the problem further.
The query I have made to add my values to the table:
The data was inserted but as it is underneath the columns I need to map with a foreign key, I cannot use the column as the top 5 values are still my not null default, which is required to let me create the foreign key
Looks like you already have your records initially created without the student_id field, you want to UPDATE the current records but you're actually INSERTING new records.
You're meant to update your students with update statements such as "UPDATE students SET student_id = X where condition = Y"
Then it looks like your student_id is your primary key which you should set to AUTO_INCREMENT value.
Regards
INSERT is the wrong command since you want to update existing rows. The problem here lies within the fact that the order of the rows is nondeterministic and I think you cannot update them in one statement. One solution would be as follows:
UPDATE students SET student_id = 1 WHERE first_name = 'Berry';
UPDATE students SET student_id = 2 WHERE first_name = 'Darren';
I hope you really do have only 5 columns to update :-)
Not been able to find something that matches what I am setting out to teach myself, so here goes:
I have added a column (called status) to an existing table (called fruit). All values in this new column are currently null. Other columns in this table are id (primary key int(11) ) and fruitname.
My question is this:
Is there a command I can use to populate this column in one go? Or do I need to update each row one by one?
Ideally I am looking for something that populates columns in the same way insert does to rows. Something where I can specify the table and column name and then list the values to fill down.
If you want the new column status to have same value for all records like Fresh then you can do it in one go like
update fruit set status = 'fresh'
Else, you have no other way than performing an UPDATE row by row and populate the status column value for each fruit record.
I'm trying to write a query to update a FK column in table B using the primary key column in table A. If there are duplicate entries in table A, I'd like to use the max id of the duplicate entry to insert into table B.
I have the first part of the query written but I'm unsure about the duplicate entry part.
Here's what I have so far...
UPDATE calliope_media.videos v
JOIN calliope_media.video_ingress_queue viq ON v.provider_unique_id = viq.provider_unique_id
SET v.video_ingress_id = viq.id;
This is how your query should look.
UPDATE B
SET B.the_column_ID = (SELECT MAX(A.some_ID)
FROM A
WHERE A.matching_value = B.matching_value)
This is the overall structure. I haven't adapted to your specific requirements, since I don't fully understand them. But this should get you back on track.