CRUD operations explained: Create, read, update, and delete
- ️Wed Dec 16 2020
SQL is a standard language used to store, manipulate, and retrieve data from databases. Let’s go over a few different ways you can use CRUD SQL queries to create a database and table, insert data into tables, and perform basic operations on data within tables.
CREATE operation#
The CREATE operation lets you create a new database and its tables.
In SQL, a new database is created using the CREATE DATABASE
keyword.
CREATE DATABASE db_name
Similarly, you can create a new table using the `CREATE TABLE` keyword.
CREATE TABLE table_name (col_1, col_2, ...)
You can add new records using the command INSERT INTO
. This command starts with the INSERT INTO
keyword, followed by the table name, column names, and the values to be inserted.
When using INSERT INTO
, you have two options:
INSERT INTO table_name
VALUES (value1, value2, value3, ...);
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
In the example below, you can create a database bakery
and table menu
. Afterward, you can insert data into your menu
table.
CREATE DATABASE bakery;
CREATE TABLE menu (id Int, item varchar(10), date DATE);
INSERT INTO menu
VALUES (1, 'croissant', '2020-12-16');
If you want to insert multiple rows, you can do that using the following syntax:
INSERT INTO menu
VALUES
(2, 'bread', '2020-12-16' ),
(3, 'eclairs', '2020-12-16' );
This will insert two new rows to the menu
table, and give each entry a unique id
.