|
Inserts new rows in a table
Insere novas linhas em uma tabela
There are two ways to write the INSERT INTO statement.
Existem duas maneiras de escrever a instrução INSERT INTO.
The first way specifies both the column names and the values to be inserted.
The syntax is:
A primeira forma especifica os nomes das colunas e os valores a serem inseridos.
A sintaxe é:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
|
Second way, If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query.
However, make sure the order of the values is in the same order as the columns in the table.
The INSERT INTO syntax would be as follows.
Segunda forma, se você estiver adicionando valores para todas as colunas da tabela, não é necessário especificar os nomes das colunas na consulta SQL.
No entanto, certifique-se de que a ordem dos valores esteja na mesma ordem das colunas da tabela.
A sintaxe INSERT INTO seria a seguinte.
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
|
Example 1 - Exemplo 1:
The table TB_DEPT contains the following columns:
A tabela TB_DEPT contém as seguintes colunas:
- DEPT_NO
- DEPT_NAME
- DEPT_MGR_NO
- DEPT_ADMR
Insert a new department with the following specifications into the TB_DEPT table.
Insira um novo departamento com as seguintes especificações na tabela TB_DEPT.
- Department number (DEPT_NO) is 'A31'
- Department name (DEPT_NAME) is 'ARCHITECTURE'
- Managed by (DEPT_MGR_NO) a person with number '00123'
- Reports to (DEPT_ADMR) department 'A01'
INSERT INTO TB_DEPT
VALUES (‘A31’
, ‘ELECTRICAL’
, ‘00123’
, ‘A01’);
|
In the above SQL query, There is no column name because you are adding values for all the columns of the table.
Na consulta SQL acima, não há nome de coluna porque você está adicionando valores para todas as colunas da tabela.
As we said before, If you are adding values for all the columns of the table, you do not need to specify the column names in the SQL query.
However, make sure the order of the values is in the same order as the columns in the table.
Como dissemos antes, se você estiver adicionando valores para todas as colunas da tabela, não precisa especificar os nomes das colunas na consulta SQL.
No entanto, certifique-se de que a ordem dos valores esteja na mesma ordem das colunas da tabela.
Result - Resultado:
Below is a selection from the "TB_DEPT" table in the DB2 database.
Abaixo está uma seleção da tabela "TB_DEPT" no banco de dados DB2.
DEPT_NO DEPT_NAME DEPT_MGR_NO DEPT_ADMR
A31 ELECTRICAL 00123 A01
|
Example 2 - Exemplo 2:
To insert a new department into the TB_DEPT table as in example 1, but without manager number to the new department.
Para inserir um novo departamento na tabela TB_DEPT como no exemplo 1, mas sem número de gerente para o novo departamento.
INSERT INTO TB_DEPT (DEPT_NO
, DEPT_NAME
, DEPT_ADMR )
VALUES (‘A31’
, ‘ELECTRICAL’
, ‘A01’);
|
In this example, You need to mention the column. because you are only inserting the data into specific columns.
Neste exemplo, você precisa mencionar a coluna. porque você está apenas inserindo os dados em colunas específicas.
Result - Resultado:
Below is a selection from the "TB_DEPT" table in the DB2 database.
Abaixo está uma seleção da tabela "TB_DEPT" no banco de dados DB2.
DEPT_NO DEPT_NAME DEPT_MGR_NO DEPT_ADMR
A31 ELECTRICAL A01
|
NULL value inserted into "TB_DEPT" table for the column DEPT_MGR_NO
Valor NULL inserido na tabela "TB_DEPT" para a coluna DEPT_MGR_NO
|