Insert is a commonly used command in the Structured Query Language (SQL) data manipulation language (DML) used by SQL Server and Oracle relational databases. The insert command inserts one or more rows into a database table with the specified table column values. The insert statement is the first DML command executed immediately after creating a table.
Syntax:
There are two basic syntaxes of the INSERT query, shown below.
- Only values: The first method is to specify only the data value to be inserted without specifying the column names.
INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
- Both column names and values: In the second method, we will specify both the columns to be filled and their associated values, as shown below:
INSERT INTO TABLE_NAME (column1, column2, column3,...columnN)
VALUES (value1, value2, value3,...valueN);
Example:
Insert value into Customer Table:
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 );
Insert value into the specific column:
INSERT INTO CUSTOMERS (ID,NAME)
VALUES (2, 'Fatima');
Insert NULL value to a column:
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)
VALUES (3, 'Sharda', 29, NULL, 34000.00 );
Insert Default value to a column:
INSERT INTO CUSTOMERS VALUES (4, 'Abdul', 30, NULL, default );
Output:
ID | NAME | AGE | ADDRESS | SALARY |
1 | Ramesh | 32 | Ahmedabad | 25000.00 |
2 | Fatima | |||
3 | Sharda | 29 | 34000.00 | |
4 | Abdul | 30 | 10000.00 |
- Using SELECT in INSERT INTO Statement:
To copy rows from one table and insert them into another, we can use the SELECT statement in conjunction with the INSERT INTO statement. This statement is used in the same way as the INSERT INTO statement. In this case, the SELECT statement is used to select data from a different table.
Syntax:
INSERT INTO first_table SELECT * FROM second_table;
OR
INSERT INTO table1 SELECT * FROM table2 WHERE condition;
here we are inserting into another table based on some conditions.
Note: also read about SQL: Truncate, Drop, Rename Query
Follow Me
Please follow me to read my latest post on programming and technology if you like my post.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment