The INSERT statement is used to insert a new record into the table.
Ways to insert the data into a table:
- 1. Inserting the data directly to a table.
- 2. Inserting data to a table through a select statement.
1. Inserting the data directly to a table.
In case when we insert the values in the specified columns.
Syntax: INSERT INTO tableName [(column1, column2,…, columnN)] VALUES (value1, value2,…,valueN);
Example:
Output:
2. In case when we insert the values in all columns.
Syntax: INSERT INTO tableName VALUES (value1, value2,…,valueN);
We can ignore the column names if we want to insert the values in all columns of the table.
Example:
Output:
2. Inserting data to a table through a select statement.
In case when we insert the values in the specified columns.
Syntax: INSERT INTO destinationTableName [(column1, column2, … columnN)] SELECT column1, column2, …columnN FROM sourceTableName WHERE [condition];
Example:
Output:
In case when we insert the values in all columns.
Syntax: INSERT INTO destinationTableName SELECT * FROM sourceTableName;
No comments:
Post a Comment