萤火小屋

优律的知识库

  • 首页
  • 归档
  • 分类
  • 标签
  • 留言
  • 关于

  • 搜索
消息队列 RabbitMQ Redis 双指针 力扣 动态代理 Git YAML SpringBoot SpringMVC 回溯算法 分治算法 归并排序 快排 手撕 事务 MySQL索引 MySQL 小技巧 Spring Framework Spring 动态规划 Linux Android 贪心算法 操作系统 进程调度模拟 IPv6 数据库 计算机组成原理 计算机基础 栈 Java 静态路由 路由器 交换机 数字通信 网络工程 计算机网络 Web http 大学学习技巧 程序设计 算法

MySQL数据库SQL语句笔记

发表于 2019-10-12 | 分类于 MySQL | 2 | 阅读次数 780

数据操作指令

增加数据
insert into 表名 values(1,'admin','admin');
insert into 表名(字段) values(值)           // 注意字段和值一一对应
insert into 表名(字段) values('admin','admin')('test','test')   
查询数据
select 字段 from 表名;
select * from users;
select username as name from users;                         // as起别名
select username,password from users;
select username,password from users where id = 2;

select distinct address from users;                         // distinct去除重复值查询 
select * from users where age between 30 and 40;             // between取区间值
select * from users where age in(10,16,18);                  // in指定内容
select * from users where address like '%山%';               // like模糊查询
select * from users order by age (asc);                     // order by升序查询
select * from users order by age desc;                      // order by xxx desc降序查询
select * from users limit 1,2;                              // limit查询,从第1条开始查询,取2条数据(默认从0开始计算)
select * from users limit 2;                                // 只取前两条
select * from users group by address;                       // group by分组查询,去重
select count(*) from users;                                 // count查询数目

select 表1字段, 表2字段 from 表1 inner join 表2 on 条件;         //内联查询
select username, gname from user inner join goods on user.gid = goods.gid;

select 表1字段(,表2字段) from 表1 left join 表2 on 条件;       //左联查询
select user.username from user left join goods on user.gid = goods.gid;

select 表1字段(,表2字段) from 表1 right join 表2 on 条件;      //右联查询
select user.username from user right join goods on user.gid = goods.gid;

select 字段 from 表 where 字段 in(select id from 表);            //嵌套查询
select * from user where gid in(select gid from goods);
删除数据
delete from 表名 where 字段;
delete from users where id = 1;
修改数据
update 表名 set 字段 where 字段;
update users set username = 'test',password = 'test' where id = 1;



表操作命令

操作表的时候必须先使用指定的数据库
use test;
创建表
create table 表名(id int(10), username varchar(50), password varchar(32));
查看表
show tables;
删除表
drop table 表名;
查看表结构
desc 表名;
修改表字段
alter table 表名 change 原字段名 要改的字段名 varchar(32);
修改表字段值
alter table 表名 modify 字段名字 修改后的值;
修改表名字
alter table 表名 rename 新表名;
添加表字段
alter table 表名 add 字段名字 字段类型;
删除表字段
alter table 表名 drop 字段名;
查询当前表在哪个库
select database();



库操作指令

查看所有数据库
show databases;
创建数据库
create database [if not exists] test;
删除数据库
drop database [if exists] test;
使用数据库
use test;




该笔记来自冰神大佬

# 数据库 # MySQL
数字通信-STP原理与配置
IPv6基础
  • 文章目录
  • 站点概览
优律

优律

优律的知识库

83 日志
20 分类
44 标签
E-mail Twitter Instagram
Links
  • CZLisyx - 浮生志
  • Vedfolnir
0%
© 2019 — 2023 萤火小屋——优律的博客网站
网站已勉强运行 
Halo博客系统技术支持