In this video we will learn how to delete rows from a table, how to update rows, and how to alter the structure of a table.
To follow along, you will need to download and install 2 sql files:
DELETE FROM actors WHERE movie = 'Styx'
DELETE FROM actors WHERE firstname = 'Naomi' AND
lastname = 'Scott' AND movie = 'The Martian';
Be careful and be precise.
UPDATE city SET population = 558545 WHERE name = 'Albuquerque';
UPDATE city SET population = population + 10000 where name = ‘Springfield’ AND district = ‘Illinois’;
ALTER TABLE <table>
DROP COLUMN <columnName>
ADD COLUMN <columnName> <type> <attributes>
ADD PRIMARY KEY (<columnNames>)
RENAME COLUMN <oldColumnName> TO <newColumnName>
ALTER COLUMN <columnName>
{SET, DROP} NOT NULL
SET DATA TYPE <type>
For this example we are using the file:
CREATE DATABASE demo;
\c demo
CREATE TABLE actors (
firstname text,
lastname text,
born text,
age int
);
INSERT INTO actors (firstname, lastname, born, age) VALUES
('Valorie', 'Curry', '1986-02-12', 33),
('Bryan', 'Dechart', '1987-03-17', 32),
('Jesse', 'Williams', '1981-08-05', 37),
('Clancy', 'Brown', '1959-01-05', 60),
('Minka', 'Kelly', '1980-06-24', 39),
('Judi', 'Beecher', '1987-11-05', 32);
ALTER TABLE actors
DROP COLUMN age;
ALTER TABLE actors
ADD COLUMN hometown text;
ALTER TABLE actors
ADD COLUMN id SERIAL,
ADD PRIMARY KEY (id);
ALTER TABLE acators
RENAME COLUMN id TO actor_id;
ALTER TABLE actors
ALTER COLUMN firstname SET NOT NULL,
ALTER COLUMN firstname SET NOT NULL;
ALTER TABLE actors
ALTER COLUMN firstname DROP NOT NULL,
ALTER TABLE actors
ALTER COLUMN firstname
SET DATA TYPE varchar(20);