SQL语句基础

刚链接mysql所选用的数据库为空

DDL

查看所有数据库
show databases;
进入数据库(-h主机名和-P端口号可不写)
mysql -h 127.0.0.1 -P3306 -uroot -pAGOC0927
创建数据库如果不存在
 create database if not exists first;
 删除数据库(如果存在)
  drop database if exists third;
  创建数据库指定数据库编码
 create database if not exists third default charset utf8mb4;
 查看当前使用的数据库
 select database();
 使用数据库
 use third;
 创建表(comment可不写)
 create table tb_user(
    -> id int comment '编号',
    -> name varchar(10) comment '姓名',
    -> age int comment '年龄',
    -> gender int comment '性别'
    -> )comment '用户表';
    描述表
desc tb_user;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | YES  |     | NULL    |       |
| name   | varchar(10) | YES  |     | NULL    |       |
| age    | int(11)     | YES  |     | NULL    |       |
| gender | int(11)     | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
查看指定表的建表语句
 show create table tb_user;
 查看表
 show tables;

DML

新建表并添加字段(varchar需要指定长度)
mysql> create table si(
    -> id int comment '编号',
    -> workno varchar(10) comment '工号',
    -> name varchar(10),
    -> sex char(1),
    -> age tinyint unsigned,
    -> idcard char(18) comment '身份证号',
    -> entrydate date comment '入职时间'
    -> ) comment '员工信息表';
添加字段
 alter table si add nickname varchar(20)comment '绰号';
 修改字段类型
  alter table si modify nickname varchar(10);
  修改表字段名及类型
 alter table si change nickname username varchar(30) comment '用户名';
 删除字段
alter table si drop username;
修改表名
 alter table si rename to sii;
 删除表(if exists)可不写
 drop table if exists meiyong;
 删除表并重新创建该表,数据会无,字段保留
  truncate table student;
  插入数据varcharchardate类型需要加引号,数字不用。
插入部分字段的单个数据
insert into sii(workno,sex,entrydate)values(4,'女','2023-6-8')
插入部分字段的多条数据
insert into sii(workno,sex,entrydate)values(6,'男','2023-6-10'),(7,'女','2023-6-11')
插入全部字段的单条数据
insert into sii values (1,3,'张无忌','男',23,'10086','2023-6-7')
插入全部字段的多条数据
insert into sii values (1,8,'周芷若','女',23,'10087','2023-6-12'),(1,9,'金毛狮王','男',24,'10088','2023-6-13')
更改表中的数据
update sii set name='孙悟空' where id=2
更改一行中多个字段的数据
update sii set name='猪八戒',age='100' where id=2;
修改全部行的一列数据
update sii set entrydate='2002-7-7'
删除一行数据
delete from sii where sex='女'
删除全部数据
delete from sii

DQL

count为所有行的计数,count()为为指定分组计数

查询某些列
select name,workno,age from sii;
查询所有字段返回
SELECT * FROM sii
查询字段返回别名,as可省略,as为返回时代替的字段名,没有修改表的字段
select workaddress as '工作地址' from sii;
查询返回字段不重复的数据
select distinct workaddress '工作'from sii;
查询年龄小于等于18的所有字段
select *from sii where age<=18;
查询idcard不为空的所有字段
select*from sii where idcard is not null;
年龄不为18select*from sii where age !=18;
select*from sii where age<>18;
年龄
select*from sii where age>=17 &&age<=20;
select*from sii where age>=17 and age<=20;
select*from sii where age between 17 and 20;
查询性别为女,年龄小于20的字段
select*from sii where sex='女'and age<20;
或(or也可以是||select*from sii where age=18 or age=20 or age=40;
in
select*from sii where age in(18,20,40);
查询名字为两个字的字段
select*from sii where name like'__';
查询身份证号以x结尾的
select*from sii where idcard like'%x';
统计人数总
select count(*)from sii;
统计平均年龄
select avg(age)from sii;
最大年龄
select max(age)from sii;
最小年龄
select min(age)from sii;
北京工作的年龄之和
select avg(age)from sii where workaddress='北京';
统计各性别人数
select sex,count(*)from sii group by sex;
统计个性别的平均年龄,按性别分组
select sex,avg(age) from sii group by sex;
统计各城市工作的人的数量>=1的城市,workaddress也可为count(*)select后面的workaddress和count(*)为
新查询的列名,groupby为以workaddress为索引(把不同行的地名合并,每个城市作为1加起来作为新列的值),count(*)和操作后的数据为查询后的新列。having为分组后过滤,分组时要执行聚合函数,where为分组前过滤
select workaddress,count(*) from sii where age<24 group by workaddress having count(*)>=1
统计城市>=1的人数并为count(*),count(workaddress)取别名
select workaddress,count(*) address_count from sii where age<24 group by workaddress having count(*)>=1

在这里插入图片描述

把人按年龄升序排列
select * from sii order by age asc
按入职时间降序排列(默认升序)
select * from sii order by entrydate desc
默认升序
select * from sii order by age;
按年龄升序,年龄相同按入职时间降序
select * from sii order by age asc,entrydate desc;
分页查询,第一个参数(起始索引)为(查询页码-1*每页的记录数,第二个参数(查询记录数)为查询到的页码需要显示的数据条数。第一页可以省略起始索引,直接输入想要显示的数据条数。
select * from sii limit 6,1

在这里插入图片描述

查询年龄在21222324中的女员工信息
select * from sii where age in(21,22,23,24)and sex='女'
查询男的年龄在20-40之间,名字为两个字的数据
select * from sii where sex='男'and age between 20 and 40 and name like '__'
查询年龄小于20的各性别的人数
select sex,count(*)from sii where age<20 group by sex
把年龄小于等于23的员工姓名和年龄查询
select name,age from sii where age<=23 order by age asc,entrydate desc
查询年龄在20-40的男员工的按年龄升序,入职时间降序的前2个员工信息
select*from sii where age between 20 and 40 and sex='男'order by age asc,entrydate desc limit 0,2

结构:select * from * where * group by * having * order by * limit *
顺序:from * where * group by * having * select * order by * limit *

查询年龄大于18的员工姓名,年龄
select name,age from sii where age>18 order by age asc
别名的使用
select e.name ename,e.age eage from sii e where e.age>18 order by eage asc;

DCL

创建用户0607,密码123456
create user '0607'@'localhost' identified by '123456'```
创建用户hm,可在任意主机访问该数据库,
create user 'hm'@'%'identified by '123457'
修改用户hm密码为1234
DROP USER 'hm'@'%';
CREATE USER 'hm'@'%' IDENTIFIED WITH mysql_native_password;
SET PASSWORD FOR 'hm'@'%' = PASSWORD('1234');
删除用户0607
drop user '0607'@'localhost'
查询当前用户的权限
show grants for 'hm'@'%'
授予用户hmfirst数据库中所有表的所有权限
grant all on first.* to 'hm'@'%'
撤销授予用户hm对first数据库所有表的所有权限
revoke all on first.* from 'hm'@'%'




    


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

麻烦放收发室,谢谢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值