Mysql 使用命令及 sql 语句示例
Mysql 是数据库开发使用的主要平台之一。sql 的学习掌握与使用是数据库开发的基础,此处展示详细sql 语句的写法,及各种功能下的 sql 语句。
在此处有 sql 语句使用示例:在这里
此处插入两张图更有说服力:
sql1.PNG
sql2.PNG
说明:
第一张图片是进入该环境,输入自己设定的密码即可进入数据库并进行相关操作;
第二张图片是演示显示所有数据库,设置当前数据库,并对当前数据库操作,显示当前数据库的所有表,查询表中部分记录的命令操作。
基本命令使用是这样的。当然sql 语句也可在其他平台使用。此处不多说明;
SQL语句详细如下:
一、数据库操作
- <span style="font-size:14px;">创建一个名称为mydb1的数据库
- create database mydb1;
- show databases;
- 创建一个使用utf-8字符集的mydb2数据库。
- create database mydb2 character set utf8;
- 创建一个使用utf-8字符集,并带校对规则的mydb3数据库。
- create database mydb3 character set utf8 collate utf8_general_ci;
- 查看前面创建的mydb2数据库的定义信息
- show create database mydb2;
- 删除前面创建的mydb1数据库
- drop database mydb1;
- 查看服务器中的数据库,并把其中某一个库的字符集修改为gb2312;
- alter database mydb2 character set gb2312;
- show create database mydb2;
- 使用当前数据库 mydb1,即想对当前数据库进行操作之前使用的命令
- use mydb1;</span>
二、表的操作
1>表的创建演示
- <span style="font-size:14px;">创建一个员工表
- use mydb2;
- create table employee
- (
- id int,
- name varchar(40),
- sex varchar(4),
- birthday date,
- entry_date date,
- job varchar(40),
- salary decimal(8,2),
- resume text
- );
- show tables; 查看库的所有表(查看库里的表要先打开库)
- show create table employee; 查看表的创建细节
- desc employee; 看表结构</span>
2>对表的基本操作:增、删、改、查
- <span style="font-size:14px;">在上面员工表的基本上增加一个image列。
- alter table employee add image varchar(20);
- 修改job列,使其长度为60。
- alter table employee modify job varchar(60);
- 删除sex列
- alter table employee drop sex;
- 表名改为user。
- rename table employee to user;
- 修改表的字符集为utf-8
- alter table user character set utf8;
- 列名name修改为username
- alter table test change column address address1 varchar(30)
- 删除表
- drop table user;</span>
3>增加、插入记录的 sql 语句详细
- <span style="font-size:14px;">使用insert语句向表中插入三个员工的信息。
- rename table user to employee;
- insert into employee(id,username,birthday,entry_date,job,salary,resume) values(1,'aaa','1980-09-09','1980-09-09','bbb',90,'aaaaa');
- select * from employee;
- 插入数据的细节1
- insert into employee values(1,'aaa','1980-09-09','1980-09-09','bbb',90,'aaaaa');
- 插入数据的细节2
- insert into employee values('1','aaa','1980-09-09','1980-09-09','bbb','90','aaaaa');
- 插入数据的细节3(插入中文)
- 要告诉mysql客户采用gb2312编码
- show variables like 'chara%';
- set character_set_client=gb2312;
- insert into employee(id,username) values('3','张三');
- 要想查看时不乱码
- show variables like 'chara%';
- set character_set_results=gb2312;
- select * from employee;</span>
4>删除记录的 sql 语句详细
- <span style="font-size:14px;">删除表中名称为’zs’的记录。
- delete from employee where username='bbb';
- 删除表中所有记录。
- delete from employee;
- 使用truncate删除表中记录。
- truncate table employee;</span>
5>修改记录的 sql 语句详细
- <span style="font-size:14px;">将所有员工薪水修改为5000元。
- update employee set salary=5000;
- 将姓名为’bbb’的员工薪水修改为3000元。
- update employee set salary=3000 where username='bbb';
- 将姓名为’bbb的员工薪水修改为4000元,job改为ccc。
- update employee set salary=4000,job='ccc' where username='bbb';
- 将bbb的薪水在原有基础上增加1000元。
- update employee set salary=salary+1000 where username='bbb';
- 更新要注意的问题
- update employee set username='ccc',salary=9000,birthday='1980-09-09',.....................
- update where id=1;</span>
6>查询记录的 sql 语句详细
- <span style="font-size:14px;">查询表中所有学生的信息。
- select * from student;
- 查询表中所有学生的姓名和对应的英语成绩。
- select name,english from student;
- 过滤表中重复的英语数据。
- select distinct english from student;
- 在所有学生总分上加10分特长分。
- select name,(chinese+english+math)+10 from student;
- 统计每个学生的总分。
- select name,(chinese+english+math) from student;
- 关于排序
- 将对象成绩过去
- 统计大于该成绩的人数即可
- 统计数学成绩大于90的学生有多少个?
- select count(*) from student where math>80;
- 使用别名表示学生分数。
- select name as 姓名,(chinese+english+math)+10 as 总分 from student;
- select name 姓名,(chinese+english+math)+10 总分 from student;
- 查询姓名为wu的学生成绩
- select * from student where name='王五';
- 查询英语成绩大于90分的同学
- select * from student where english>'90';
- 查询总分大于200分的所有同学
- select name from student where (chinese+english+math)>200;
- 查询英语分数在 80-90之间的同学。
- select name from student where english>80 and english<90;
- select name from student where english between 80 and 90; == select name from student where english>=80 and english<=90;
- 查询数学分数为89,90,91的同学。
- select * from student where math in(89,90,91);
- 查询所有姓李的学生成绩。
- select * from student where name like '李%';
- select * from student where name like '李_';
- 查询数学分>80,语文分>80的同学。
- select * from student where math>80 and chinese>80;
- 分页查询,查询从第 8 条记录开始的 3 条记录;即:第8 、9 、10 三条记录
- int from = 2;
- int end = 10;
- String sql = "select * from student limit "+from+","+end; //字符串类型的语句
- select * from student limit 8,3;</span>
三、对数据记录的操作
查询统计排序等相关处理的 sql 语句
- <span style="font-size:14px;">对数学成绩排序后输出。
- select name,math from student order by math;
- 对总分排序后输出,然后再按从高到低的顺序输出
- select name 姓名,(chinese+english+math) 总分 from student order by (chinese+english+math) desc;
- select name 姓名,(chinese+english+math) 总分 from student order by 总分 desc;
- 对姓李的学生成绩排序输出
- select * from student where name like '李%' order by (chinese+english+math) desc;
- 统计一个班级共有多少学生?
- select count(name) from student;
- select count(*) from student;
- 统计数学成绩大于90的学生有多少个?
- select count(*) from student where math>80;
- 统计总分大于250的人数有多少?
- select count(*) from student where (chinese+english+math)>250;
- 关于 count的函数的细节 (count只统有值的行)
- 统计一个班级数学总成绩?
- select sum(math) from student;
- 统计一个班级语文、英语、数学各科的总成绩
- select sum(chinese),sum(english),sum(math) from student;
- 统计一个班级语文、英语、数学的成绩总和
- select sum(chinese+english+math) from student;
- 统计一个班级语文成绩平均分
- select sum(chinese)/count(*) from student;
- 统计一个班级语文成绩平均分
- select avg(chinese) from student;
- 求一个班级总分平均分
- select avg(chinese+math+english) from student;
- 求班级最高分和最低分
- select max(chinese+math+english),min(chinese+math+english) from student;
- 对订单表中商品归类后,显示每一类商品的总价
- select product,sum(price) from orders group by product;
- 查询购买了几类商品,并且每类总价大于100的商品
- select product from orders group by product having sum(price)>100;
- 按某一属性对记录进行排序
- select * from student order by grade desc;</span>
Mysql 使用命令及 sql 语句示例的更多相关文章
- mysql 常用命令 常用SQL语句
维护命令 数据库 ##创建数据库 mysql> create database test; Query OK, 1 row affected ##删除数据库 mysql> drop dat ...
- MySQL的EXPLAIN命令用于SQL语句的查询执行计划
MySQL的EXPLAIN命令用于SQL语句的查询执行计划(QEP).这条命令的输出结果能够让我们了解MySQL 优化器是如何执行SQL 语句的.这条命令并没有提供任何调整建议,但它能够提供重要的信息 ...
- 【转】MySQL用户管理及SQL语句详解
[转]MySQL用户管理及SQL语句详解 1.1 MySQL用户管理 1.1.1 用户的定义 用户名+主机域 mysql> select user,host,password from mysq ...
- 如何用VS EF连接 Mysql,以及执行SQL语句 和存储过程?
VS2013, MySQL5.7.18 , MySQL5.7.14 执行SQL语句: ztp_user z = new ztp_user(); object[] obj = new object[] ...
- MySQL数据库(一)—— 数据库介绍、MySQL安装、基础SQL语句
数据库介绍.MySQL安装.基础SQL语句 一.数据库介绍 1.什么是数据库 数据库即存储数据的仓库 2.为什么要用数据库 (1)用文件存储是和硬盘打交道,是IO操作,所以有效率问题 (2)管理不方便 ...
- 浅谈mysql配置优化和sql语句优化【转】
做优化,我在这里引用淘宝系统分析师蒋江伟的一句话:只有勇于承担,才能让人有勇气,有承担自己的错误的勇气.有承担错误的勇气,就有去做事得勇气.无论做什么事,只要是对的,就要去做,勇敢去做.出了错误,承担 ...
- 如何记录MySQL执行过的SQL语句
很多时候,我们需要知道 MySQL 执行过哪些 SQL 语句,比如 MySQL 被注入后,需要知道造成什么伤害等等.只要有 SQL 语句的记录,就能知道情况并作出对策.服务器是可以开启 MySQL 的 ...
- mysql里面如何用sql语句让字符串转换为数字
sql语句将字符串转换为数字默认去掉单引号中的空格,遇到空格作为字符串截止, SELECT '123 and 1=1' +0 结果为123 MySQL里面如何用sql语句让字符串的‘123’转换为数字 ...
- MySql命令行命令和SQL语句
一.常用mysql命令行命令 1.启动MYSQL服务 net start mysql 停止MYSQL服务 net stop mysql 2.netstat -na|findstr 3306 查看被监听 ...
随机推荐
- python入门之实例-商品选择
需求: 显示一系列商品,根据序号选择商品 li = ["手机","电脑","电视"] #函数enumerate在for循环遍历的时候,会默认 ...
- 用javascript编写一个简单的随机验证码程序
简单模拟网页的随机数字验证码,效果图如下: html代码: <div id="content"> <div class="left"> ...
- C#操作Windows用户
首先需要引入System.DirectoryServices.dll using System; using System.Collections.Generic; using System.Dire ...
- IIS网站设置禁止IP访问设置方法
本文设置系统为Windows2003.IIS版本是6.0. 打开IIS管理器,在iis管理器左侧单击打开网站下面的相应需要设置的网站,并在此网站上右键,选择属性,即可打开该网站属性进行相关设置. (i ...
- Oracle如何创建表空间
create user frame identified by tiger; grant create session to frame; grant create table to frame; g ...
- vue.js 2.0 学习笔记
指令带有前缀 v-,表示是由 Vue 提供的专用属性. v-on 指令 来增加事件监听器,触发事件后会调用 Vue 实例中 methods 下定义的方法 v-model 指令,使得表单输入和应用程序状 ...
- React 实践记录 01 组件开发入门
Introduction 本文组成: Ryan Clark文章Getting started with React的翻译. 博主的实践心得. React由Facebook的程序员创建,是一个非常强大的 ...
- linux学习笔记汇总
linux 文件系统是采用级层树状的目录结构,采用"/"根目录的方式 目录结构: / 根目录 |---root: 存放root用户相关的文件 ...
- js 下载文件/导出
const url = '/sasd/fsd/xxxx/exportMailData2Excel'this.downloadFile(url, 'blob', this.isSearch) // 调用 ...
- shell脚本自动部署及监控
一.shell脚本部署nginx反向代理和三个web服务 1 对反向代理服务器进行配置 #!/bin/bash #修改用户交互页面 用户输入参数执行相应的参数 #安装epel扩展包和nginx fun ...