mysql基本操作(重点)
显示数据库
show databases
进入指定数据库
use 数据库名称
- 创建数据库
create database 数据库名称 default character set=utf8
- 删除数据库
drop database 数据库名称
二、数据库表的操作
- 创建表:
create tabale studentinfo(
name varchar(10) not null,
sex char(10) null,
age int(5),
phone bigint(11)
) studentinfo 为所创建的表名 - 删除表
drop table 表名;
- 新增表数据
# 一次增加一条数据 insert into studentinfo (name,sex,age)
values('黎诗','女','')# 一次增加多条数据 insert into studentinfo (name,sex,age) values('黎诗','女',''),('诗诗','女','') insert into 表名称 (字段名称,多个以,间隔) values (具体的值多个以,间隔)
- 修改
update studentinfo set name = '黎诗' where name = '小诗诗'
- 删除
delete from 表名 where 条件
#拷贝表结构+记录
create table day43.user select host,user,password from mysql.user;
#只拷贝表结构
create table day43.user select host,user,password from mysql.user where 1=2;拷贝
三、数据库表的简单查询(一个简单小例子)
- 查询所有人员
select * from people
- 只查询人员的姓名和年龄
select p_name,p_age from people
- 查询年龄为20岁的人有哪些?
select * from people where p_age = ''
- 查询60岁一下的人员有哪些?
select * from people where p_age < '' 查询年龄不等于60岁 的人员
select * from people where p_age <> '' (推荐,正式) select * from people where p_age != '' (非常规) 常见的逻辑运算符 <, >, =, <=, >=, <>, != - 查询50岁以上并且工资大于8000的人员有哪些?(逻辑运算符查询)
select * from people where p_age > 50 && p_sal < 8000 # 注意: and (&&)用于连接两个条件 表示并且意思 # or(||) 用于连接两个条件表示 或者意思 # 逻辑运算符: = , <, >, !=, <> , <=, >=
- 查询姓张的人员(模糊查询)
select * from people where p_name LIKE '张%', select * from prople wherer p_name LIKE '%张%' # 中间有张字的 # select * from 表名 where 字段 like '%张'
# like:表示模糊查询
# 以什么开头 : 's%'
# 以什么结尾: '%s'
# 包含: '%s%' - 查询哪些人属于武当 华山 嵩山(集合查询)
select * from people where p_menpai = '武当' or p_menpai = '华山' or p_menpai = '嵩山'; select * from people where p_menpai in ('武当','华山','嵩山'); # select * from 表名 where 字段 in('值1','值2','值3')
# in : 表示 集合
# not in:表示 反向集合 - 查询工资在5000-8900的人员有哪些?(区间查询)
select * from people where p_sal >= 5000 and p_sal <= 8900; select * from people where p_sal between 5000 and 8900; # select * from 表名 wheere 字段 between 值1 and 值2
# 注意 : between...and... 表示区间查询 - 查询所有人员,要求工资倒序排序(排序)
select * from people where p_sal > 3000 ORDER BY p_sal desc # asc 为正序 (默认)
# desc 为倒序 - 查询年龄为21岁人员的领导者(嵌套查询)
# select p_learder from people where p_age = '21' # selecr * from people where p_id = 'p003' select * from people where p_id = (selest p_learder from peopple where p_age = '')
# select * from 表 where 字段 in (select 字段 from 表 where id = '值1'
# () 优先执行
# 遇到 '=' 值唯一,遇到 in 值为集合 - 查询当前人员中谁的工资最高?(嵌套函数)
# select max(p_sal) as p_sal from people select p_name from people where p_sal = (select max(p_sal) as p_sal from people ) # max () 表示最大值
as 表示 起别名 - 查询当前人员中谁的工资最低?
select p_name from people where p_sal = (select min(p_sal) from people)
SELECT p_name,p_sal from people where p_sal = (SELECT MIN(p_sal) as p_sal FROM people) # 注意:
min 为最小值 - 查询所有人员的平均工资是多少
select avg(p_sal) from people # 注意:avg()表示平均值
- 查询所有人员的工资总和是多少
select sum(p_sal) from ren # 注意 sum()求和
- 查询目前有多少个人员?
select count (p_id) from people # 注意 count(主键) 表示查询表中数据的总条数
- 查询各门派的平均工资是多少
select avg(p_sal),p_menpai,p_name from people GROUP BY p_menpai order by avg(p_sal) desc # 注意: group by 表示分组
- 查询武当派最高工资是谁
select p_name from people where p_sal = (select max(p_sal)from people where p_menpai = '武当') and p_menpai ='武当'
- 查询当前武林中有哪些门派
select p_menpai from people GROUP BY p_menpai select distinct p_menpai,p_name from people # 注意 : distinct 表示去重复查询,要求查询的所有字段必须一样,才认为是重复数据
- 查询当前武林中有哪些门派和门派的平均工资是多少
select p_menpai ,avg(p_sal) from people group by p_menpai
- 查询当前人员表的中的第3条数据到第七条数据(分页)
select * from people limit 2,5 # 注意 limit 表示分页
# 参数1 : 表示从第几条开始查询,下标从0开始
# 参数2 : 表示每次查询多少条数据 - 查询没有门派的人员有哪些?(条件时 用is null 修改时 用= null)
select * from people where p_menpai is null # 表示查询字段为null 的数据 为 is select * from people where p_menpai = '' # 表示查询字段为 ''的数据 # 别的例子
updata people set p_menpai = null where p_id = 'p_008' # 注意 修改字段为null 时,要写 = - 查询武当派下有哪些小弟
select * from people where p_leader = (select p_id from people where p_menpai = '武当' and p_leader = '') select * from people where p_mempai = '武当' and p_leader != ''
- 查询各门派的工资总和按倒序/正序排列
select sum(p_sal) sal,p_menpai from people group by p_menpai order by sal
- 查询人员并显示门派所在位置
select * from people,location where people.p_menpai = location.a_name # 注意: 如果多表联合查询不加条件则会出现(笛卡尔乘积)
# : 在使用多表联合查询时,一定要加条件
# 结果: 符合两个表条件的结果 - 查询人员表,如果人员门派存在位置则显示位置信息,不存在则不显示位置
select * from people left join location on people.p_menpai = location.a_name # 左连接查询
# 注意:on 表示条件 专门配置 left join 来使用
# 特点:左表数据全要,右表的数据与左表数据相匹配则显示,不匹配则以NULL填充 - 查询位置表,如果人员的门派有位置信息则显示人员,没有则不显示
select * from people right join location on people.p_menpai = location.a_name
- 查询登记了地理位置的门派人员信息
select * from people inner join location on people.p_menpai = location.a_name
mysql基本操作(重点)的更多相关文章
- mysql 基本操作语句
mysql 基本操作笔记: 创建表demo:CREATE TABLE `role` ( `role_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMME ...
- css属性编写顺序+mysql基本操作+html细节(个人笔记)
css属性编写顺序: 影响文档流的属性(比如:display, position, float, clear, visibility, table-layout等) 自身盒模型的属性(比如:width ...
- 【mysql】mysql基本操作
mysql基本操作 1.mysql表复制 mysql 表结构的复制 create table t2 like t2 mysql 表数据的复制 insert into t2 select * from ...
- 数据库相关 Mysql基本操作
数据库相关 设计三范式: 第一范式: 主要强调原子性 即表的每一列(字段)包含的内容,不能再拆分.如果,某张表的列,还可以细分,则违背了数据库设计的第一范式. 第二范式: 主要强调主键,即:数据库中的 ...
- Mysql基本操作、C++Mysql简单应用、PythonMysql简单应用
MySql基本操作 -- 当指定名称的数据库不存在时创建它并且指定使用的字符集和排序方式 CREATE DATABASE IF NOT EXISTS db_name CHARACTER SET UTF ...
- MySQL数据库重点监控指标
MySQL数据库重点监控指标 QPS queries per seconds 每秒中查询数量 show global status like 'Question%'; Queries/seconds ...
- MySQL必知必会笔记-Mysql基本操作
Mysql基本操作 mysql的基本操作包括增.删.改.查,本书中前三章简单的介绍MySQL为何物,查是mysql中非常重要的功能,4-6章展示了mysql的查(查询--select)的简单实现,my ...
- day02 MySQL基本操作
day02 MySQL基本操作 昨日内容回顾 数据库演变史 1.纯文件阶段 2.目录规范 3.单机游戏 4.联网游戏 # 数据库就是一款帮助我们管理数据的程序 软件开发架构及数据库本质 cs架构与bs ...
- MYSQL基本操作(上)
很久之前,就想做个Mysql的小结,毕竟数据库知识是软件研发的基本技能,这里话不多说,开始总结一波. 数据库基本概念 数据库为高效的存储和处理数据的介质(主要分为磁盘和内存两种),一般关系型数据库存储 ...
随机推荐
- 【转】Python爬虫(7)_scrapy-redis
scrapy-redis使用以及剖析 scrapy-redis是一个基于redis的scrapy组件,通过它可以快速实现简单分布式爬虫程序,该组件本质上提供了三大功能: scheduler - 调 ...
- git---控制面板提交
比如我修改了一个项目的代码.需要提交代码. 1.打开项目所在目录,右键>Git Bash Here 2.打开交互模式.git会列出所有untracked的文件,然后你可以用各种形式加入.git ...
- HackerRank - string-reduction【反推】【规律】
HackerRank - string-reduction[反推] 题意 给出一串 只有 字母 a, b, c 组成的字符串,然后没两个不同的字符碰到一起都可以变成另外一个字符,然后变到最后,求最短的 ...
- python入门四:异常
一.异常 异常就是在触发异常条件时(解释器或程序员)而采取相应的措施 c++中异常使用try, throw, catch等关键字,而python中使用try, raise, except等 二.标准异 ...
- LVS原理详解以及部署
linux virtual server简称LVS,Internet的快速增长使多媒体网络服务器面对的访问数量快速增加,服务器需要具备提供大量并发访问服务的能力,因此对于大负载的服务器来讲, CPU. ...
- mysql数据库导入、导出、数据传输
Navicat数据库之间导入导出1.双击要导出的数据库,右键选转储SQL文件...,选择要保存的文件夹. 2.点击开始后,开始导出. 数据库导入1.新建数据库,数据库的名字必须和导入的数据库文件一致. ...
- 使用Kali Linux 破解无线网
用到的工具 airmon-ngairodump-ngaireplay-ngaircrack-ng 过程 123456789101112131415161718192021222324 root@lm: ...
- [BZOJ1257]余数之和
Description 给出正整数n和k,计算j(n, k)=k mod 1 + k mod 2 + k mod 3 + … + k mod n的值 其中k mod i表示k除以i的余数. 例如j(5 ...
- 修改Maven源为阿里巴巴的镜像
在C:\Users\Administrator\.m2创建setting.xml文件,内容如下 <settings xmlns="http://maven.apache.org/SET ...
- idea中git合并切换分支等操作
https://blog.csdn.net/autfish/article/details/52513465