数据库mariadb操作sql语句

数据库基本操作

数据库登录
#mysql -u'user' -p'password'

查看数据库版本
>status

产看所有库
>show databases;

创建数据库pikachu
>create datatbase pikachu;

查看pikachu的创建属性
>show create database pikachu;

删除数据库pikachu2
>drop database pikachu2;

使用数据库pikachu
>use pikachu

修改数据库pikachu支持中文
>alter database pikachu character set utf8;
>alter database pikachu default character set=utf8;



查看当前用户
>select user();

创建用户pikachu
>create user pikachu identified by '123';

创建用户,并给他查询创建更新删除的权限
>grant select,create,update,delete on pikachu.grade to 'pikachu'@'%' indentified by password;

赋予用户所有权限
>grant all privileges on pikachu.grade to 'piakchu'@'%';

收回所有权限
>revoke all on *.* from 'pikachu'@'%';

查看用户权限
>show grants for pikachu;

查看所有用户权限
>show grants;

*.* 任意库的任意表
“%”支持第三方登录

数据库的数据类型
1,整数int,bit
2,小数decimal #decimal(5,2)5位保留两位小数
3,字符串varchar,char;varchar可变字符长度,char不可变,前提确定要储存的小于charde长度
4,日期时间date,time,datetime
5,枚举类型enum

约束
1,主键primary key:物理上存储的顺序(不能重复,默认为索引,不能为空,可与外键关联)
2,非空not null:此字段不能为空
3,唯一unique:此字段不能重复
4,默认default:不填写此值时使用默认值
5,外键foreign key:对关系字段进行约束,当为关系字段填写值时,会到关联的表中查询此值是否存在,如果存在则填写成功,如果不存在则填写失败并抛出异常
在这里插入图片描述

数据库增,删,改,查

创建表
>create table piakchu(id tinyint unsigned primary key auto_increment,
name varchar(20),
age tinyint unsigned,
high decimal(5,2),
sex enum('male',female','a') default 'a',));

#auto_increment 自增长
#enmu 可1,2,3代表
#default 设置默认

>desc pikachu;查看表信息
>show create table pikachu;查看表的创建参数

添加表头
>alter table pikachu add birth date;
修改表头和数据类型
>alter table pikakchu change birth bithday datetime;
修改表头数据类型,只能修改数据类型
>alter table pikachu modify birthday date;
删除表头
>alter table piakchu drop birthday;
删除表
>drop table piakchu;

增加内容
>insert into pikachu values(0,'name',18,182.152,0);
>inser into pikachu(id,name) values(0,'name2');
>inser into pikachu(id,name) values(0,'name3'),(0,'name4');
可以插入1,0,default,null

更新内容
>update pikachu set name='pikachu' where id=1;

物理删除
>delete from pikachu where name='pikachu';
逻辑删除
用一条字段来表示这条信息是否已经不能使用了
给pikachu表添加一个is_delete字段(bit类型)
>alter table pikachu add is_delete bit default 0;
>update pikachu set is_delete where id=1;


查
查询pikachu表的所有信息
>select * from pikachu;
查询pikachu表age=30的信息
>select * from pikachu where age=30;
查询pikachu表id,name,age列
>select id,name,age from pikahu;
>select pikachu.id,pikachu.name from pikachu;
查询时将age字段设置为‘年纪’
>select age as '年纪' from pikachu;
给表pikachu起别名
>select p.id,p.name,p.age from pikahcu as p;
消除age重复的行
>select distinct age from pikachu;

条件查询
查询pikachu表age>10的信息
>select * from pikachu where age > 10;
查询pikachu表age大于10且小于20的信息
>select * from pikachu where age > 10 and age  < 20;
>select * from pikachu where age > 10 && age < 20;
查询pikachu表age大于20或小于10的信息
>select * from pikachu where age > 20 or age < 10;

查看age=10或age=20
>select * from pikachu where age = 10 or age =20;
>select * from pikachu where age in (10,20);

查看在10-20的范围内
>select * from pikahcu where age between 10 and 20;
>select * from pikachu where age not between 10 and 20;

>select * from pikachu where high = null;查看high是字符串‘null’
>select * from pikachu where high is null;查看high为空
>select * from pikahcu where high is not null: 不为空


模糊查询
--like
--% 代表1个 或 多个 或 没有 字符
--_ 代表1个字符
#查询name中含有a的信息
>select * from pikachu where name like '%a%';
#查询name为2个字符的信息
>select * from pikachu where name like '__';
#查询name至少为2个的信息
>select * from pikachu where name like '__%';

排序
>select * from pikachu wherr sex = 1 and age between 10 and 20 order by age asc;
查看sex=1,年龄10-20,age从小到大,asc:从小到大,不写默认从小到大

>select * from pikachu order by high desc;
desc由大到小

high由大到小,high相同时按age从小到大
>select  * from pikachu order by high desc,age asc;


聚合函数

查看有多少条信息
>select count(*) from pikachu;
查看身高有多少条信息,如果信息为空,不算在内
>select count(high) from pikachu;

>select count(*) from pikachu where sex=1;

>select max(age) from pikachu;
>select name,max(age) from pikachu;是错误用法,用排序

>select max(high) from pikahcu where sex = 1;
>select min(high) from pikachu where sex = 1;

>select sum(age) from pikachu;
求平均(保留2位小数)
>select round(sum(age)/count(*),2) as 'avarge age' from pikachu;
>select avg(age) from pikachu;

分页查询1是第一页,2是显示2条信息
>select * from pikachu limit 1,2;
>select * from pikachu limit 2;
>select * from pikachu limit 0,2;

分组
>select sex from pikahcu group by sex;
select * from tables group by sex;错误的方法
与select distinct sex from 结果相同,意义不同

>select count(*),sex from pikachu group bu sex;
显示已性别为组,每组年龄最大的先分组,在筛条件
>select max(age),sex from pikachu group by sex;

>select sex,group_concat(name) from pikachu group by sex;
>select sex,group_concat(name,'|',high) from pikachu group by sex;
筛选平均age大于20的组
>select sex,group_concat(name),avg(age) from pikahcu group by sex having avg(age) > 20;
筛选组员大于4
>select sex,group_concat(name) from pikachu group by sex having count(*) > 4;
having通常用在分组,先分组,再having


连接查询(内关联)
inner join ... on
select * from table1 inner join table2;不正确,2个表要有联系
>select * from table inner jon table1 on table.cls_id = table1.id;
>select table.name,table1.name from table inner join table1 on table.cls_id = table1.id;
>select 1.name,2.name from table as 1 inner join table1 as 2 on 1.cls_is = 2.id;
>select 1.name as 'a' ,2.name as 'b' from table as 1 inner join table1 as 2 on 1.cls_id = 2.id;

连接查询(左关联,右关联)
--左关联,以左边表为基准,条件对应不上的显示null
>select * from table left join table1 on table.cls_id = table1.id;
--右关联,以后边为基准,条件对应不上的整条信息不显示
>select * from classes left join students on students.cls_id=classes.id; 

自关联
>select * from a as b inner join a as c on b.id=c.ID having name= 'pikachu';错误的
>select b.name,c.name from a as b inner join a as c on b.id = c.ID having c.name = 'pikahcu';或where c.name = 'pikachu'

子查询
标量子查询
查询 aid = 内容为a的pid 的信息
>select * from pikachu where aid = (select bid from pikachu where name = 'a') 

未完待续

猜你喜欢

转载自blog.csdn.net/PpikachuP/article/details/88826818