Issues with MySQL inserting columns below desired location - mysql

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 :-)

Related

insert into select with external key reference

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

How to sort a column based on another column to resolve can't add or update child row issue

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.

Adding a column of data

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.

inserting data into a new column of an already exsisting table

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'

Using MySQL, I'd like to update 1 tables id values from another and use ma

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.