mysql基本操作
0x01:
以管理员身份运行cmd
执行 net start mysql57
mysql -u root -p
mysql中的基础语句模块
create:创建
show:展示数据库
desc/describe:展示表
drop:删除
alter:修改
order by xx:按xx排序
ljmit x,x:从x开始显示x条记录
limit x:检索前x条记录
imit x offset y:从y开始检索x条记录
where:限定查询条件
group by:按字段分组
ASC:升序
DESC:降序
如果忘记写;可直接在后面添上;
因为在mysql中;是结束符
数据库
create database;
show create database;
show databases;
数据表
先进入数据库
use xxx;
1.create table xxx( sid int unsigned not null primary key auto_increment, //创建表
nickname varchar(20) not null,
Sname CHAR(20) UNIQUE, /Sname 取唯一值/
age tinyint not null ); //表初始化
2.增加表中字段
alter table t_namel add c col_name varchar(20)(定义类型);
//我执行之后有点奇怪。发现原因原来是字段名不能为sql内置函数where (捂脸
3.一些其他的操作
show create table;
show tables;
desc/describe xxx(name);
default 数值/‘字符’
CURD操作
ALTER TABLE user10 MODIFY test CHAR(32) NOT NULL DEFAULT '123';
1.alter table 表名 modify column 字段名 数据类型(修改后的长度)+(可选)+default 数值/‘字符’ ;
eg:alter table student modify column sid int(20);
2.alter table <表名> change <字段名> <字段新名称> <字段的类型>+(可选)+default 数值/‘字符’ ;
eg: alter table student change aname sname varchar(30) ;
(改了之后不给默认值,之前给的默认值会直接抹去变成null)
3.update 表名 set 字段名=‘新内容’ + where条件
4.简单insert语句
4.插入多行
在这种形式中,每行的值列表用逗号分隔。
如果为表中的所有列指定相应列的值,则可以忽略INSERT语句中的列列表
自己小小的练习了一下insert语句
数据查询
1.SELECT * FROM 表名
eg:select * from emp
2.SELECT colName1,colName2,colName3,… FROM 表名 [约束]
eg:select empno,ename,job form emp
3.数据运算查询
对NUMBER类型,可以使用算术操作符创建表达式 (+ - * /)
对DATE类型,可以使用部分算术运算符创建表达式(+ -)
运算符优先级
1.先乘除后加减
2.同级运算符,从左到右
3.表达式中使用“括号”,可强行改变运算顺序
eg:
1,查询所有员工的年薪
select ename,sal * 12 '年薪' from emp;
2,查询所有员工的年薪(使用别名)
3,查询每月都有500元的餐补和200元交通补助并且年底多发一个月工资的年薪
select ename,(sal + 500 + 200)*12+sal '年薪'from emp;
4,演示date类型数据的运算:查询员工的雇佣日期加上10
select DATE_ADD(hiredate,INTERVAL 10 DAY) from emp;
4.where查询
SELECT colName1,colName2 FROM tableName [WHERE 限制条件 AND/OR 限制条件2]
eg:
要求查询出基本工资高于1500的所有员工信息
select * from emp where sal >= 1500;
1,查询名字叫SCOTT的员工所从事的工作
select * from emp where ename = 'scott';
严格区分大小写,binary
select * from emp where binary ename = 'SCOTT';
2,查询1981年入职的员工信息.
select * from emp where hiredate between '1981-01-01' and '1981-12-31';
5.结果排序
单列排序
SELECT * FROM tName ORDER BY colName [DESC\ASC]
多列排序
SELECT * FROM tName ORDER BY colName1 [DESC\ASC],colName2 [DESC\ASC]
- MAX函数(适用于数值和字符串)
eg:
mysql> SELECT MAX(student_score)
-> AS max_score
-> FROM tb_students_score;
7.union select 联合查询
to be completed
导入sql文件
1.use xxx;
2.source 路径 d:/xxxxxxxxx.sql(注意复制路径时要把\ 改成/)
1.查询最大值
mysql> select max(SALARY)
-> as max_SALARY
-> from employees;
*网上看到的同时查找前三个和最后三个语句
select * from (select *from (select * from 表名 order by 字段名 desc limit 3)a order by score limit 1)a union
select * from (select *from (select * from 表名 order by 字段名 limit 3)a order by score desc limit 1)b
2.查询三列中的最大值
select id,a,b,c,greatest(a,b,c) as max from XXX
3.对表中数据排序
select * from employees
a where( select count(1)
from employees b
where b.salary=a.salary and b.salary>=a.salary)<=3
order by a.salary,a.salary desc;
关于大礼包作业
1.create database demo;
2.use demo;
3.source d:/employees.sql ; //导入sql文件
-
select * from employees //按employees中的salary进行从低到高排序 a where( select count(1) from employees b where b.salary=a.salary and b.salary>=a.salary)<=3 order by a.salary,a.salary desc;
补充:(select * from employees order by salary asc;) //这样也能按从低到高输出表
5.select * from employees limit 0,3;(再进行表中字段查询
到了这里看似好像把大礼包作业就完成了
但我凭借我的语法直觉,感觉不太对劲(因为语句中没有表明是跟据哪个字段排的序
于是我又执行
6.select * from employees limit 0,5;
到了这不难看出 这是默认第一个字段id 从低到高进行排序的
前三个id刚好也是工资最高的3个
7.执行网上看到的一个select * from employees order by salary limit 0,3;
但这是由工资从低取的前三个,并不是我们想要的
于是又有了
8.select * from(select * from(select * from employees order by salary desc limit 3) a order by salary limit 3) a;
至此大礼包作业完成,看似大功告成了
但其实还没有,我们还应该理解一些这些语句的原理
经过查阅百度出来的博客后
又可以通过以下语句实现
select * from employees order by salary desc limit 0,3;
(很多人都说可以这样 select top 3 * from employees order by salary desc;但其实mysql没有top 只能通过limit x,x实现 。 ms sqlserver中才有top。。orz