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的數據記錄

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章