Postgre SQL
Crear tabla
CREATE TABLE IF NOT EXISTS db_pokemon(
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
type VARCHAR(50),
evolutionLevel INTEGER
);
Insertar un registro
INSERT INTO db_pokemon
(id, name, type, evolutionLevel)
VALUES
(1, 'Bulbasur', 'plant', 16);
Insertar varios registros
INSERT INTO db_pokemon
(id, name, type, evolutionLevel)
VALUES
(2, 'Ivysaur', 'plant', 32),
(3, 'Venusaur', 'plant', NULL);
Actualizar un registro
UPDATE db_pokemon
SET type = 'Grass'
WHERE id IN (1, 2, 3);
Actualizar varios registros
UPDATE db_pokemon SET hp = 45, attack = 49, defense = 49, special_attack = 65, special_defense = 65 WHERE id = 1;
UPDATE db_pokemon SET hp = 60, attack = 62, defense = 63, special_attack = 80, special_defense = 80 WHERE id = 2;
UPDATE db_pokemon
SET
hp = CASE
WHEN id = 1 THEN 45
WHEN id = 2 THEN 60
END,
attack = CASE
WHEN id = 1 THEN 49
WHEN id = 2 THEN 62
END,
defense = CASE
WHEN id = 1 THEN 49
WHEN id = 2 THEN 63
END,
special_attack = CASE
WHEN id = 1 THEN 65
WHEN id = 2 THEN 80
END,
special_defense = CASE
WHEN id = 1 THEN 65
WHEN id = 2 THEN 80
END
WHERE id IN (1, 2);
Añadir nuevas columnas
ALTER TABLE db_pokemon
ADD COLUMN hp INTEGER,
ADD COLUMN attack INTEGER,
ADD COLUMN defense INTEGER,
ADD COLUMN special_attack INTEGER,
ADD COLUMN special_defense INTEGER;
Modificar columna
ALTER TABLE db_pokemon RENAME COLUMN evolutionLevel TO evolution_level;
Última actualización