CRUD is a popular acronym for create, read, update, and delete. It’s the foundation of any programming language or framework.
In this shot, we’ll cover:
Let’s get started!
MySQL
, PostgreSQL
…)-- connect to MySQL
mysql -uroot -p
-- enter password, hit Enter
-- create a database
CREATE DATABASE test_crud;
-- select the database
USE test_crud;
Create at least one database and select it as explained above.
The syntax looks like this:
CREATE TABLE [IF NOT EXISTS] table_name (
column1 datatype(length) column_contraint,
column2 datatype(length) column_contraint,
column3 datatype(length) column_contraint,
table_constraints
);
Here, datatype
can be serial
, VARCHAR
, etc., and constraints
can be PRIMARY KEY
, UNIQUE
, and so on.
Let’s look at a real example:
CREATE TABLE users (id serial PRIMARY KEY,username VARCHAR ( 50 ) UNIQUE NOT NULL,password VARCHAR ( 50 ) NOT NULL,email VARCHAR ( 255 ) UNIQUE NOT NULL);
To see the list of all tables, we use:
SHOW TABLES;
There are many possibilities here. We can:
ALTER TABLE table_name
RENAME TO new_table_name;
ALTER TABLE table_name
CHANGE COLUMN old_name TO new_name;
CREATE TABLE users (id serial PRIMARY KEY,username VARCHAR ( 50 ) UNIQUE NOT NULL,password VARCHAR ( 50 ) NOT NULL,email VARCHAR ( 255 ) UNIQUE NOT NULL);ALTER TABLE usersRENAME TO clients;SHOW TABLES;
ALTER TABLE table_name
ADD column_name column_definition
[ FIRST | AFTER column_name ];
column_definition
: The data type and definition of the column (NULL
or NOT NULL
).
FIRST | AFTER column_name
: This is optional. It tells MySQL where in the table to create the column. If not specified, the new column is added to the end of the table.
ALTER TABLE table_name
DROP COLUMN column_name;
DROP TABLE IF EXISTS table_name;
CREATE TABLE users (id serial PRIMARY KEY,username VARCHAR ( 50 ) UNIQUE NOT NULL,password VARCHAR ( 50 ) NOT NULL,email VARCHAR ( 100 ) UNIQUE NOT NULL);ALTER TABLE usersADD country VARCHAR ( 50 );DESC users;
In this section, we’ll learn how to work with entries (data) in a table. CRUD operations are equivalents to the following statements:
INSERT
SELECT
UPDATE
DELETE
INSERT INTO
statementThe syntax to insert data into a table is:
INSERT INTO table_name(column1, column2, …)
VALUES (value1, value2, …);
Let’s insert five entries into our table (users
):
INSERT INTO users(username, password, email)
VALUES
('bam', 'B@m:321', 'bam@ulikis.cd'),
('salimas', 'sal1989', 'salimas@gmail.com'),
('pati', 'kav87', 'reseacher@unikis.cd'),
('yves', '1v3s:321', 'yves@yves.com'),
('val', 'valnas123', 'valnas@gmail.com')
;
Here, we insert multiple entries at once. We ignored the id
column because SQL handles it automatically.
SELECT
statementTo read entries, use the SELECT
command:
SELECT * FROM users;
To display all entries from the users
table:
SELECT * FROM users ORDER BY username;
This command will order the output by username
within an existing column in the table.
CREATE TABLE users (id serial PRIMARY KEY,username VARCHAR ( 50 ) UNIQUE NOT NULL,password VARCHAR ( 50 ) NOT NULL,email VARCHAR ( 255 ) UNIQUE NOT NULL);#--- insert dataINSERT INTO users(username, password, email)VALUES('bam', 'B@m:321', 'bam@ulikis.cd'),('salimas', 'sal1989', 'salimas@gmail.com'),('pati', 'kav87', 'reseacher@unikis.cd'),('yves', '1v3s:321', 'yves@yves.com'),('val', 'valnas123', 'valnas@gmail.com');#-- read entriesSELECT * FROM users ORDER BY username;
UPDATE
statementImagine that a user named pati
changes their email address. We would then want to modify it in our database too.
How can we proceed? We cannot use the INSERT
command because the user is already in the database. Instead, we use the UPDATE
command:
UPDATE table_name
SET column_name = "new value"
WHERE condition;
DELETE
statementUse the DELETE
command like this:
DELETE FROM table_name
WHERE condition;
To illustrate this, let’s say we want to delete the fourth entry, id 4
:
DELETE FROM users
WHERE id=4;
Here, we use the id
column. If we want to use the username
column, we can write it like this:
DELETE FROM users
WHERE username='yves';
Let’s recall some of the essential points:
CREATE TABLES
, SHOW TABLES
, ALTER TABLE
, and DROP TABLE
if we work with tables in general.INSERT
, SELECT
, UPDATE
, and DELETE
if we work with table content.Happy coding!