Câu lệnh MySQL cơ bản trên Linux

MySQL

Contents:

MySQL là hệ quản trị cơ sở dữ liệu mã nguồn mở phổ biến nhất , nó được sử dụng rộng rãi trong nhiều môi trường phát triển như WindowsLinux, Mac OS X, Unix, FreeBSD, NetBSD, Novell NetWare, SGI Irix, Solaris, SunOS,..

Bài viết này sẽ hướng dẫn các bạn một số câu lệnh mysql cơ bản trong môi trường Linux.

Xem thêm:

Khởi động/ Dừng/Khởi động lại MySQL

Để khởi động MySQL các bạn thực hiện câu lệnh sau:

# systemctl start mysqld.service     # cho hệ thống sử dụng systemd 
# /etc/init.d/mysqld start           # cho hệ thống sử dụng init

Dừng dịch vụ:

# systemctl stop mysqld.service # cho hệ thống sử dụng systemd
# /etc/init.d/mysqld stop# cho hệ thống sử dụng init

Khởi động lại:

# systemctl restart mysqld.service # cho hệ thống sử dụng systemd
# /etc/init.d/mysqld restart # cho hệ thống sử dụng init

Đăng nhập với user root

# mysql -u root -p

Bạn cũng có thể đăng nhập với những user khác bằng cách thay root bằng tên user, sau đó gõ mật khẩu để đăng nhập. Nếu thành công sẽ có console tương tự bên dưới:

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 195 

Server version: 5.5.31-0+wheezy1 (Debian) 

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. 

Oracle is a registered trademark of Oracle Corporation and/or its affiliates. 
Other names may be trademarks of their respective owners. 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Tổng hợp câu lệnh MySQL cơ bản

Tạo database itfromzero

mysql> create database itfromzero;
Query OK, 1 row affected (0.02 sec) 

mysql>

Sau khi tạo xong bạn dùng lệnh show databases; để liệt kê các database hiện có

mysql> show databases; 
+--------------------+
| Database           | 
+--------------------+ 
| information_schema | 
| mysql              | 
| performance_schema | 
| itfromzero         |
| test               | 
+--------------------+ 
9 rows in set (0.00 sec) 
mysql>

Chọn Database

Để chọn database bạn dùng lệnh sau:

mysql> use itfromzero;
Database changed
mysql>

Tạo  Tables

Mình sẽ tạo bảng tên member bằng lệnh sau:

mysql> CREATE TABLE member (
    -> id Int(3), 
    -> first_name Varchar (15), 
    -> email Varchar(20) 
    -> ); 
Query OK, 0 rows affected (0.08 sec) 
mysql>

Kiểm tra bảng vừa tạo bằng lệnh show tables;

mysql> show tables; 
+----------------------+ 
| Tables_in_itfromzero | 
+----------------------+ 
| member               | 
+----------------------+ 

1 row in set (0.00 sec) 

mysql>

Bạn có thể xem các cột đã tạo bằng lệnh:

mysql> show columns from member; 

+------------+-------------+------+-----+---------+-------+ 
| Field      | Type        | Null | Key | Default | Extra | 
+------------+-------------+------+-----+---------+-------+ 
| id         | int(3)      | YES  |     | NULL    |       | 
| first_name | varchar(15) | YES  |     | NULL    |       | 
| email      | varchar(20) | YES  |     | NULL    |       | 
+------------+-------------+------+-----+---------+-------+ 
3 rows in set (0.00 sec)

mysql>

Thêm cột vào database

Bạn có thể thêm cột last_name vào giữa  first_name và email như sau:

mysql> ALTER TABLE member ADD last_name varchar (20) AFTER first_name; 
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

Kiểm tra lại:

mysql> show columns from member; 

+------------+-------------+------+-----+---------+-------+ 
| Field      | Type        | Null | Key | Default | Extra | 
+------------+-------------+------+-----+---------+-------+ 
| id         | int(3)      | YES  |     | NULL    |       | 
| first_name | varchar(15) | YES  |     | NULL    |       | 
| last_name  | varchar(20) | YES  |     | NULL    |       | 
| email      | varchar(20) | YES  |     | NULL    |       | 
+------------+-------------+------+-----+---------+-------+ 

4 rows in set (0.00 sec) 

mysql>

Hoặc có thể thêm cột country vào sau email:

mysql> ALTER TABLE member ADD country varchar (15) AFTER email; 
Query OK, 0 rows affected (0.16 sec) 
Records: 0  Duplicates: 0  Warnings: 0 

mysql>

Kiểm tra lại:

mysql> show columns from minttec; 

+------------+-------------+------+-----+---------+-------+ 
| Field      | Type        | Null | Key | Default | Extra | 
+------------+-------------+------+-----+---------+-------+
| id         | int(3)      | YES  |     | NULL    |       | 
| first_name | varchar(15) | YES  |     | NULL    |       | 
| last_name  | varchar(20) | YES  |     | NULL    |       | 
| email      | varchar(20) | YES  |     | NULL    |       | 
| country    | varchar(15) | YES  |     | NULL    |       | 
+------------+-------------+------+-----+---------+-------+
5 rows in set (0.00 sec) 

mysql>

Insert giá trị vào các trường dữ liệu

Ví dụ insert vào bảng member với thông tin 1 member như sau:

mysql> INSERT INTO member VALUES ('1' , 'Ravi' , 'Saive' , '[email protected]' , 'India' );
Query OK, 1 row affected (0.02 sec) 

mysql>

Hoặc có thể insert nhiều member cùng một lúc:

mysql> INSERT INTO member VALUES ('2' , 'Narad' , 'Shrestha' , '[email protected]' , 'India' ), ('3' , 'user' , 'singh' , '[email protected]' , 'Aus' ), ('4' , 'itfromzero' , '[dot]com' , '[email protected]' , 'VN' );
Query OK, 3 rows affected (0.05 sec) 
Records: 3  Duplicates: 0  Warnings: 0

Kiểm tra lại bảng vừa insert:

mysql> select * from member; 
+------+---------------+-----------+----------------------+---------+ 
| id   | first_name    | last_name | email                | country | 
+------+---------------+-----------+----------------------+---------+ 
|    1 | Ravi	       | Saive     | [email protected]    | India   | 
|    2 | Narad         | Shrestha  | [email protected]        | India   | 
|    3 | user          | singh     | [email protected]         | Aus     | 
|    4 | itfromzero    | [dot]com  | [email protected] | VN      | 
+------+---------------+-----------+----------------------+---------+ 

4 rows in set (0.00 sec)

mysql>

Xóa dữ liệu trong MySQL

Ví dụ xóa từ bảng member thành viên có id = 3

mysql> DELETE FROM member WHERE id = 3;

Query OK, 1 row affected (0.02 sec)

Kiểm tra lại bảng

mysql> select * from member;

+------+------------+-----------+----------------------+---------+ 
| id   | first_name | last_name | email                | country | 
+------+------------+-----------+----------------------+---------+
|    1 | Ravi       | Saive     | [email protected]    | India   | 
|    2 | Narad      | Shrestha  | [email protected]        | India   | 
|    4 | itfromzero | [dot]com  | [email protected] | VN      | 
+------+------------+-----------+----------------------+---------+
3 rows in set (0.00 sec)

Cập nhật giá trị

Ví dụ chỉnh sửa thông tin của member có id=4

mysql> UPDATE member SET id = 3 WHERE first_name = 'itfromzero'; 
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql>

Lưu ý: Bạn nên sử dụng nhiều điều kiện bằng câu lệnh sau:

mysql> UPDATE member SET id = 6 WHERE first_name = itfromzero' AND last_name = '[dot]com'; 
Query OK, 1 row affected (0.03 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql>

Xóa cột trong MySQL

Ví dụ xóa cột country:

mysql> ALTER TABLE member drop country; 
Query OK, 3 rows affected (0.15 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql>

Kiểm tra lại bảng:

mysql> select * from member; 

+------+---------------+-----------+----------------------+ 
| id   | first_name    | last_name | email                | 
+------+---------------+-----------+----------------------+ 
|    1 | Ravi          | Saive     | [email protected]    | 
|    2 | Narad         | Shrestha  | [email protected]        | 
|    6 | itfromzero    | [dot]com  | [email protected] | 
+------+---------------+-----------+----------------------+
3 rows in set (0.00 sec) 

mysql>

Đổi tên bảng

Ví dụ đổi tên bảng member thành member_tbl

mysql> RENAME TABLE member TO member_tbl; 
Query OK, 0 rows affected (0.03 sec)

mysql>

Liệt kê tất cả bảng

Sử dụng lệnh show tables để liệt kê:

mysql> show tables; 

+----------------------+ 
| Tables_in_itfromzero | 
+----------------------+ 
| member_tbl           | 
+----------------------+
1 row in set (0.00 sec) 

mysql>

Xóa Database

Ví dụ xóa Database itfromzero:

mysql> drop database itfromzero; 
Query OK, 1 row affected (0.02 sec)

Liệt kê danh sách database:

mysql> show databases; 

+--------------------+ 
| Database           | 
+--------------------+ 
| information_schema |  
| mysql              | 
| performance_schema | 
| phpmyadmin         |  
| test               | 
+--------------------+

7 rows in set (0.00 sec) 
mysql>

Trên đây là một số câu lệnh mysql cơ bản trên Linux. Chúc các bạn thành công.

Share: