MySQL基本操作命令
阿新 • • 發佈:2020-10-10
資料庫的基本操作命令
1.登入MySQL
-- 進入資料庫的方法一
mysql -uroot -pmysql # mysql 資料庫密碼(顯示)
-- 進入資料庫的方法二
mysql -uroot -p # 隱藏密碼輸入
2.資料庫的基本操作
-- 顯示資料庫版本(記得加;
select version();
-- 顯示當前的時間
select now();
-- 檢視所有資料庫
show databases;
-- 建立資料庫
create database 資料庫名 charset=utf8;
-- 建立淘寶資料庫
create database taobao;
-- 建立淘寶資料庫並指定編碼
create database taobao charset=utf8;
-- 檢視建立資料庫的語句
show create database school
3.使用資料庫taobao資料庫
-- 使用資料庫
use school;
-- 顯示資料庫中所有的表
show tables;
--刪除資料庫
drop database school;
4.資料表的基本操作
-- auto_increment :自動增長
-- not null :表示不為空
-- primary key :表示主鍵
-- default :預設值
-- 檢視當前的資料庫中所有的表
-- show tables;
-- 建立students資料表
create table students(
id int unsigned not null auto_increment primary key,
name varchar(50) not null default "張三",
age tinyint unsigned not null default 18,
high decimal(5,2) not null,
gender enum("男", "女", "保密")default "保密",
cls_id int unsigned not null
);
-- 插入一條資料到students表中
insert into students values(0, "mike", 18, 145,"保密",2)
-- 查詢students表中的所有的資料
select * from students;
-- 檢視建立表的語句
show create table students;
-- 刪除表
drop table students;
-- 查看錶的欄位
desc students;
-- 新增表的欄位
alter table students add birth datetime;
-- 修改欄位:不改變欄位名字
alter table students modify birth date;
-- 修改欄位:不重新命名版
alter table students change birth birthday date default "2020-01-01";
-- 刪除欄位
alter table students drop cls_id;
-- 插入資料 insert into 表明 value(...)
-- 主鍵可以用 0 null default來佔位
insert into students values(null, "lily", 22, 168, 2, "1990-01-01");
-- 部分插入
insert into students(high) values(172);
-- 多行插入
insert into students(name, high) values("李四", 178),("老王", 1.44);
-- 多行插入全部資料
insert into students values(null, "lily", 23, 173, 2, "1990-01-01"), (null, "xiao", 22, 189, 2, "1990-02-03");
-- 修改表
-- 修改全部年齡
update students set age= 30;
-- 修改指定id的年齡
update students set age=28 where id=1;
--查詢表的內容
select * from students;
-- 定條件查詢
select * from students where id=2;
select * from students where id>=1 and id<=3;
--查詢指定的列
select id, name from students where id>=1 and id<=3;
select name, id from students where id>=1 and id<=3;
-- 可以用as來為列表指定別名(顯示出來的名字就是指定的名字)
select name as 姓名, id as 學號 from students where id>=1 and id<=3;
-- 物理刪除
delete from student where id=6;
--邏輯刪除(用新的欄位作為條件限制顯示資訊)
alter table students add is_delete bit default 0;
-- 把id=1的is_delete改為1
update students set is_delete=1 where id=1;
-- 查詢然後條件限制為is_delete=0 就可以隱藏資料
select * from students where is_delete=0;
5.資料表的查詢操作
-- 查詢所有欄位
select * from students;
-- 查詢指定欄位
select name, age from students;
-- 給欄位起別名(用別名顯示)
select name as 姓名, age as 年齡 from students;
-- 從指定的表中尋找指定的欄位
select students.name, students.age from students;
-- 用as起別名再用別名呼叫欄位
select s.name, s.age from students as s;
-- 利用distinct欄位消除重複行
select distinct gender from students;
-- 條件查詢(比較運算子)
select * from students where age>19;
select * from students where age<19;
select * from students where age!=18;
-- 條件查詢(邏輯運算子)
select * from students where age>=17 and age<=27;
select * from students where age>=13 or high>=159;
select * from students where not(age<=17 and gender=2);
-- 模糊查詢
-- 查詢以"李"開頭的所有名字
select * from students where name like "李%";
-- 查詢以"王"字結尾的所有名字
select * from students where name like "%王";
-- 查詢有"三"的所有名字
select * from students where name like "%三%";
-- 查詢有兩個字的名字
select * from students where name like "__";
-- 查詢有三個字的名字
select * from students where name like "___";
-- 查詢至少有兩個的名字
select * from students where name like "%__%";
-- 空判斷is null
select * from students where high is null;
6.資料表內資料的排序
-- order by 欄位 查詢改欄位內的的排序
-- asc從小到大排序,默然從小到大
-- desc 從大到小排序
select * from students where (age between 18 and 26)and gender=2 order by age;
-- 查詢students表中,年紀17到30歲的女性 身高從高到矮
select * from students where (age between 17 and 30) and gender=2 order by high desc;
-- order by 多個欄位
-- 查詢students表中,按high 降序, age升序
select * from students where(age between 17 and 37) and gender=2 order by high desc ,age asc;
7.資料表的集合函式
-- 聚合函式
-- count(*)統計列數,count(欄位)一樣
select count(*) from students where gender=2;
-- 最大值,最小值,求和,平均
select max(age), min(age),sum(age),avg(age) from students;
8.分組
-- group by 按照性別分組
select gender from students group by gender;
-- 在students表中,計算每個性別的人數
select gender, count(*) from students group by gender;
--在students表中,通過性別分組顯示名字
select gender, group_concat(name) from students group by gender;
-- group by + having :用來分組查詢後指定一些條件的查詢結果
select gender,count(*) from students group by gender having count(*)>2;
9.分頁
-- 查詢前3行女生的資訊
select * from students where is_delete=0 limit(n-1)*m,n
select * from students where gender=2 limit 0,3;
10.連線查詢
-- 先建立一個班級表
-- 內連線查詢班級表和學生表
select * from students inner join classes on students.cls_id=classes.id;
-- 左連線查詢班級表和學生表
select * from students as s left join classes as c on c.cls_id=c.id;
-- 右連線查詢班級表和學生表
select * from students as s right join classes as c on s.cls_id=c.id;
11.自關聯
-- 表中的某一列,關聯表中的另一列,這就是自關聯
12.子查詢
-- 在一個select語句中,嵌入另外一個select語句,那麼嵌入的select語句被稱為子查詢語句
-- 子查詢是嵌入到主查詢中
-- 子查詢是輔助主查詢的,要麼充當條件,要麼充當資料來源
-- 子查詢是可以獨立存在的語句,是一條完整的 select 語句
-- 標準子查詢
select * from students where age > (select avg(age) from students);
-- 列級子查詢
select name from classes where id in (select id from students);
-- 行級子查詢
select * from students where (height,age) = (select max(height),max(age) from students);
13.檢視
-- 有個查詢結果,這個結果表作為建立檢視基礎,這個結果中不能出現同名欄位
select g.id, g.name, c.name as cate_name, b.name as brand_name, g.price
from goods as g inner join goods_brands as b on g.brand_id=b.id inner join
goods_cates as c on c.id=g.cate_id;
-- 新建了一個檢視,這個檢視它是一個虛擬表,這個表字段是原表字段的引用,可以簡單理解為軟連結
create view v_info as select g.id, g.name, c.name as cate_name, b.name as brand_name, g.price
from goods as g inner join goods_brands as b on g.brand_id=b.id inner join
goods_cates as c on c.id=g.cate_id;
-- 刪除檢視
drop view v_info
14.事務
1.原子性
一個事務必須被視為一個不可分割的最小工作單元,整個事務中的所有操作要麼全部提交成功,要麼全部失敗回滾,對於一個事務來說,不可能只執行其中的一部分操作,這就是事務的原子性
2.一致性
資料庫總是從一個一致性的狀態轉換到另一個一致性的狀態。(在前面的例子中,一致性確保了,即使在執行第三、四條語句之間時系統崩潰,支票賬戶中也不會損失200美元,因為事務最終沒有提交,所以事務中所做的修改也
不會儲存到資料庫中。)
3.隔離性
通常來說,一個事務所做的修改在最終提交以前,對其他事務是不可見的。(在前面的例子中,當執行完第三條語句、第四條語句還未開始時,此時有另外的一個賬戶彙總程式開始執行,則其看到支票帳戶的餘額並沒有被減去200美元。)
4.永續性
一旦事務提交,則其所做的修改會永久儲存到資料庫。(此時即使系統崩潰,修改的資料也不會丟失。)
-- 開啟事務
begin;
start transaction;
-- 提交事務
commit;
-- 回滾事務
rollback;
15.索引
索引是一種特殊的檔案(InnoDB資料表上的索引是表空間的一個組成部分),它們包含著對資料表裡所有記錄的引用指標。
更通俗的說,資料庫索引好比是一本書前面的目錄,能加快資料庫的查詢速度(建立索引會縮短執行的時間)
-- 檢視索引
show index from 表名;
--建立索引
create index 索引名稱 on 表名(字元段名稱(長度))
--刪除索引:
drop index 索引名稱 on 表名;
--查詢
--開啟執行時間
set profiling=1;
--檢視執行時間
show profiles;
謝謝