-
Notifications
You must be signed in to change notification settings - Fork 0
/
68languagetable-230714-090121.sql
61 lines (51 loc) · 1.27 KB
/
68languagetable-230714-090121.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
-- Tarea con countryLanguage
-- Crear la tabla de language
-- Sequence and defined type
CREATE SEQUENCE IF NOT EXISTS language_code_seq;
-- Table Definition
CREATE TABLE "public"."language" (
"code" int4 NOT NULL DEFAULT nextval('language_code_seq'::regclass),
"name" text NOT NULL,
PRIMARY KEY ("code")
);
-- Crear una columna en countrylanguage
ALTER TABLE countrylanguage
ADD COLUMN languagecode varchar(3);
-- Empezar con el select para confirmar lo que vamos a actualizar
select
DISTINCT language
from
countrylanguage
order by
language asc;
insert into
"language" (name)
select
DISTINCT "language"
from
countrylanguage
order by
language asc
-- Actualizar todos los registros
update
countrylanguage cl
set
languagecode = (
select
"code"
from
language l
where
cl.language = l.name
)
-- Cambiar tipo de dato en countrylanguage - languagecode por int4
alter table
countrylanguage
alter column
languagecode TYPE int4 USING languagecode :: integer
-- Crear el forening key y constraints de no nulo el language_code
alter TABLE
countrylanguage
add
CONSTRAINT fk_language_code FOREIGN KEY (languagecode) REFERENCES language (code) not null;
-- Revisar lo creado