UPDATE
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;
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'
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;
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'