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.
Related
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.
I have a table which contains id and supervisorId. Id is the PK and supervisorId is the foreign key (Self Join).
When I try to insert the data into the table, mysql throws unable to add or update a child row error. Because of relationship between two columns, I can't populate the supervisorId column without Id column.
Is there any way to insert the data in a single query?
You need to insert the rows in two steps:
Insert the rows without supervisors (i.e. supervisor_id = NULL.
Then update the rows with the correct supervisor, when you know their ids.
It is not clear how you are processing the data. If you are trying to move rows from one table to another, you should probably ask another question, with sample data, desired results, and a clear explanation of what you want to accomplish.
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 :-)
I added a new column to an already existing table, I just need to get data into this new column...I assume I can use a variant of the insert command, yet I do not know how to specify the column name and then how to order the values going into the new column.
Ok, after some conversation through the comments lets go to an answer.
I suppose your table is something like id, name, age, dateBirth, etc fields. But whoever create this table forget to add the gender for the registries. As you said that the new column is an sex enum('m', 'f') you will have to update every registry on this table one by one. Like this:
update matches set sex = 'm' where id = 1;
Pay attention that with this command I just updated the row on the table where the id=1 and Im assuming that id is your primary key. On the where caluse you have to put your primary key, otherwise you may update more then one column.
If your table has many registries there is a way that you can do it cuting down the heavy work (at least a little)
In order to update many rows at once you have to do an Update with a LIKE filter, you will set a filter that can identifie many womans at a time then many men at time as this:
update matches set sex = 'f' where name like '%Jheniffer%'
Since Jheniffer is a female name most likely you will update every registry which has part of the name as Jheniffer like 'Jheniffer Smith'. So repeat this process for the common names until the work is done. For all womens then repeat for the men.
Hope it help you to understand
you have to use update command, insert is for adding new rows.
update myTABLE set NewColumn = ?
Why INSERT here? You need to UPDATE data to column inserted.
Here is the list of steps
Alter the table then add column with constraint is NULLABLE.
Process the update the column added using UPDATE command.
If you want the column added is NOT nullable. Just re-alter the column and change it to NOT nullable.
You can use UPDATE command.
UPDATE my_table SET new_col='new_val'
if you need to set new column value just in few rows add WHERE clause
UPDATE my_table SET new_col='new_val' WHERE condition='true'
I want to know how to give unique id to 3 tables which have the same column name in mysql.
when inserting a new value the value should be compared with all three table column values and assign a new unique id to the inserted table. Is this possible in mysql.
Thanks
You'll need to create a temporary table that has all the values aggregated together so you can check.
What do you mean by "unique id"? A suitable hash function like SHA1() usually gives you something reasonably unique.