浅记录一下学习mysql时的一些基本sql语句
(很粗糙,不系统,仅供自己记忆模糊时查看记忆)
修改mysql 数据库的密码:
alter user ‘root’@‘localhost’ identified by ‘root’;(by 接着的是密码)
一些基本的sql语句,增删查改
– create drop select alter
– insert delete select update
– *操作表DDL Date Definition Language–
– 修改表名
alter table stu rename to newstu;
– 添加一列
alter table stu add newcolumn int;
– 修改数据类型
alter table stu modify id varchar(10);
– 修改列名和数据类型
alter table stu change id newid int;
– 删除列
alter table stu drop newcolumn;
– *删除表DDL Date Definition Language–
drop table if exists stu;
– *删除表DML Date Manipulation Language–
– 添加数据
insert into stu(id, uname, gender) values(1, ‘lili’, ‘woman’);
– 修改数据
update stu set uname=‘gewei’, gender=‘man’ where id=2;
– 删除数据(若不加条件,则将所有数据都删除)
delete from stu where id=3;
– 查询数据
select distinct(去除重复) * as 名字 from stu where id=1 group by ‘nv’ having math>70 order by math asc, english desc limit 0(起始索引 = (当前页码-1)*每页显示的条数, 3(查询条目数)
– 执行顺序 where>聚合函数>having
分组查询 DQL
分页查询 DQL
分页查询案例
1.向DEPT表插入一行数据,部门号为50,部门名称为’CS’,地址为‘YueYang’。
insert into scott.dept values(50, ‘cs’, ‘yueyang’);
2.查询20号部门所有员工的员工号、员工姓名和每位员工的领导(MGR)姓名。
select a.empno, a.ename 员工姓名, b.ename 领导姓名
from scott.emp a, scott.emp b
where a.mgr=b.empno and a.deptno=20;
3.统计各部门工资总额、平均工资,并按平均工资升序排列
select deptno, round(sum(sal),0), round(avg(sal),0)
from scott.emp
group by deptno
order by avg(sal);
4.查询同一部门中工资相同的员工的姓名及工资。
select a.deptno, a.ename, b.ename, a.sal
from scott.emp a, scott.emp b
where a.deptno=b.deptno and a.sal=b.sal and a.empno!=b.empno;
5.查询各个部门中各个工种(JOB)的平均工资。
select deptno, job, round(avg(sal), 0)
from scott.emp
group by deptno, job
order by deptno;
6、查询每个部门的平均工资和所有员工的平均工资。
select a.deptno 部门号, a.sal 部门平均工资, b.avg 所有员工平均工资
from
(select deptno, avg(sal) sal
from scott.emp
group by deptno) a,
(select avg(sal) avg from scott.emp) b;