一.数据库的操作
(1).查看数据库
SHOW DATABASES;
示例:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
3 rows in set (0.017 sec)
(2).创建数据库
CREATE DATABASE [IF NOT EXISTS] [数据库名];
//数据库名只能是数字、字母、下划线,IF NOT EXISTS为可选选项,如果数据库存在则不再创建
示例:
MariaDB [(none)]> CREATE DATABASE test;
Query OK, 1 row affected (0.001 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lee |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.000 sec)
(3).使用数据库
USE [数据库名];
示例:
MariaDB [(none)]> USE test;
Database changed
(4).删除数据库
DROP DATABASE [IF EXISTS] [数据库名];
示例:
MariaDB [test]> DROP DATABASE test;
Query OK, 0 rows affected (0.032 sec)
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| lee |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.000 sec)
二.表的操作
(1).查看数据库中所有的表
SHOW TABLES;
示例:
MariaDB [lee]> SHOW TABLES;
+---------------+
| Tables_in_lee |
+---------------+
| stu |
+---------------+
1 row in set (0.001 sec)
(2).查看表结构
DESC [数据库名];
示例:
MariaDB [lee]> DESC stu;
+---------+---------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+---------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| name | varchar(8) | YES | | NULL | |
| birth | date | YES | | NULL | |
| math | decimal(10,0) | YES | | NULL | |
| english | decimal(10,0) | YES | | NULL | |
+---------+---------------+------+-----+---------+-------+
6 rows in set (0.021 sec)
(3).删除表
DROP TABLE [IF EXISTS] [数据库名];
示例:
MariaDB [lee]> DROP TABLE stu;
Query OK, 0 rows affected (0.006 sec)
MariaDB [lee]> SHOW TABLES;
Empty set (0.001 sec)
三.表的增删查改(CRUD)
CRUD即增加(Create)、查询(Retrieve)、更新(Update)、删除(Delete)四个单词的首字母缩写,是表的四种基本操作。
表的插入:
(1).全列插入
INSERT INTO [表名] VALUES(参数1, 参数2, 参数3......);
示例:
INSERT INTO stu VALUES(2, 15, "李梅", 20200811, 88.5, 90.5);
MariaDB [lee]> select * from stu;
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 2 | 15 | 李梅 | 2020-08-11 | 88.50 | 90.50 |
+------+------+--------+------------+-------+---------+
1 row in set (0.000 sec)
(2).指定列插入
INSERT INTO [表名](列1,列2,列3.......) values(参数1, 参数2, 参数3......);
示例:
INSERT INTO stu(id, age, name) values(3, 16, "李华");
MariaDB [lee]> select * from stu;
+------+------+--------+-------+------+---------+
| id | age | name | birth | math | english |
+------+------+--------+-------+------+---------+
| 3 | 16 | 李华 | NULL | NULL | NULL |
+------+------+--------+-------+------+---------+
1 row in set (0.001 sec)
表的查询:
(1).全列查询
SELECT * FROM [表名];
示例:
SELECT * FROM stu;
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 1 | 11 | 李华 | 2020-08-12 | 76.50 | 87.50 |
| 2 | 15 | 李梅 | 2020-08-13 | 88.50 | 90.50 |
| 3 | 18 | 张三 | 2020-08-14 | 84.50 | 70.50 |
| 4 | 16 | 李四 | 2020-08-15 | 75.60 | 40.80 |
| 5 | 14 | 王五 | 2020-08-16 | 48.80 | 91.90 |
| 6 | 13 | 马六 | 2020-08-17 | 85.90 | 95.40 |
+------+------+--------+------------+-------+---------+
(2).指定列查询
SELECT 列1,列2,列3..... FROM [表名];
示例:
SELECT id, age, name FROM stu;
+------+------+--------+
| id | age | name |
+------+------+--------+
| 1 | 11 | 李华 |
| 2 | 15 | 李梅 |
| 3 | 18 | 张三 |
| 4 | 16 | 李四 |
| 5 | 14 | 王五 |
| 6 | 13 | 马六 |
+------+------+--------+
(3).条件查询
SELECT * FROM [表名] WHERE [条件];
示例:
SELECT * FROM stu where name = "李华";
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 1 | 11 | 李华 | 2020-08-12 | 76.50 | 87.50 |
+------+------+--------+------------+-------+---------+
(4).分页查询
SELECT * FROM [表名] LIMIT [每页条数] OFFSET [偏移条数];
示例:
SELECT * FROM stu LIMIT 2;//限制每页两条
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 1 | 11 | 李华 | 2020-08-12 | 76.50 | 87.50 |
| 2 | 15 | 李梅 | 2020-08-13 | 88.50 | 90.50 |
+------+------+--------+------------+-------+---------+
SELECT * FROM stu LIMIT 2 OFFSET 2;//偏移两条,也就是查询第二页
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 3 | 18 | 张三 | 2020-08-14 | 84.50 | 70.50 |
| 4 | 16 | 李四 | 2020-08-15 | 75.60 | 40.80 |
+------+------+--------+------------+-------+---------+
(5).去重查询(distinct)
SELECT DISTINCT * FROM [表名];
示例:
SELECT DISTINCT * FROM stu;
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 1 | 11 | 李华 | 2020-08-12 | 76.50 | 87.50 |
| 2 | 15 | 李梅 | 2020-08-13 | 88.50 | 90.50 |
| 3 | 18 | 张三 | 2020-08-14 | 84.50 | 70.50 |
| 4 | 16 | 李四 | 2020-08-15 | 75.60 | 40.80 |
| 5 | 14 | 王五 | 2020-08-16 | 48.80 | 91.90 |
| 6 | 13 | 马六 | 2020-08-17 | 85.90 | 95.40 |
+------+------+--------+------------+-------+---------+
(6).排序 order by
SELECT * FROM [表名] ORDER BY [排序依赖项] [DESC/ASC];
//DESC为降序排序,ASC为升序排序,默认为ASC
示例:
SELECT * FROM stu ORDER BY math DESC;//按数学成绩降序
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 2 | 15 | 李梅 | 2020-08-13 | 88.50 | 90.50 |
| 6 | 13 | 马六 | 2020-08-17 | 85.90 | 95.40 |
| 3 | 18 | 张三 | 2020-08-14 | 84.50 | 70.50 |
| 1 | 11 | 李华 | 2020-08-12 | 76.50 | 87.50 |
| 4 | 16 | 李四 | 2020-08-15 | 75.60 | 40.80 |
| 5 | 14 | 王五 | 2020-08-16 | 48.80 | 91.90 |
+------+------+--------+------------+-------+---------+
(7).别名
SELECT 列a,列b...... AS [别名] FROM [表名];
示例:
SELECT name, math + english AS total FROM stu;//获取每个人的总分
+--------+--------+
| name | total |
+--------+--------+
| 李华 | 164.00 |
| 李梅 | 179.00 |
| 张三 | 155.00 |
| 李四 | 116.40 |
| 王五 | 140.70 |
| 马六 | 181.30 |
+--------+--------+
表的修改:
UPDATE [表名] SET [修改项] = [修改结果];
示例:
UPDATE stu SET math = 99.5 WHERE name = "李梅";
MariaDB [lee]> select * from stu;
+------+------+--------+------------+-------+---------+
| id | age | name | birth | math | english |
+------+------+--------+------------+-------+---------+
| 2 | 15 | 李梅 | 2020-08-11 | 99.50 | 90.50 |
+------+------+--------+------------+-------+---------+
表的删除:
DELETE FROM [表名] 条件语句;
示例:
DELETE FROM stu where name = "李梅";
MariaDB [lee]> select * from stu;
Empty set (0.000 sec)