-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmodulo-04.sql
More file actions
70 lines (52 loc) · 1.93 KB
/
modulo-04.sql
File metadata and controls
70 lines (52 loc) · 1.93 KB
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
62
63
64
65
66
67
68
69
70
-- Insertar datos
-- 1. Insert into tabla (campo1, ..., campoN) VALUES (valor1, ..., valorN);
INSERT INTO persons (id, first_name, last_name, birthday, available)
VALUES (gen_random_uuid(), 'Alexys', 'Lozada', '1980-01-02', true);
INSERT INTO persons (first_name, birthday, last_name, available, id)
VALUES ( 'Alexys', '1980-01-02', 'Lozada', true, gen_random_uuid());
-- 2. Insert into tabla VALUES (valor1, valor2, ..., valorN);
INSERT INTO persons
VALUES (gen_random_uuid(), 'Alexys', 'Lozada', '1980-01-02', true);
-- 3. Insert into tabla VALUES (default, valorN, valorM, ..., valorZ);
INSERT INTO persons
VALUES (DEFAULT, 'Alexys', DEFAULT, DEFAULT, true);
-- 3.1 Valores por defecto.
CREATE TABLE students (
id UUID DEFAULT gen_random_uuid(),
first_name VARCHAR(50),
is_active BOOL DEFAULT true,
created_at TIMESTAMP DEFAULT now()
);
INSERT INTO students
VALUES (DEFAULT, 'Pedro', DEFAULT, DEFAULT);
INSERT INTO students (first_name)
VALUES ('JUAN');
-- 4. Insert into tabla VALUES (valorA1, ..., valorAn), (valorB1, ..., valorBN);
INSERT INTO students
VALUES
(DEFAULT, 'Pedro', DEFAULT, DEFAULT),
(DEFAULT, 'Leidy', DEFAULT, DEFAULT),
(DEFAULT, 'Jenny', DEFAULT, DEFAULT),
(DEFAULT, 'Juan', DEFAULT, DEFAULT);
INSERT INTO students (first_name, created_at)
VALUES
('Juan', '2021-06-01'),
('Paola', '2021-06-01');
-- 5. Insert into tabla SELECT your-query;
INSERT INTO students (first_name, is_active)
SELECT f_name, active
FROM tmp_students;
-- 6. Datos nulos.
INSERT INTO students VALUES (NULL, NULL, NULL, NULL);
DROP TABLE students;
CREATE TABLE students (
id UUID DEFAULT gen_random_uuid() NOT NULL,
first_name VARCHAR(50) NOT NULL,
is_active BOOL NOT NULL,
created_at TIMESTAMP DEFAULT now() NOT NULL,
updated_at TIMESTAMP
);
INSERT INTO students
VALUES (DEFAULT, 'Alexys', TRUE, DEFAULT, DEFAULT);
-- No es posible, porque viola las restricciones de nulo.
INSERT INTO students VALUES (NULL, NULL, NULL, NULL);