Skip to content
Published on

MySQL SQL Basics (SELECT, INSERT, DELETE, UPDATE) Usage

Authors
  • Name
    Twitter

Overview

Learn how to use SQL (SELECT, INSERT, DELETE, UPDATE) in MySQL.

How to Install MySQL on Ubuntu

sudo apt install mysql-server
service mysql start
mysql -u root

Checking Databases

mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)

Creating a Database

mysql> create database my_database;
Query OK, 1 row affected (0.01 sec)

Creating a Table

mysql> CREATE TABLE person ( id INT NOT NULL AUTO_INCREMENT , name VARCHAR(20),
PRIMARY KEY(id) );

Checking Table Schema

mysql> show create table person\G
*************************** 1. row ***************************
       Table: person
Create Table: CREATE TABLE `person` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

Row Insert

To insert a row, use the INSERT INTO {table_name}, VALUES ({value1}, {value2}); syntax.

mysql> INSERT INTO person VALUES (3,'abc');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM person;
+----+-------+
| id | name  |
+----+-------+
|  1 | NULL  |
|  2 | chaos |
|  3 | abc   |
+----+-------+
3 rows in set (0.00 sec)

Update Column

To update a column, the following syntax is generally used: UPDATE {table_name} SET {column_name}={value} WHERE {condition};

mysql> select * FROM person;
+----+-------+
| id | name  |
+----+-------+
|  1 | NULL  |
|  2 | chaos |
|  3 | abc   |
+----+-------+
3 rows in set (0.00 sec)

mysql> UPDATE person SET name='def' WHERE id=3;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * FROM person;
+----+-------+
| id | name  |
+----+-------+
|  1 | NULL  |
|  2 | chaos |
|  3 | def   |
+----+-------+