mariadb 基础使用

MariaDB 基础使⽤
mariadb的基本概念
DQL 数据查询语言 select
DDL 数据定义语言 create drop alter
DML 数据操作语言 insert delete update
DCL 数据控制语言 grant revoke commit rollback

使⽤ MariaDB 的 root ⽤户登陆,然后使⽤ show databases 命令查看当前数据库服务中都有哪些具体的库 database
1、登陆
    mysql -uroot -p密码           #登陆myriadb

2、显示数据库
show databases; #显示数据库有哪些?

使⽤ use 命令可以指定当前要操作的默认数据库,之后可以使⽤ show tables 查看当前默认数据库下的所有表信息。 mysql 库是 MariaDB 的
内部管理数据库,存放着很多和 MariaDB 相关的信息。该库中的 user 表存放着所有 MariaDB 的⽤户信息。
1、use mysql;#使用mysql数据库
2、show tables;#展示mysql数据库下的所有表
3、desc user;展示mysql数据库下user表结构
4、select User, Host, Password from user;#展示user表中用户名,主机名,密码字段

创建和删除数据库
create database 库名称;#创建库
drop database 库名称; #删除库

创建表
create table 表名; #创建表
drop table 表名; #删除表
例子
create table student(
stuid int(10) unsigned not null auto_increment primary key,
name varchar(50) not null,
gender enum('F',"M") not null
)
engine=innodb default charset=utf8;

表结构修改: alter table 表名;
alter table student add phone varchar(50) not null; #增加字段

alter table student drop gender; #删除gender字段

alter table student change phone phone varchar(20) not null; #change用来修改字段名称为phone1和属性修改为varchar(20)

alter table student modify name varchar(20) not null; #modify只能修改字段属性

使⽤ DML 管理数据库中的表数据
使⽤ insert 语句在 school 库的 student 表中添加数据记录

insert into student (Name, phone) values ("gordon", "15201039555"); #插入值

在表中添加数据格式为 insert into 表名 [(对应的字段列表,如果不写代表默认所有字段)] values ( 对应字段中每⼀个字段的具体值 ) ;
查看表中的数据可以使⽤ select from 表名 ,⼀般不建议写 号,他代表所有字段,表数据内容⽐较少时可以测试使⽤

使⽤ delete 删除表中的指定数据记录,delete 后不加 where 条件代表删除所有数据,⼀般我们操作时肯定要指明过滤条件的,删除表中的数据时,对应的 where 判断条件可以根据实际需要写的很复杂,具体的细节查看官⽅⽂档。

delete from student; #删除student表内所有数据
delete from student where name="mimi"; #指定删除名字为mimi的数据记录
delete from student where phone="123"; #指定删除电话为123的数据记录

使⽤ update 语句修改表中的数据记录

格式为 update 表名 set 字段=值 where 过滤条件

update student set phone="123" where name="gordon"; #修改名字为gordon的电话为123
update student set name="hehe" where phone="123"; #d修改电话为123的名字为hehe

使⽤ DQL 单表数据查询

数据库查询可以使⽤ select 完成,
格式为 select 字段列表 from 表名 where 过滤条件, 
其中字段列表可以使⽤ * 号 代表所有字段

select * from student; #查看student表的所有字段

select stuid,phone from student; #查看student表中的stuid和phone字段的数据记录

select stuid,phone from student where stuid >6; #查看student表中满足stuid>6的stuid和phone字段

对结果进⾏排序, 使⽤ order by 排序字段名 即可, 默认顺序,使⽤ desc 实现逆序

select stuid,phone from student order by phone; #按电话排序
select stuid,phone from student order by phone desc; #按电话实现逆序

限定只需要返回结果的前⼏条记录,使⽤ limit 记录调试 

select * from student limit 2; #只显示前两条记录

对指定重复的字段进⾏去重, 使⽤ select distinct 去重的字段 from 表名 where 判断条件 即可, distinct 后可以是⼀个字段,也可以是多个字段,当为多个字段时代表他们的组合不能重复, * 号 代表 所有字段的组合不能重复

select distinct name,phone from student; #从student表中对名字和电话都重复的记录做去重处理

查询时对数据进⾏分组,查询时以 student 表中的 姓名进⾏分组,然后处理改组⾥的数据,使 ⽤ group by 分组的字段名 即可实现,分组后显⽰的字段值默认为分组中第⼀次出现的记录信息值,因此⼤多数要显⽰的字段都需要进⾏运算

select * from student group by name; #对姓名进行分组

select count(stuid),name,phone from student group by name; #对姓名进行分组后,统计学号的数量

where 可以对单⾏数据进⾏过滤, having 可以对分组处理后的数据再次进⾏过滤

select count(stuid),name,phone from student group by name having phone="133"; #按名字分组后再次进行过滤筛选出电话为133的记录

使⽤ DQL 进⾏多表查询数据

多表连接查询中的内连接查询,也叫等值连接 使⽤ select 字段名 from 表名1 inner join 表名2 on 表名1.字段=表名2.字段
内连接查询两个表,inner join 前⾯的表为参照表,按照参照表的顺序匹配⽣成 
查询结果,参照表中如果有不匹配 on 条件的记录,则跳过该记录。

select * from class inner join student on
student.classid=class.classid;

外连接查询,左外链接查询和内连接查询的差异在于,如果参照表中有不匹配 on 条件的记录,则也会 
⽣成对应的数据,结果为参照表信息和空信息组成查询记录。使⽤ 表1 left join 表2 on 条件 即可

select * from student left join class on student.classid=class.classid;

右外连接查询和上⾯的左外链接查询参照表不⼀样,左外链接以左边的表为参照,右外连接以右⾯的表为参照 
使⽤ 表1 right join 表2 on 条件 即可

select * from student right join class on student.classid=class.classid;

嵌套查询,⽤⼀次查询的结果作为查询匹配条件的范围或者作为⼀个新表来被再次查询

select * from student where student.classid in (select classid from class where classid <3); #括号内为一次查询结果,此处查询结果作为查询匹配条件的范围使用

select a.stuid,a.name,a.classname from (select student.stuid,student.name,student.gender,class.classname from student inner join class on student.classid=class.classid)as a;
#将括号内的作为整体赋给变量a,通过a.字段,引用查询结果内的字段,此处查询结果赋给变量作为一个新表再次查询

合并查询结果,当多个查询的结果⼜相同个数和内容的字段时,使⽤ union 会将这些查询结果去重合并

select from student where stuid <=3 union select from student where stuid >=3; #合并去重stuid=3的数据记录

猜你喜欢

转载自blog.51cto.com/14231434/2390084