2.14. Inserindo Dados
Sintaxe:
INSERT INTO nome-tabela (campo1, campo2, ...)
VALUES (v1a, v2a, ...)[,(v1b, v2b, ...)];
Vamos inserir as seguintes tuplas na tabela estudante
:
INSERT INTO estudante (nome, data_nascimento, genero, data_matricula)
VALUES ('Eduardo', '1980-08-04', 'M', '2019-01-01'),
('Maria', '1980-04-03', 'F', '2019-02-02'),
('Eugenio', '1981-02-01', 'M', '2019-02-02'),
('Luiza', '1981-09-14', 'F', '2020-01-05'),
('Ana Maria', '1981-01-04', 'F', '2020-01-05'),
('Ana Clara', '1981-03-04', 'F', '2020-01-05'),
('Manuel', '1981-10-11', 'M', '2020-01-06'),
('Jose', '1981-12-12', 'M', '2020-01-06'),
('Ricardo', '1982-11-11', 'M', '2020-01-01'),
('Claudio', '1982-07-07', 'M', '2019-01-01'),
('Carla', '1982-06-06', 'F', '2019-01-05'),
('Telma', '1983-05-05', 'F', '2020-01-06'),
('Gilberto', '1983-04-04', 'M', '2021-01-08'),
('Lucia', '1983-08-06', 'F', '2021-01-08'),
('Tassiana', '1983-09-06', 'F', '2021-01-08'),
('Julio', '1983-10-06', 'M', '2021-01-08'),
('Felipe', '1983-11-04', 'M', '2021-01-08'),
('Carlos', '1984-12-04', 'M', '2021-01-09'),
('Fabio', '1984-01-04', 'M', '2021-01-10'),
('Mateus', '1984-02-04', 'M', '2022-01-10'),
('Joana', '1984-03-04', 'F', '2022-01-10'),
('Manuel', '1984-04-04', 'M', '2022-01-10'),
('Gilberto', '1978-05-04', 'M', '2022-01-10'),
('Marco', '1985-06-04', 'M', '2022-01-10'),
('Marcia', '1984-07-04', 'F', '2022-01-11'),
('Luis', '1984-08-04', 'M', '2022-01-12'),
('Everaldo', '1985-09-04', 'M', '2022-01-12'),
('Katia', '1985-10-04', 'F', '2022-01-12'),
('Emiliano', '1985-11-04', 'M', '2022-01-12'),
('Zoraide', '1985-12-04', 'F', '2022-01-12'),
('Roberta', '1985-01-04', 'F', '2019-01-01'),
('Roberto', '1985-02-04', 'M', '2019-01-01'),
('Antonio', '1985-03-04', 'M', '2021-01-08'),
('Joana', '1985-04-04', 'F', '2019-01-01'),
('Josi', '1985-05-04', 'F', '2022-01-12'),
('Antonio', '1976-06-30', 'M', '2019-01-01'),
('Zuleica', '1986-06-30', 'M', '2019-01-01');
Vamos inserir as seguintes tuplas na tabela professor
:
INSERT INTO professor (nome) VALUES ('Romildo'), ('Thales'), ('Karine'), ('Tamara'), ('Carolina'), ('Cleiton');
Vamos inserir as seguintes tuplas na tabela disciplina
:
INSERT INTO disciplina (titulo, creditos, professor_codigo) VALUES ('Matemática', 6, 1);
INSERT INTO disciplina (titulo, creditos, professor_codigo) VALUES ('Fisica', 4, 1);
INSERT INTO disciplina (titulo, creditos, professor_codigo) VALUES ('Bilogia', 2, 2);
INSERT INTO disciplina (titulo, creditos, professor_codigo) VALUES ('Quimica', 3, 3);
INSERT INTO disciplina (titulo, creditos, professor_codigo) VALUES ('Geografia', 2, 4);
INSERT INTO disciplina (titulo, creditos, professor_codigo) VALUES ('Historia', 2, 4);
INSERT INTO disciplina (titulo, creditos, professor_codigo) VALUES ('Lingua Portuguesa', 4, 4);
INSERT INTO disciplina (titulo, creditos, professor_codigo) VALUES ('Lingua Inglesa', 2, 5);
INSERT INTO disciplina (titulo, creditos, professor_codigo) VALUES ('Lingua Francesa', 1, 5);
INSERT INTO disciplina (titulo, creditos, professor_codigo) VALUES ('Ciências', 1, 4);
Vamos inserir as seguintes tuplas na tabela estudante_disciplina
:
INSERT INTO estudante_disciplina VALUES (1, 1, '2019-06-01'), (1, 2, '2019-06-01'),
(2, 1, '2019-06-01'), (2, 3, '2019-06-01'),
(2, 4, '2019-06-01'), (3, 6, '2019-06-01'),
(4, 7, '2020-06-01'), (5, 8, '2020-06-01'),
(5, 9, '2020-06-01'), (6, 5, '2020-06-01'),
(6, 6, '2020-06-01'), (6, 7, '2020-06-01'),
(7, 1, '2020-06-01'), (7, 8, '2020-06-01'),
(7, 9, '2020-06-01'), (7, 6, '2020-06-01'),
(7, 2, '2020-06-01'), (8, 1, '2020-06-01'),
(8, 2, '2020-06-01'), (8, 3, '2020-06-01'),
(9, 4, '2020-06-01'), (9, 5, '2020-06-01'),
(10, 6, '2019-06-01'), (11, 7, '2019-06-01'),
(12, 8, '2020-06-01'), (12, 9, '2020-06-01'),
(12, 1, '2020-06-01'), (13, 2, '2021-06-01'),
(13, 3, '2021-06-01'), (14, 4, '2021-06-01'),
(15, 5, '2021-06-01'), (15, 6, '2021-06-01'),
(15, 7, '2021-06-01'), (15, 8, '2021-06-01'),
(16, 9, '2021-06-01'), (16, 1, '2021-06-01'),
(17, 2, '2021-06-01'), (17, 3, '2021-06-01'),
(18, 4, '2021-06-01'), (18, 5, '2021-06-01'),
(18, 6, '2021-06-01'), (19, 7, '2021-06-01'),
(19, 8, '2021-06-01'), (19, 9, '2021-06-01'),
(20, 9, '2022-06-01'), (21, 9, '2022-06-01'),
(22, 9, '2022-06-01'), (23, 7, '2022-06-01'),
(23, 8, '2022-06-01'), (23, 9, '2022-06-01'),
(24, 8, '2022-06-01'), (24, 9, '2022-06-01'),
(25, 1, '2022-06-01'), (25, 2, '2022-06-01'),
(26, 1, '2022-06-01'), (26, 2, '2022-06-01'),
(27, 1, '2022-06-01'), (27, 2, '2022-06-01'),
(28, 1, '2022-06-01'), (28, 2, '2022-06-01'),
(29, 4, '2022-06-01'), (30, 5, '2022-06-01'),
(30, 6, '2022-06-01'), (31, 5, '2019-06-01'),
(32, 6, '2019-06-01'), (33, 5, '2021-06-01'),
(34, 6, '2019-06-01'), (34, 5, '2019-06-01'),
(35, 6, '2022-06-01'), (36, 5, '2019-06-01'),
(36, 6, '2019-06-01'), (36, 7, '2019-06-01'),
(36, 8, '2019-06-01');
Nota
Observe que nos comandos acima relativos à tabela estudante
, omitimos os valores para a coluna matricula
. Como definimos essa coluna como GENERATED ALWAYS AS IDENTITY
, o PostgreSQL utiliza valores de uma sequência para preencher este campo automaticamente quando omitido. De maneira similar, omitimos os valores para a coluna codigo
nos comandos de inserção na tabela professor
e disciplina
.
2.14.1. Conteúdo das Tabelas
Apenas para referência, apresentamos abaixo o conteúdo de cada tabela.
Tabela: professor
codigo | nome
--------+----------
1 | Romildo
2 | Thales
3 | Karine
4 | Tamara
5 | Carolina
6 | Cleiton
(6 rows)
Tabela: disciplina
codigo | titulo | creditos | professor_codigo
--------+-------------------+----------+------------------
1 | Matemática | 6 | 1
2 | Fisica | 4 | 1
3 | Bilogia | 2 | 2
4 | Quimica | 3 | 3
5 | Geografia | 2 | 4
6 | Historia | 2 | 4
7 | Lingua Portuguesa | 4 | 4
8 | Lingua Inglesa | 2 | 5
9 | Lingua Francesa | 1 | 5
10 | Ciências | 1 | 4
(10 rows)]
Tabela: estudante
matricula | nome | data_nascimento | genero | data_matricula
-----------+-----------+-----------------+--------+----------------
1 | Eduardo | 1980-08-04 | M | 2019-01-01
2 | Maria | 1980-04-03 | F | 2019-02-02
3 | Eugenio | 1981-02-01 | M | 2019-02-02
4 | Luiza | 1981-09-14 | F | 2020-01-05
5 | Ana Maria | 1981-01-04 | F | 2020-01-05
6 | Ana Clara | 1981-03-04 | F | 2020-01-05
7 | Manuel | 1981-10-11 | M | 2020-01-06
8 | Jose | 1981-12-12 | M | 2020-01-06
9 | Ricardo | 1982-11-11 | M | 2020-01-01
10 | Claudio | 1982-07-07 | M | 2019-01-01
11 | Carla | 1982-06-06 | F | 2019-01-05
12 | Telma | 1983-05-05 | F | 2020-01-06
13 | Gilberto | 1983-04-04 | M | 2021-01-08
14 | Lucia | 1983-08-06 | F | 2021-01-08
15 | Tassiana | 1983-09-06 | F | 2021-01-08
16 | Julio | 1983-10-06 | M | 2021-01-08
17 | Felipe | 1983-11-04 | M | 2021-01-08
18 | Carlos | 1984-12-04 | M | 2021-01-09
19 | Fabio | 1984-01-04 | M | 2021-01-10
20 | Mateus | 1984-02-04 | M | 2022-01-10
21 | Joana | 1984-03-04 | F | 2022-01-10
22 | Manuel | 1984-04-04 | M | 2022-01-10
23 | Gilberto | 1978-05-04 | M | 2022-01-10
24 | Marco | 1985-06-04 | M | 2022-01-10
25 | Marcia | 1984-07-04 | F | 2022-01-11
26 | Luis | 1984-08-04 | M | 2022-01-12
27 | Everaldo | 1985-09-04 | M | 2022-01-12
28 | Katia | 1985-10-04 | F | 2022-01-12
29 | Emiliano | 1985-11-04 | M | 2022-01-12
30 | Zoraide | 1985-12-04 | F | 2022-01-12
31 | Roberta | 1985-01-04 | F | 2019-01-01
32 | Roberto | 1985-02-04 | M | 2019-01-01
33 | Antonio | 1985-03-04 | M | 2021-01-08
34 | Joana | 1985-04-04 | F | 2019-01-01
35 | Josi | 1985-05-04 | F | 2022-01-12
36 | Antonio | 1976-06-30 | M | 2019-01-01
37 | Zuleica | 1986-06-30 | M | 2019-01-01
(37 rows)
Tabela: estudante_disciplina
matricula | codigo | data
-----------+--------+------------
1 | 1 | 2019-06-01
1 | 2 | 2019-06-01
2 | 1 | 2019-06-01
2 | 3 | 2019-06-01
2 | 4 | 2019-06-01
3 | 6 | 2019-06-01
4 | 7 | 2020-06-01
5 | 8 | 2020-06-01
5 | 9 | 2020-06-01
6 | 5 | 2020-06-01
6 | 6 | 2020-06-01
6 | 7 | 2020-06-01
7 | 1 | 2020-06-01
7 | 8 | 2020-06-01
7 | 9 | 2020-06-01
7 | 6 | 2020-06-01
7 | 2 | 2020-06-01
8 | 1 | 2020-06-01
8 | 2 | 2020-06-01
8 | 3 | 2020-06-01
9 | 4 | 2020-06-01
9 | 5 | 2020-06-01
10 | 6 | 2019-06-01
11 | 7 | 2019-06-01
12 | 8 | 2020-06-01
12 | 9 | 2020-06-01
12 | 1 | 2020-06-01
13 | 2 | 2021-06-01
13 | 3 | 2021-06-01
14 | 4 | 2021-06-01
15 | 5 | 2021-06-01
15 | 6 | 2021-06-01
15 | 7 | 2021-06-01
15 | 8 | 2021-06-01
16 | 9 | 2021-06-01
16 | 1 | 2021-06-01
17 | 2 | 2021-06-01
17 | 3 | 2021-06-01
18 | 4 | 2021-06-01
18 | 5 | 2021-06-01
18 | 6 | 2021-06-01
19 | 7 | 2021-06-01
19 | 8 | 2021-06-01
19 | 9 | 2021-06-01
20 | 9 | 2022-06-01
21 | 9 | 2022-06-01
22 | 9 | 2022-06-01
23 | 7 | 2022-06-01
23 | 8 | 2022-06-01
23 | 9 | 2022-06-01
24 | 8 | 2022-06-01
24 | 9 | 2022-06-01
25 | 1 | 2022-06-01
25 | 2 | 2022-06-01
26 | 1 | 2022-06-01
26 | 2 | 2022-06-01
27 | 1 | 2022-06-01
27 | 2 | 2022-06-01
28 | 1 | 2022-06-01
28 | 2 | 2022-06-01
29 | 4 | 2022-06-01
30 | 5 | 2022-06-01
30 | 6 | 2022-06-01
31 | 5 | 2019-06-01
32 | 6 | 2019-06-01
33 | 5 | 2021-06-01
34 | 6 | 2019-06-01
34 | 5 | 2019-06-01
35 | 6 | 2022-06-01
36 | 5 | 2019-06-01
36 | 6 | 2019-06-01
36 | 7 | 2019-06-01
36 | 8 | 2019-06-01
(73 rows)