MySQL基本操作命令


數據庫的基本操作命令

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;

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM