SQL insert into
Created By: chatGPT
To insert data into a SQL table, you typically use the
INSERT INTO
statement. This command allows you to specify the table you want to add data to and the values for each column. Below is the general syntax along with an example.INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);
Here's a practical example using a table named
employees
with columns first_name
, last_name
, and email
.INSERT INTO employees (first_name, last_name, email)
VALUES ('John', 'Doe', 'john.doe@example.com');
You can also insert multiple rows in a single SQL statement. This can be done by separating each set of values with commas.
INSERT INTO employees (first_name, last_name, email)
VALUES
('Jane', 'Doe', 'jane.doe@example.com'),
('Alice', 'Johnson', 'alice.j@example.com');
If you want to insert data into all columns of the table, you don’t need to specify the column names. The values must correspond to the order of the columns in the table schema.
INSERT INTO employees
VALUES (NULL, 'Bob', 'Smith', 'bob.smith@example.com');
In some cases, you might want to insert a new record based on a selection from another table. You can achieve this using the
INSERT INTO ... SELECT
syntax.INSERT INTO new_table (column1, column2)
SELECT column1, column2
FROM existing_table
WHERE condition;