A simple INSERT
query in SQL is used to add new records to a table in a database. Here’s the basic syntax for an INSERT
statement:
Syntax
sqlCopy codeINSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
Example
Suppose you have a table called employees
with the following columns: id
, name
, and position
. To insert a new employee, you would use:
sqlCopy codeINSERT INTO employees (id, name, position)
VALUES (1, 'John Doe', 'Software Engineer');
Notes
- Make sure the values match the data types of the respective columns.
- If you are inserting a value for every column in the table and the columns are in the same order as they appear in the table, you can omit the column names:
sqlCopy codeINSERT INTO employees
VALUES (2, 'Jane Smith', 'Data Analyst');
Important Considerations
- If a column is set to auto-increment (like
id
in many cases), you can skip it in yourINSERT
statement:
sqlCopy codeINSERT INTO employees (name, position)
VALUES ('Alice Johnson', 'Project Manager');
This will automatically assign the next available ID to the new record.
Feel free to ask if you need more examples or explanations!