Insert and Select Command
In the previous tutorial, we have come across creating tables and deleting them. Now, in this tutorial, let us look into the insertion of data into the tables, in the form of rows and about the most used SELECT command in the SQL queries. Insert and select commands
INSERT Command
Insert command is used to add the rows in the tables. It is the most common command used after the SELECT command. Insert command can be useful to fill the data in the tables accordingly.
- Inserting the one complete row
- Inserting the one row partially
- Inserting the result of the query
Now, let us look into the syntax with an example of the implementation of the Insert Query on the Tables.
Inserting the one complete row
The way of inserting the one complete row in the table needs the name of the table with the values that are to be stored, implemented with the INSERT command.
Syntax
INSERT INTO Table_name
VALUES( ‘val1’, ‘val2’, ……. ‘valN’);
Inserting the one row partially
You can insert some part of the values in the row of a table in a database. The implementation looks similar to the above case, but need to mention the columns in the table along with the values that are to be inserted.
Syntax
INSERT INTO Table_name( Col1, Col2, …… ColN)
VALUES(‘val1’, ‘val2’, ……. ‘valN’);
One must be aware that the values have to be mentioned properly with respect to the order of the columns mentioned to avoid inserting the wrong data into the table.
The other important point over the implementation of the inserting the partial data across the row is that, if any value is mentioned with the NOT NULL constraint, then it is compulsory to provide the value to that column. If the column is defined with the default value, then there is no issue. But if the column is with NOT NULL constraint and without any default value, then the insertion of the data into the table will fail.
Inserting the result of the query
Insert command can insert the rows of the table with the data, but using the INSERT command along with the SELECT command, we can import the list of all the rows from one table and copy them into the other table accordingly.
Syntax
INSERT INTO Table_name1 (col1, col2, ….. colN)
SELECT col1, col2, ….. colN
FROM Table_name2;
You can insert only some columns from one table to the other and there is no necessity that the name of the columns of both the tables must be same.
Generally, insert command is useful to insert one single row of a table, but this INSERT SELECT command will insert multiple rows from one table to the other.
SELECT Command
The other important SQL command is SELECT, which is frequently used to retrieve the data from the table. The SELECT keyword requires, what data you want to select and from where you want to select.
The SELECT command can also be implemented in the following ways, to retrieve the data.
Retrieving the Individual & Multiple Columns
We can select column of a table and the name of the table to retrieve all the elements from that column,
Syntax
SELECT Column_name
FROM Table_Name
Similarly, the variation in the syntax is little different to retrieve the multiple columns from the table.
Syntax
SELECT Column_name1, Column_name2, …. Column_nameN
FROM Tbale_name
Execution of the command with this syntax will retrieve and display all the multiple columns selected from the table.
Retrieving all the Columns
The SELECT statement can retrieve all the columns from the table. This can be possible with the asterisk (*) symbol.
Syntax
SELECT*
From Table_name;
This completes this tutorial and in the following upcoming tutorials, we can discuss much regarding the SELECT command, which is mostly used. For any sort of doubts or questions, you can reach out through the comment box.
Read related topics: