基础命令
MYSQL解释方式
毗连数据库
退出数据库毗连
使用exit;命令可以退出毗连
查询MYSQL版本
- mysql> select version();
- +-----------+
- | version() |
- +-----------+
- | 8.0.27 |
- +-----------+
- 1 row in set (0.00 sec)
复制代码 查看全部数据库
使用数据库
如果想要操作数据库,须要使用use 数据库名;来选择要操作的数据库
查看所选择数据库的全部表
查看表的详细信息
使用describe 表名;可以查看表中的字段信息
- mysql> describe user;
- +--------------+---------------+------+-----+-------------------+-----------------------------------------------+
- | Field | Type | Null | Key | Default | Extra |
- +--------------+---------------+------+-----+-------------------+-----------------------------------------------+
- | id | bigint | NO | PRI | NULL | auto_increment |
- | userName | varchar(256) | YES | | NULL | |
- | userAccount | varchar(256) | NO | UNI | NULL | |
- | userAvatar | varchar(1024) | YES | | NULL | |
- | gender | tinyint | YES | | NULL | |
- | userRole | varchar(256) | NO | | user | |
- | userPassword | varchar(512) | NO | | NULL | |
- | accessKey | varchar(512) | YES | | NULL | |
- | secretKey | varchar(512) | YES | | NULL | |
- | createTime | datetime | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
- | updateTime | datetime | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
- | isDelete | tinyint | NO | | 0 | |
- +--------------+---------------+------+-----+-------------------+-----------------------------------------------+
- 12 rows in set (0.00 sec)
复制代码 创建数据库
- create database [if not exists] 数据库名称 character set 字符编码;
- mysql> create database practice character set utf8;
- Query OK, 1 row affected, 1 warning (0.01 sec)
- -- 已存在的数据库不能重复创建,不然会报错
- mysql> create database practice character set utf8;
- ERROR 1007 (HY000): Can't create database 'practice'; database exists
- -- 使用if not exists,可以判断数据库不存在的时候才创建数据库,这样就不会报错
- mysql> create database if not exists practice character set utf8;
- Query OK, 1 row affected, 2 warnings (0.01 sec)
复制代码 如果数据库名字有短横杆,用``包住,否则可能会报错
- create database `second-hand-market` character set utf8mb4;
复制代码 查看创建数据库的语句
- mysql> show create database practice;
- +----------+------------------------------------------------------------------------------------------------------+
- | Database | Create Database |
- +----------+------------------------------------------------------------------------------------------------------+
- | practice | CREATE DATABASE `practice` /*!40100 DEFAULT CHARACTER SET utf8 */ /*!80016 DEFAULT ENCRYPTION='N' */ |
- +----------+------------------------------------------------------------------------------------------------------+
- 1 row in set (0.00 sec)
复制代码 修改用户密码
数据表管理
创建数据表
如:
- CREATE TABLE IF NOT EXISTS `student` (
- `id` BIGINT NOT NULL auto_increment COMMENT '学号' PRIMARY KEY,
- `name` VARCHAR ( 30 ) NOT NULL DEFAULT '匿名' COMMENT '姓名'
- ) COMMENT '学生表'
复制代码 查看创建数据表的语句
- mysql> use practice;
- Database changed
- mysql> show create table student;
- +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| Table | Create Table
- |+---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+| student | CREATE TABLE `student` (
- `id` bigint NOT NULL AUTO_INCREMENT COMMENT '学号',
- `name` varchar(30) NOT NULL DEFAULT '匿名' COMMENT '姓名',
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='学生表' |
- +---------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)
复制代码 查看表的结构
- mysql> desc student;
- +-------+-------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+-------------+------+-----+---------+----------------+
- | id | bigint | NO | PRI | NULL | auto_increment |
- | name | varchar(30) | NO | | 匿名 | |
- +-------+-------------+------+-----+---------+----------------+
- 2 rows in set (0.00 sec)
复制代码 修改表名
alter table 旧表名 rename as 新表名;
- mysql> alter table student rename as teacher;
- Query OK, 0 rows affected (0.02 sec)
复制代码 删除表
drop table if exists 表名;
- mysql> show tables;
- +--------------------+| Tables_in_practice |+--------------------+| teacher |+--------------------+1 row in set (0.00 sec)mysql> drop table if exists teacher;Query OK, 0 rows affected (0.02 sec)mysql> show tables;
- Empty set (0.00 sec)
复制代码 数据表字段管理
给指定表增长字段
alter table 表名 add 字段名 列属性;
- mysql> alter table teacher add gender bigint;
- Query OK, 0 rows affected (0.02 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> desc teacher;
- +--------+-------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +--------+-------------+------+-----+---------+----------------+
- | id | bigint | NO | PRI | NULL | auto_increment |
- | name | varchar(30) | NO | | 匿名 | |
- | gender | bigint | YES | | NULL | |
- +--------+-------------+------+-----+---------+----------------+
- 3 rows in set (0.00 sec)
- mysql> alter table teacher add age int(11);
- Query OK, 0 rows affected, 1 warning (0.02 sec)
- Records: 0 Duplicates: 0 Warnings: 1
- mysql> desc teacher;
- +--------+-------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +--------+-------------+------+-----+---------+----------------+
- | id | bigint | NO | PRI | NULL | auto_increment |
- | name | varchar(30) | NO | | 匿名 | |
- | gender | bigint | YES | | NULL | |
- | age | int | YES | | NULL | |
- +--------+-------------+------+-----+---------+----------------+
- 4 rows in set (0.00 sec)
复制代码 修改指定表的字段
修改列属性
alter table 表名 modify 字段名 列属性;
- mysql> alter table teacher modify age varchar(11);
- Query OK, 0 rows affected (0.07 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> desc teacher;
- +--------+-------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +--------+-------------+------+-----+---------+----------------+
- | id | bigint | NO | PRI | NULL | auto_increment |
- | name | varchar(30) | NO | | 匿名 | |
- | gender | bigint | YES | | NULL | |
- | age | varchar(11) | YES | | NULL | |
- +--------+-------------+------+-----+---------+----------------+
- 4 rows in set (0.00 sec)
复制代码 同时修改字段名和字段属性
alter table 表名 change 旧字段名 新字段名 列属性;
- mysql> desc teacher;
- +--------+-------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +--------+-------------+------+-----+---------+----------------+
- | id | bigint | NO | PRI | NULL | auto_increment |
- | name | varchar(30) | NO | | 匿名 | |
- | gender | bigint | YES | | NULL | |
- | age | varchar(11) | YES | | NULL | |
- +--------+-------------+------+-----+---------+----------------+
- 4 rows in set (0.00 sec)
- mysql> alter table teacher change age age1 int(2);
- Query OK, 0 rows affected, 1 warning (0.07 sec)
- Records: 0 Duplicates: 0 Warnings: 1
- mysql> desc teacher;
- +--------+-------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +--------+-------------+------+-----+---------+----------------+
- | id | bigint | NO | PRI | NULL | auto_increment |
- | name | varchar(30) | NO | | 匿名 | |
- | gender | bigint | YES | | NULL | |
- | age1 | int | YES | | NULL | |
- +--------+-------------+------+-----+---------+----------------+
- 4 rows in set (0.00 sec)
复制代码 删除指定表的字段
- mysql> desc teacher;
- +--------+-------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +--------+-------------+------+-----+---------+----------------+
- | id | bigint | NO | PRI | NULL | auto_increment |
- | name | varchar(30) | NO | | 匿名 | |
- | gender | bigint | YES | | NULL | |
- | age1 | int | YES | | NULL | |
- +--------+-------------+------+-----+---------+----------------+
- 4 rows in set (0.00 sec)
- mysql> alter table teacher drop gender;
- Query OK, 0 rows affected (0.06 sec)
- Records: 0 Duplicates: 0 Warnings: 0
- mysql> desc teacher;
- +-------+-------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+-------------+------+-----+---------+----------------+
- | id | bigint | NO | PRI | NULL | auto_increment |
- | name | varchar(30) | NO | | 匿名 | |
- | age1 | int | YES | | NULL | |
- +-------+-------------+------+-----+---------+----------------+
- 3 rows in set (0.00 sec)
复制代码 给表添加外键
创建表的时间增长外键
- -- 创建专业表
- CREATE TABLE IF NOT EXISTS `major` (
- `id` BIGINT NOT NULL auto_increment COMMENT '专业id' PRIMARY KEY,
- `name` VARCHAR ( 30 ) NOT NULL COMMENT '专业名称'
- ) COMMENT '专业表'
- -- 创建学生表,并将学生表的major_id声明为外键,引用专业表的id字段
- CREATE TABLE IF NOT EXISTS `student` (
- `id` BIGINT NOT NULL auto_increment COMMENT '学号' PRIMARY KEY,
- `name` VARCHAR ( 30 ) NOT NULL DEFAULT '匿名' COMMENT '姓名',
- `major_id` BIGINT NOT NULL COMMENT '专业id',
- key `FK_majorId` (`major_id`),
- CONSTRAINT `FK_majorId` FOREIGN KEY (`major_id`) REFERENCES `major`(`id`)
- ) COMMENT '学生表'
复制代码
给已有表添加外键
- ALTER TABLE 表名 ADD CONSTRAINT 约束名 FOREIGN KEY(作为外键的列的字段名) REFERENCES 引用哪个表(哪个字段)
复制代码 【案例】
- ALTER TABLE `student` ADD CONSTRAINT `FK_majorId` FOREIGN KEY(`major_id`) REFERENCES `major`(`id`);
复制代码 外键使用建议
当使用了外键时,如果须要删除被引用表的记录,须要先删除引用表的记录。如上面的例子所示,如果须要删除一个专业,须要先删除这个专业所对应的学生。
建议:使用外键束缚会让开发者很痛楚,测试数据不方便,建议不使用外键
数据插入语句 INSERT
数据插入
- insert into `表名` (`字段名1`,`字段名2`) values('字段值1','字段值2');
复制代码 一次性插入多条数据
- insert into `表名` (`字段名1`,`字段名2`) values('字段值11','字段值12'),('字段值21','字段值22'),('字段值31','字段值32');
复制代码 案例
- mysql> desc student;
- +----------+-------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +----------+-------------+------+-----+---------+----------------+
- | id | bigint | NO | PRI | NULL | auto_increment |
- | name | varchar(30) | NO | | 匿名 | |
- | major_id | bigint | NO | MUL | NULL | |
- +----------+-------------+------+-----+---------+----------------+
- 3 rows in set (0.00 sec)
- mysql> desc major;
- +-------+-------------+------+-----+---------+----------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------+-------------+------+-----+---------+----------------+
- | id | bigint | NO | PRI | NULL | auto_increment |
- | name | varchar(30) | NO | | NULL | |
- +-------+-------------+------+-----+---------+----------------+
- 2 rows in set (0.00 sec)
- -- insert into `表名` (`字段名`) values('字段值');
- mysql> insert into `major` (`name`) values('计算机科学');
- Query OK, 1 row affected (0.01 sec)
- mysql> insert into `major` (`name`) values('工业工程');
- Query OK, 1 row affected (0.01 sec)
- mysql> select * from major;
- +----+-----------------+
- | id | name |
- +----+-----------------+
- | 1 | 计算机科学 |
- | 2 | 工业工程 |
- +----+-----------------+
- 2 rows in set (0.00 sec)
- -- 插入多条数据
- mysql> insert into `major` (`name`) values('自动化'),('机械工程'),('工业设计');
- Query OK, 3 rows affected (0.01 sec)
- Records: 3 Duplicates: 0 Warnings: 0
- mysql> select * from major;
- +----+-----------------+
- | id | name |
- +----+-----------------+
- | 1 | 计算机科学 |
- | 2 | 工业工程 |
- | 3 | 自动化 |
- | 4 | 机械工程 |
- | 5 | 工业设计 |
- +----+-----------------+
- 5 rows in set (0.00 sec)
- mysql> insert into `student` (`name`,`major_id`) values('小明','1');
- Query OK, 1 row affected (0.01 sec)
- mysql> select * from student;
- +----+--------+----------+
- | id | name | major_id |
- +----+--------+----------+
- | 1 | 小明 | 1 |
- +----+--------+----------+
- 1 row in set (0.00 sec)
- -- 插入数据的时候,不一定要给表的所有字段赋值,只赋值部分字段也可以,前提是省略的字段可以为空
- mysql> insert into `student` (`major_id`) values('1');
- Query OK, 1 row affected (0.01 sec)
- -- 插入数据的时候,没有写入学生的名字,MYSQL会自动填入默认值“匿名”
- mysql> select * from student;
- +----+--------+----------+
- | id | name | major_id |
- +----+--------+----------+
- | 1 | 小明 | 1 |
- | 2 | 匿名 | 1 |
- +----+--------+----------+
- 2 rows in set (0.00 sec)
复制代码 数据更新语句 UPDATE
没有指定匹配条件的话,会全部表的全部记录
- mysql> select * from major;
- +----+-----------------+
- | id | name |
- +----+-----------------+
- | 1 | 计算机科学 |
- | 2 | 工业工程 |
- | 3 | 自动化 |
- | 4 | 机械工程 |
- | 5 | 工业设计 |
- +----+-----------------+
- 5 rows in set (0.00 sec)
- mysql> update major set name='智能制造';
- Query OK, 5 rows affected (0.01 sec)
- Rows matched: 5 Changed: 5 Warnings: 0
- mysql> select * from major;
- +----+--------------+
- | id | name |
- +----+--------------+
- | 1 | 智能制造 |
- | 2 | 智能制造 |
- | 3 | 智能制造 |
- | 4 | 智能制造 |
- | 5 | 智能制造 |
- +----+--------------+
- 5 rows in set (0.00 sec)
复制代码 根据条件来修改记录
- mysql> update major set name='智能科学与技术' where id = 1;
- Query OK, 1 row affected (0.01 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> select * from major;
- +----+-----------------------+
- | id | name |
- +----+-----------------------+
- | 1 | 智能科学与技术 |
- | 2 | 智能制造 |
- | 3 | 智能制造 |
- | 4 | 智能制造 |
- | 5 | 智能制造 |
- +----+-----------------------+
- 5 rows in set (0.00 sec)
复制代码 一次性修改多个字段的值
- mysql> select * from student;
- +----+--------+----------+
- | id | name | major_id |
- +----+--------+----------+
- | 1 | 小明 | 1 |
- | 2 | 匿名 | 1 |
- +----+--------+----------+
- 2 rows in set (0.00 sec)
- mysql> update student set name='李华',major_id=2 where id = 1;
- Query OK, 1 row affected (0.01 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> select * from student;
- +----+--------+----------+
- | id | name | major_id |
- +----+--------+----------+
- | 1 | 李华 | 2 |
- | 2 | 匿名 | 1 |
- +----+--------+----------+
- 2 rows in set (0.00 sec)
复制代码 稍微高级一点的使用
- update load_product,product set load_product.product_id = product.id where load_product.code=product.code
复制代码 where条件
where条件可以使用的操作符
多个条件共同使用,使用and毗连,如where id=1 and name=‘李华’
- mysql> select id,name as 专业名称 from major;
- +----+-----------------------+
- | id | 专业名称 |
- +----+-----------------------+
- | 1 | 智能科学与技术 |
- | 2 | 智能制造 |
- | 3 | 智能制造 |
- | 4 | 智能制造 |
- | 5 | 智能制造 |
- +----+-----------------------+
- 5 rows in set (0.00 sec)
- mysql> select id,name as 专业名称 from major where name='智能制造';
- +----+--------------+
- | id | 专业名称 |
- +----+--------------+
- | 2 | 智能制造 |
- | 3 | 智能制造 |
- | 4 | 智能制造 |
- | 5 | 智能制造 |
- +----+--------------+
- 4 rows in set (0.00 sec)
- mysql> select id,name as 专业名称 from major where name!='智能制造';
- +----+-----------------------+
- | id | 专业名称 |
- +----+-----------------------+
- | 1 | 智能科学与技术 |
- +----+-----------------------+
- 1 row in set (0.00 sec)
- mysql> select id,name as 专业名称 from major where not name='智能制造';
- +----+-----------------------+
- | id | 专业名称 |
- +----+-----------------------+
- | 1 | 智能科学与技术 |
- +----+-----------------------+
- 1 row in set (0.00 sec)
- mysql> select id,name as 专业名称 from major where name='智能制造' and id =2;
- +----+--------------+
- | id | 专业名称 |
- +----+--------------+
- | 2 | 智能制造 |
- +----+--------------+
- 1 row in set (0.00 sec)
- mysql> select id,name as 专业名称 from major where id < 2;
- +----+-----------------------+
- | id | 专业名称 |
- +----+-----------------------+
- | 1 | 智能科学与技术 |
- +----+-----------------------+
- 1 row in set (0.00 sec)
- mysql> select id,name as 专业名称 from major where id >= 2;
- +----+--------------+
- | id | 专业名称 |
- +----+--------------+
- | 2 | 智能制造 |
- | 3 | 智能制造 |
- | 4 | 智能制造 |
- | 5 | 智能制造 |
- +----+--------------+
- 4 rows in set (0.00 sec)
- mysql> select id,name as 专业名称 from major where id >= 2 and id <= 4;
- +----+--------------+
- | id | 专业名称 |
- +----+--------------+
- | 2 | 智能制造 |
- | 3 | 智能制造 |
- | 4 | 智能制造 |
- +----+--------------+
- 3 rows in set (0.00 sec)
- mysql> select id,name as 专业名称 from major where id = 2 or id = 4;
- +----+--------------+
- | id | 专业名称 |
- +----+--------------+
- | 2 | 智能制造 |
- | 4 | 智能制造 |
- +----+--------------+
- 2 rows in set (0.00 sec)
复制代码 判空
固然,还可以用来判空大概非空,并不是直接=null,而是is null和is not null
- UPDATE sys_user
- SET avatar = concat( "https://hahaha/", FLOOR(RAND()*29+1), ".png" )
- WHERE
- avatar IS NULL
- OR avatar = "";
复制代码 and和or的使用
- SELECT
- *
- FROM
- chat
- WHERE
- ( from_who = "admin" AND to_who = "user1" )
- OR ( to_who = "admin" AND from_who = "user1" )
- ORDER BY
- create_time DESC;
复制代码
数据删除 DELETE
删除全部数据
使用delete删除全部数据,如果重启数据库,会出现如下现象
- InnoDB 自增列会从1开始(存在内存中,断电即失)
- MyISAM 自增列继承从上一个自增量开始(存在文件中,不会丢失)
删除指定数据
数据表清空 TRUNCATE
清空一个数据表的全部数据,自增id计数器重新恢复到1,表的结构和索引束缚不会改变
数据查询语言Data Query LANGUAGE(DQL) SELECT
查询一个表中的全部数据
查询指定字段的数据
- select 字段1,字段2,字段3 from 表名;
复制代码 注意:能查询部门字段的,千万不要查询*,这样会增长查询时间
给字段取别名
- mysql> select name from major;
- +-----------------------+
- | name |
- +-----------------------+
- | 智能科学与技术 |
- | 智能制造 |
- | 智能制造 |
- | 智能制造 |
- | 智能制造 |
- +-----------------------+
- 5 rows in set (0.00 sec)
- mysql> select name as 专业名称 from major;
- +-----------------------+
- | 专业名称 |
- +-----------------------+
- | 智能科学与技术 |
- | 智能制造 |
- | 智能制造 |
- | 智能制造 |
- | 智能制造 |
- +-----------------------+
- 5 rows in set (0.00 sec)
复制代码 给表取别名
- mysql> select student.id as 学号,student.name as 姓名,major.name as 专业 from student left join major on student.major_id = major.id;
- +--------+--------+-----------------------+
- | 学号 | 姓名 | 专业 |
- +--------+--------+-----------------------+
- | 1 | 李华 | 智能制造 |
- | 2 | 匿名 | 智能科学与技术 |
- +--------+--------+-----------------------+
- 2 rows in set (0.00 sec)
- mysql> select s.id as 学号,s.name as 姓名,m.name as 专业 from student as s left join major as m on s.major_id = m.id;
- +--------+--------+-----------------------+
- | 学号 | 姓名 | 专业 |
- +--------+--------+-----------------------+
- | 1 | 李华 | 智能制造 |
- | 2 | 匿名 | 智能科学与技术 |
- +--------+--------+-----------------------+
- 2 rows in set (0.00 sec)
复制代码 取别名可以更方便开发者编写sql语句
字符串拼接
- mysql> select concat('专业名称:',name) from major;
- +--------------------------------------+
- | concat('专业名称:',name) |
- +--------------------------------------+
- | 专业名称:智能科学与技术 |
- | 专业名称:智能制造 |
- | 专业名称:智能制造 |
- | 专业名称:智能制造 |
- | 专业名称:智能制造 |
- +--------------------------------------+
- 5 rows in set (0.00 sec)
复制代码 查询效果数据去重
重复的数据只体现一条
- mysql> select name as 专业名称 from major;
- +-----------------------+
- | 专业名称 |
- +-----------------------+
- | 智能科学与技术 |
- | 智能制造 |
- | 智能制造 |
- | 智能制造 |
- | 智能制造 |
- +-----------------------+
- 5 rows in set (0.00 sec)
- mysql> select distinct name as 专业名称 from major;
- +-----------------------+
- | 专业名称 |
- +-----------------------+
- | 智能科学与技术 |
- | 智能制造 |
- +-----------------------+
- 2 rows in set (0.00 sec)
复制代码 查询时增长计算
- mysql> select id,name as 专业名称 from major;
- +----+-----------------------+
- | id | 专业名称 |
- +----+-----------------------+
- | 1 | 智能科学与技术 |
- | 2 | 智能制造 |
- | 3 | 智能制造 |
- | 4 | 智能制造 |
- | 5 | 智能制造 |
- +----+-----------------------+
- 5 rows in set (0.00 sec)
- -- 将所有数据的 id 增加 1
- mysql> select id+1,name as 专业名称 from major;
- +------+-----------------------+
- | id+1 | 专业名称 |
- +------+-----------------------+
- | 2 | 智能科学与技术 |
- | 3 | 智能制造 |
- | 4 | 智能制造 |
- | 5 | 智能制造 |
- | 6 | 智能制造 |
- +------+-----------------------+
- 5 rows in set (0.00 sec)
复制代码 含糊查询
毗连查询
七种毗连方式
https://blog.csdn.net/laodanqiu/article/details/131233741
自毗连
用途:一个表同时存储了父类数据和子类数据,如省市区表同时存储了中国的全部省、市、区的数据,希望查询出广东省下面的全部城市
- mysql> desc province_city_region;
- +-------------+-------------+------+-----+-------------------+-----------------------------------------------+
- | Field | Type | Null | Key | Default | Extra |
- +-------------+-------------+------+-----+-------------------+-----------------------------------------------+
- | id | bigint | NO | PRI | NULL | auto_increment |
- | create_time | datetime | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
- | update_time | datetime | NO | | CURRENT_TIMESTAMP | DEFAULT_GENERATED on update CURRENT_TIMESTAMP |
- | is_deleted | tinyint | YES | | 0 | |
- | name | varchar(50) | YES | | NULL | |
- | type | tinyint | YES | | NULL | |
- | parent_id | bigint | YES | | NULL | |
- +-------------+-------------+------+-----+-------------------+-----------------------------------------------+
- 7 rows in set (0.00 sec)
- mysql> select a.id,a.name as 市名称,b.name as 省名称 from province_city_region as a,province_city_region as b where b.name="广东省" and a.parent_id=b.id;
- +------+-----------+-----------+
- | id | 市名称 | 省名称 |
- +------+-----------+-----------+
- | 4792 | 茂名市 | 广东省 |
- | 4798 | 湛江市 | 广东省 |
- | 4808 | 东莞市 | 广东省 |
- | 4809 | 江门市 | 广东省 |
- | 4817 | 清远市 | 广东省 |
- | 4826 | 佛山市 | 广东省 |
- | 4832 | 阳江市 | 广东省 |
- | 4837 | 汕头市 | 广东省 |
- | 4845 | 河源市 | 广东省 |
- | 4852 | 珠海市 | 广东省 |
- | 4856 | 汕尾市 | 广东省 |
- | 4861 | 深圳市 | 广东省 |
- | 4868 | 梅州市 | 广东省 |
- | 4877 | 揭阳市 | 广东省 |
- | 4883 | 韶关市 | 广东省 |
- | 4894 | 惠州市 | 广东省 |
- | 4900 | 潮州市 | 广东省 |
- | 4904 | 广州市 | 广东省 |
- | 4916 | 肇庆市 | 广东省 |
- | 4925 | 中山市 | 广东省 |
- | 4926 | 云浮市 | 广东省 |
- +------+-----------+-----------+
- 21 rows in set (0.00 sec)
复制代码 分页
【查询语句】limit 数据索引起始值 数据量的巨细
limit 0,5:查询的是第0,1,2,3,4条数据
limit 2,5:查询的是第2,3,4,5,6条数据
排序
【查询语句】order by 字段 ASC / DESC
- mysql> select * from province_city_region order by id asc limit 0,10;
- +------+---------------------+---------------------+------------+-----------+------+-----------+
- | id | create_time | update_time | is_deleted | name | type | parent_id |
- +------+---------------------+---------------------+------------+-----------+------+-----------+
- | 2928 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 山东省 | 0 | 0 |
- | 2929 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 莱芜市 | 1 | 2928 |
- | 2930 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 莱城区 | 2 | 2929 |
- | 2931 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 钢城区 | 2 | 2929 |
- | 2932 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 济南市 | 1 | 2928 |
- | 2933 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 历城区 | 2 | 2932 |
- | 2934 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 历下区 | 2 | 2932 |
- | 2935 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 长清区 | 2 | 2932 |
- | 2936 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 平阴县 | 2 | 2932 |
- | 2937 | 2023-02-10 20:38:54 | 2023-02-10 20:38:54 | 0 | 章丘市 | 1 | 2932 |
- +------+---------------------+---------------------+------------+-----------+------+-----------+
- 10 rows in set (0.00 sec)
- mysql> select * from province_city_region order by id desc limit 0,10;
- +------+---------------------+---------------------+------------+--------------------------+------+-----------+
- | id | create_time | update_time | is_deleted | name | type | parent_id |
- +------+---------------------+---------------------+------------+--------------------------+------+-----------+
- | 5851 | 2023-02-10 20:39:11 | 2023-02-10 20:39:11 | 0 | 天峨县 | 2 | 5840 |
- | 5850 | 2023-02-10 20:39:11 | 2023-02-10 20:39:11 | 0 | 凤山县 | 2 | 5840 |
- | 5849 | 2023-02-10 20:39:11 | 2023-02-10 20:39:11 | 0 | 南丹县 | 2 | 5840 |
- | 5848 | 2023-02-10 20:39:11 | 2023-02-10 20:39:11 | 0 | 环江毛南族自治县 | 2 | 5840 |
- | 5847 | 2023-02-10 20:39:11 | 2023-02-10 20:39:11 | 0 | 巴马瑶族自治县 | 2 | 5840 |
- | 5846 | 2023-02-10 20:39:11 | 2023-02-10 20:39:10 | 0 | 东兰县 | 2 | 5840 |
- | 5845 | 2023-02-10 20:39:11 | 2023-02-10 20:39:10 | 0 | 金城江区 | 2 | 5840 |
- | 5844 | 2023-02-10 20:39:11 | 2023-02-10 20:39:10 | 0 | 罗城仫佬族自治县 | 2 | 5840 |
- | 5843 | 2023-02-10 20:39:11 | 2023-02-10 20:39:10 | 0 | 宜州市 | 1 | 5840 |
- | 5842 | 2023-02-10 20:39:11 | 2023-02-10 20:39:10 | 0 | 都安瑶族自治县 | 2 | 5840 |
- +------+---------------------+---------------------+------------+--------------------------+------+-----------+
- 10 rows in set (0.00 sec)
复制代码 子查询
注意:子查询的效率高于连表查询
查询常用函数
【数学运算】
- SELECT ABS(-8)--绝对值
- SELECT CEILING(9.4)--向上取整
- SELECT FLOOR(9.4)--向下取整
- SELECT RAND()一一返回一个0~1之间的随机数
- SELECT SIGN (10)一一判断一个数的符号(负数返回-1,正数返回1,0返回0)
复制代码 【字符串函数】
- SELECT CHAR_LENGTH('即使再小的帆也能远航')--字符串长度
- SELECT CONCAT('哈','哈','哈')--拼接字符串
- SELECT INSERT('我爱编程helloworld',1,2,'超级热爱')--从某个位置开始替换某个长度
- SELECT LOWER('Abc')--转化为小写字母
- SELECT UPPER('Abc')--转化为大写字母
- SELECT INSTR('Abc','bc')--返回第一次出现的子串的索引
- SELECT REPLACE('你好','好','坏')--替换出现的指定字符串
- SELECT SUBSTR('你吃饭了吗',1,3)--返回指定的子字符串(源字符串,截取的位置,截取的长度)
- SELECT REVERSE('我和你')--反转
- -- 查询学生,将查询出来的数据姓氏进行替换
- SELECT REPLACE(student_name,'王','玩')FROM student
复制代码 【时间和日期函数】
- SELECT CURRENT_DATE()--获取当前日期
- SELECT CURDATE()-一获取当前日期
- SELECT NOW()--获取当前的时间
- SELECT LOCALTIME()--本地时间
- SELECT SYSDATE()-一系统时间
- SELECT YEAR(NOW())
- SELECT MONTH(NOW())
- SELECT DAY(NOW())
- SELECT HOUR(NOW())
- SELECT MINUTE(NOW())
- SELECT SECOND(NOW())
复制代码 【数据库相干】
- SELECT SYSTEM USER();
- SELECT USER();
- SELECT VERSION();--查询数据库版本
复制代码 【统计相干】
- 都能够统计表中的数据(想查询一个表中有多少个记录,就使用这个cout())
- SELECT COUNT(`BornDate`) FROM student;--Count(字段),会忽略所有的nul1值
- SELECT COUNT(*) FROM student;--Count(*),不会忽略null值,本质计算行数
- SELECT COUNT(1) FROM result;--Count(1),不会忽略忽略所有的nul1值,本质计算行数
- SELECT SUM(`StudentResult`) AS 总和 FROM result;
- SELECT AVG(`StudentResult`) AS 平均分 FROM result;
- SELECT MAX(`StudentResult`) AS 最高分 FROM result;
- SELECT MIN(`StudentResult`) As 最低分 FROM result;
复制代码 注意事项
- UPDATE sys_user
- SET avatar = concat( "https://hahaha/", FLOOR(RAND()*29+1), ".png" )
- WHERE
- avatar IS NULL
- OR avatar = "";
复制代码 使用函数可能导致字段的索引失效,造成效率下降,建议直接读出来,然后用java举行处理
分组过滤
- mysql> select name,gender from user limit 0,10;
- +-----------------+--------+
- | name | gender |
- +-----------------+--------+
- | 系统管理员 | 0 |
- | 企业管理员 | 0 |
- | 门店管理员 | 0 |
- | 郗淑 | 0 |
- | 柳之 | 1 |
- | 夏侯之 | 1 |
- | 吉滢 | 0 |
- | 独孤杰 | 1 |
- | 蒙飘 | 0 |
- | 卫柔 | 0 |
- +-----------------+--------+
- 10 rows in set (0.00 sec)
- -- 根据 gender 进行分组
- mysql> select name,gender from user group by gender limit 0,10;
- +-----------------+--------+
- | name | gender |
- +-----------------+--------+
- | 系统管理员 | 0 |
- | 柳之 | 1 |
- +-----------------+--------+
- 2 rows in set (0.01 sec)
复制代码
能使用where,就不要使用having,否则可能导致索引失效
SELECT语法完整结构
[]:代表可选
{}:代表必选
数据加密
【建表sql】
- DROP TABLE
- IF
- EXISTS `user`;
- CREATE TABLE `user` (
- `id` BIGINT NOT NULL auto_increment COMMENT '主键' PRIMARY KEY,
- `create_time` datetime DEFAULT CURRENT_TIMESTAMP NOT NULL COMMENT '创建时间',
- `update_time` datetime DEFAULT CURRENT_TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
- `is_deleted` TINYINT DEFAULT 0 NULL COMMENT '是否删除 0:未删除 1:已删除',
- `username` VARCHAR ( 50 ) DEFAULT NULL COMMENT '用户名',
- `password` VARCHAR ( 255 ) NOT NULL COMMENT '密码',
- `gender` TINYINT DEFAULT 0 NULL COMMENT '性别 0:男 1:女',
- `age` INT DEFAULT NULL COMMENT '年龄'
- ) COMMENT '用户表';
- -- 插入数据
- mysql> insert into user(`username`,`password`,`gender`,`age`) values('admin','123456',0,18),('hello','123456',0,28),('word','123456',1,17);
- Query OK, 3 rows affected (0.01 sec)
- Records: 3 Duplicates: 0 Warnings: 0
复制代码 【密码MD5加密操作】
- mysql> select * from user;
- +----+---------------------+---------------------+------------+----------+----------+--------+------+
- | id | create_time | update_time | is_deleted | username | password | gender | age |
- +----+---------------------+---------------------+------------+----------+----------+--------+------+
- | 1 | 2023-07-05 10:16:16 | 2023-07-05 10:16:16 | 0 | admin | 123456 | 0 | 18 |
- | 2 | 2023-07-05 10:16:16 | 2023-07-05 10:16:16 | 0 | hello | 123456 | 0 | 28 |
- | 3 | 2023-07-05 10:16:16 | 2023-07-05 10:16:16 | 0 | word | 123456 | 1 | 17 |
- +----+---------------------+---------------------+------------+----------+----------+--------+------+
- 3 rows in set (0.00 sec)
- -- 加密id=1的数据的密码
- mysql> update user set password=MD5(password) where id=1;
- Query OK, 1 row affected (0.01 sec)
- Rows matched: 1 Changed: 1 Warnings: 0
- mysql> select * from user;
- +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+
- | id | create_time | update_time | is_deleted | username | password | gender | age |
- +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+
- | 1 | 2023-07-05 10:16:16 | 2023-07-05 10:18:38 | 0 | admin | e10adc3949ba59abbe56e057f20f883e | 0 | 18 |
- | 2 | 2023-07-05 10:16:16 | 2023-07-05 10:16:16 | 0 | hello | 123456 | 0 | 28 |
- | 3 | 2023-07-05 10:16:16 | 2023-07-05 10:16:16 | 0 | word | 123456 | 1 | 17 |
- +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+
- 3 rows in set (0.00 sec)
- -- 加密所有数据的密码
- mysql> update user set password=MD5(password);
- Query OK, 3 rows affected (0.01 sec)
- Rows matched: 3 Changed: 3 Warnings: 0
- mysql> select * from user;
- +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+
- | id | create_time | update_time | is_deleted | username | password | gender | age |
- +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+
- | 1 | 2023-07-05 10:16:16 | 2023-07-05 10:19:01 | 0 | admin | 14e1b600b1fd579f47433b88e8d85291 | 0 | 18 |
- | 2 | 2023-07-05 10:16:16 | 2023-07-05 10:19:01 | 0 | hello | e10adc3949ba59abbe56e057f20f883e | 0 | 28 |
- | 3 | 2023-07-05 10:16:16 | 2023-07-05 10:19:01 | 0 | word | e10adc3949ba59abbe56e057f20f883e | 1 | 17 |
- +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+
- 3 rows in set (0.00 sec)
- -- 插入数据的时候就执行加密
- mysql> insert into user(`username`,`password`,`gender`,`age`) values('md5',MD5('123456'),0,18);
- Query OK, 1 row affected (0.01 sec)
- mysql> select * from user;
- +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+
- | id | create_time | update_time | is_deleted | username | password | gender | age |
- +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+
- | 1 | 2023-07-05 10:16:16 | 2023-07-05 10:19:01 | 0 | admin | 14e1b600b1fd579f47433b88e8d85291 | 0 | 18 |
- | 2 | 2023-07-05 10:16:16 | 2023-07-05 10:19:01 | 0 | hello | e10adc3949ba59abbe56e057f20f883e | 0 | 28 |
- | 3 | 2023-07-05 10:16:16 | 2023-07-05 10:19:01 | 0 | word | e10adc3949ba59abbe56e057f20f883e | 1 | 17 |
- | 4 | 2023-07-05 10:19:46 | 2023-07-05 10:19:46 | 0 | md5 | e10adc3949ba59abbe56e057f20f883e | 0 | 18 |
- +----+---------------------+---------------------+------------+----------+----------------------------------+--------+------+
- 4 rows in set (0.00 sec)
复制代码 在存储密码这种数据时,一定要举行加密,直接使用MD5加密也还是不安全的,可以被暴力破解,可以使用更加安全的盐值加密
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |