SQL data manipulation workshop: INSERT statement
The INSERT
statement is used to insert new records into a table in SQL.
SQL INSERT Operation Properties:
Column Specification (Optional): You can specify which columns you want to insert data into. If you don't, you'll need to provide a value for every column in the table.
Value Specification: You must provide values that match the specified columns in type and order. If a column has a default value defined and you don't specify a value for it, the default value will be used.
Multiple Rows Insertion: You can insert multiple rows with a single
INSERT
statement.Return Value: Most SQL databases do not return a result set for an
INSERT
statement, but many will return a count of rows inserted or a unique identifier for the last inserted row.
1. Inserting into all columns:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
In this case, you have to make sure that the values are in the same order as the columns in the table, and that a value is provided for every column.
2. Inserting into specific columns:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
When specifying columns, you only need to provide values for the columns listed. This gives you the flexibility to insert data into only the columns you want, leaving others to be filled with default values (if they have any defined).
3. Inserting multiple rows:
INSERT INTO table_name (column1, column2, ...)
VALUES
(value1a, value2a, ...),
(value1b, value2b, ...),
...
(value1n, value2n, ...);
This syntax allows you to insert multiple rows with a single INSERT
statement.
Important Notes:
The number of values must match the number of columns specified, and their data types should be compatible.
If a column has a default value and you don't provide a value for it in the
INSERT
statement, the default value will be used.If you're working with strings or date values, ensure they're enclosed in single quotes (
'
).
For more advanced usage, SQL also supports INSERT ... ON DUPLICATE KEY UPDATE
(in MySQL) or INSERT ... ON CONFLICT DO ...
(in PostgreSQL) to handle situations where the data might conflict with existing records. This is particularly useful when dealing with unique constraints.
Workshop: Inserting Records in MySQL
Setup:
To create the database:
create database workshop;
To grant permissions to the database:
GRANT ALL PRIVILEGES ON workshop.* TO 'user'@'localhost';
Let's say we have a table called students
with the following structure:
CREATE TABLE students (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
age INT
);
Exercise 1: Inserting a Single Row
INSERT INTO students (first_name, last_name, age)
VALUES ('John', 'Doe', 20);
In this statement, we specify the first_name
, last_name
, and age
columns and provide the corresponding values. The id
column is auto-incremented, so we don't need to provide a value for it.
Exercise 2: Inserting Multiple Rows
INSERT INTO students (first_name, last_name, age)
VALUES
('Alice', 'Smith', 25),
('Bob', 'Johnson', 22),
('Charlie', 'Brown', 23);
Here, we're inserting three new records with a single INSERT
statement. Each set of values inside the VALUES
clause corresponds to a new row.
Things to remember:
Ensure the data type of values matches the column's data type.
Be cautious about NOT NULL columns. If a column is defined as NOT NULL, then you must provide a value for it during the insert, unless the column has a default value defined.
Ensure that unique constraints or primary key constraints are not violated. For example, if
first_name
andlast_name
together had a unique constraint, you couldn't insert two records with the same first and last names.