How to Inserts new rows in a table in SQL ?
To insert new rows in a table in SQL, you can use the INSERT INTO
statement. There are two ways to use this statement:
- Inserting values directly into the table:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Example:
INSERT INTO customers (name, email, phone)
VALUES ('John Doe', '[email protected]', '123-456-7890');
- Inserting values from another table:
INSERT INTO table_name (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM another_table_name
WHERE condition;
Example:
INSERT INTO customers (name, email, phone)
SELECT name, email, phone
FROM new_customers
WHERE date_added >= '2021-01-01';
Note: Make sure that the data types of the values being inserted match the data types of the columns in the table. Also, if a column has a NOT NULL
constraint, a value must be provided for that column during the insertion.