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的小结,毕竟数据库知识是软件研发的基本技能,这里话不多说,开始总结一波. 数据库基本概念 数据库为高效的存储和处理数据的介质(主要分为磁盘和内存两种),一般关系型数据库存储 ...
随机推荐
- Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) C - Table Tennis Game 2
地址:http://codeforces.com/contest/765/problem/C 题目: C. Table Tennis Game 2 time limit per test 2 seco ...
- HDU - 6336 Problem E. Matrix from Arrays (规律+二维前缀和)
题意: for (int i = 0; ; ++i) { for (int j = 0; j <= i; ++j) { M[j][i - j] = A[cursor]; cursor = (cu ...
- launch 文件解析
roslaunch工具是ros中python实现的程序启动工具,通过读取launch文件中的参数配置.属性配置等来启动一系列节点: 很多ROS包或源码包中都有launch文件,一般为该程序包能够运行起 ...
- Facebook力推导航库:React Navigation使用详解
本文来自Songlcy投稿:文章地址:http://blog.csdn.net/u013718120/article/details/72357698 一.开源库介绍 今年1月份,新开源的react- ...
- Java 四大作用域总结
一.ServletContext 1.生命周期:当Web应用被加载进容器时创建代表整个web应用的ServletContext对象,当服务器关闭或Web应用被移除时,ServletContext对象跟 ...
- JS中函数之外不能写return
JS中return有时会遇到这种情况,具体表现为:google浏览器等浏览器可以继续执行,IE浏览器不能执行return,并且google浏览器:执行时会显示SyntaxError: Illegal ...
- centos 使用rz sz指令
在linux下安装rz很方便,使用 yum install lrzsz 就可以安装,正常使用rz和sz命令. 下面对sz和rz命令的一点介绍: 一般来说,linux服务器大多是通过ssh客户端来进行远 ...
- Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum 树状数组+离线
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
- bzoj 2190: [SDOI2008]仪仗队 线性欧拉函数
2190: [SDOI2008]仪仗队 Time Limit: 10 Sec Memory Limit: 259 MB[Submit][Status][Discuss] Description 作为 ...
- JNIjw04
1.VC6(CPP)的DLL代码: #include<stdio.h> #include "jniZ_JNIjw04.h" #include <string> ...