04-01 UPDATE

01

This is our table we will update

CREATE TABLE student(
    student_id INT AUTO_INCREMENT,
    name VARCHAR(20),
    major VARCHAR(20),
    PRIMARY KEY(student_id)
);
INSERT INTO student(name,major) VALUES('John','Biology');
INSERT INTO student(name,major) VALUES('Kate','Sociology');
INSERT INTO student(name,major) VALUES('Claire','Chemistry');
INSERT INTO student(name,major) VALUES('Jack','Biology');
INSERT INTO student(name,major) VALUES('Mike','Computer Science');
SELECT * FROM student;

02

We can set the values for all the rows this way,

update student
SET major = 'Bio';
SELECT * FROM student

This will change all the values in the major row to 'Bio'

03

Drop the table, recreate it, and fill the columns again

DROP TABLE student;
CREATE TABLE student(
    student_id INT AUTO_INCREMENT,
    name VARCHAR(20),
    major VARCHAR(20),
    PRIMARY KEY(student_id)
);
INSERT INTO student(name,major) VALUES('John','Biology');
INSERT INTO student(name,major) VALUES('Kate','Sociology');
INSERT INTO student(name,major) VALUES('Claire','Chemistry');
INSERT INTO student(name,major) VALUES('Jack','Biology');
INSERT INTO student(name,major) VALUES('Mike','Computer Science');
SELECT * FROM student;

04

Use the WHERE to change the values of rows of specific columns

UPDATE student
SET major = 'Bio'
WHERE major = 'Biology';
SELECT * FROM student; 

This will ony change the major to 'Bio' if the value was originally 'Biology'