前言

MYSQL是最流行的关系型数据库管理系统。

目录


基本入门

连接MYSQL

我们先连接mysql服务器:

mysql -u root -p
Enter password: *****

数据库操作

  • 查看数据库
mysql> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| test               |
+--------------------+
4 rows in set (0.00 sec)

  • 选择数据库
mysql> use mysql;

Database changed

  • 创建数据库
mysql> create database test;

Query OK, 1 row affected (0.00 sec)

  • 删除数据库
mysql> drop database test;

Query OK, 0 rows affected (0.00 sec)

MYSQL 数据类型

MySql支持多种类型,大致可以分为三类: 数值、日期/时间和字符串类型。

数据表操作

  • 创建数据表
mysql> CREATE TABLE IF NOT EXISTS `runoob_tbl`(
   `runoob_id` INT UNSIGNED AUTO_INCREMENT,
   `runoob_title` VARCHAR(100) NOT NULL,
   `runoob_author` VARCHAR(40) NOT NULL,
   `submission_date` DATE,
   PRIMARY KEY ( `runoob_id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;

  • 删除数据表
mysql> DROP TABLE runoob_tbl;

数据操作

  • 插入数据
insert into arms (name, img_src, type_id, descript, remarks) values ('碎 片手雷', 'www', 8, '手雷手雷', '哈哈,炸死你');

Query OK, 1 row affected (0.00 sec)

  • 查询数据
select * from arms where id = 1;

  • 更新数据
update arms set name = '手雷1' where id = 30;

  • 删除数据
delete from arms where id = 30;

  • like查询
    • %a // 以A结尾的数据
    • a% // 以a开始的数据
    • %a% // 包含a的数据
    • _a_ // 三位且中间字母是a的数据
    • _a // 两位且结尾字母是a的数据
    • a_ // 两位且开头字母是a的数据
select * from arms where img_src like '%5200%';

  • union: 用于连接两个或者两个以上的数据表
SELECT country FROM Websites
UNION
SELECT country FROM apps
ORDER BY country;

  • order by
select * from arms order by id asc limit 0, 20;