|
| 1 | +# INSERT INTO Statement |
| 2 | +Reference: https://www.w3schools.com/sql/sql_insert.asp |
| 3 | + |
| 4 | +The INSERT INTO statement is used to insert new records in a table. |
| 5 | + |
| 6 | +It is possible to write the INSERT INTO statement in two ways: |
| 7 | + |
| 8 | +1. Specify both the column names and the values to be inserted: |
| 9 | + |
| 10 | + INSERT INTO table_name (column1, column2, column3, ...) |
| 11 | + VALUES (value1, value2, value3, ...); |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +2. 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. Here, the INSERT INTO syntax would be as follows: |
| 16 | + |
| 17 | + INSERT INTO table_name |
| 18 | + VALUES (value1, value2, value3, ...); |
| 19 | + |
| 20 | + |
| 21 | +## CUSTOMERS_TABLE |
| 22 | + |
| 23 | +| CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country | |
| 24 | +|------------|--------------------|-----------------|--------------------|------------|------------|---------| |
| 25 | +| 1 | Company A | John Doe | 123 Main Street | New York | 10001 | USA | |
| 26 | +| 2 | Company B | Jane Smith | 456 Oak Avenue | Los Angeles| 90001 | USA | |
| 27 | +| 3 | Company C | Michael Johnson | 789 Elm Drive | Chicago | 60601 | USA | |
| 28 | +| 4 | Company D | Sarah Brown | 101 Pine Road | Houston | 77001 | USA | |
| 29 | + |
| 30 | + |
| 31 | +### Q1 |
| 32 | +For the given table `CUSTOMERS`, Insert a new record into the table. (CustomerID will be updated automatically) |
| 33 | + |
| 34 | +<h4 style="color: goldenrod;">Answer: </h4> |
| 35 | + |
| 36 | +```sql |
| 37 | +INSERT INTO CUSTOMERS (CustomerName, ContactName, Address, City, PostalCode, Country) |
| 38 | +VALUES ('Company E', 'Utkarsh Hadgekar', 'Cannought Place, Delhi, India', 'Delhi', 20304, 'INDIA'); |
| 39 | +``` |
| 40 | + |
| 41 | + |
| 42 | +## Insert Data Only in Specified Columns |
| 43 | +It is also possible to only insert data in specific columns. |
| 44 | + |
| 45 | +### Q1 |
| 46 | +For the given table `CUSTOMERS`, only insert data in the "CustomerName", "City", and "Country" columns. (CustomerID will be updated automatically). |
| 47 | + |
| 48 | +```sql |
| 49 | +INSERT INTO CUSTOMERS (CustomerName, City, Country) |
| 50 | +VALUES ('Company F', 'Tokyo', 'JAPAN'); |
| 51 | +``` |
| 52 | + |
| 53 | + |
| 54 | +## Insert Multiple Rows |
| 55 | + |
| 56 | +- It is also possible to insert multiple rows in one statement. |
| 57 | +- To insert multiple rows of data, we use the same INSERT INTO statement, but with multiple values |
| 58 | + |
| 59 | + |
| 60 | +### Q1 |
| 61 | +For the given table `CUSTOMERS`, insert 3 rows with the required data into the table. |
| 62 | + |
| 63 | +```sql |
| 64 | +INSERT INTO CUSTOMERS (CustomerName, ContactName, Address, City, PostalCode, Country) |
| 65 | +VALUES |
| 66 | + ('Company F', 'John Smith', '789 Elm Street', 'Chicago', 60602, 'USA'), |
| 67 | + ('Company G', 'Emma Johnson', '101 Pine Avenue', 'Houston', 77002, 'USA'), |
| 68 | + ('Company H', 'Alice Brown', '456 Maple Road', 'Los Angeles', 90002, 'USA'); |
| 69 | +``` |
| 70 | + |
0 commit comments