Mysql 學習思維導圖

Mysql常用sql總結(SQL學習思維導圖)

https://blog.csdn.net/qq_42651904/article/details/96275284

mysql常用函數總結(完整實用) https://blog.csdn.net/qq_42651904/article/details/101640852

文章目錄

基本SQL語句
查看數據庫:show databases;
使用數據庫: use 數據庫名;
創建數據庫:create database 數據庫名
查看所有表名: show tables;
刪除數據庫 drop database 數據庫名
刪除表 : drop table t_student
查看錶結構:desc 表名
查看數據庫版本:select version();
查看錶索引:show index from 表名
索引的創建與使用
查看系統事物隔離級別: select @@global.tx_isolation;
查看當前會話事物隔離級別:select @@tx_isolation;
設置當前會話事物隔離級別: set session transaction isolatin level repeatable read;
設置系統當前隔離級別:set global transaction isolation level repeatable read;
開始事物: start transaction
提交事物:commit
回滾事物:rollback
常用函數
創建存儲過程
 create procedure 存儲過程名字(參數)
BEGIN
存儲過程體
END

 

創建表:
drop table if EXISTS`t_student`;
create table t_student
(
-- 設置stuid爲主鍵,自動增加
    stuid int primary key auto_increment COMMENT  '主鍵',
-- 設置約束不能爲空
    stuname char(20) not null COMMENT  '姓名',
    stuage int  COMMENT  '年齡',
-- 設置stuphone 唯一
    stuphone char(11) unique,
-- 設置stusex 默認爲男
    stusex char(4) default '男',
-- 自動獲取系統時間
    writerDate TIMESTAMP  default CURRENT_TIMESTAMP,
-- 設置外鍵爲 t_teacher.tid
    tid int references t_teacher.tid

創建teacher表
drop table if EXISTS`t_teacher`;
create table t_teacher
(
    tid char(20) primary key,
    tname char(20),
-- 設置聯合主鍵
    primary key(tid, tname)
);
1
2
3
4
5
6
7
8
對錶追加字段:
-- alter table 表名 add (字段名 字段類型 默認值 是否爲空);
alter table t2a_cust_c add (LEGALREPNAME varchar2(120));
comment  on  column  t2a_cust_c.LEGALREPNAME   is  '代理人姓名';
1
2
3
修改字段
alter table 表名 modify (字段名 字段類型 默認值 是否爲空);
1
刪除字段
alter table tablename drop (column);
1
字段的重命名
alter table 表名 rename  column  列名 to 新列名   (其中:column是關鍵字)
1
表的重命名
alter table 表名 rename to  新表名
1
創建視圖
DROP view IF EXISTS t_student;
create or replace  view v_studentas
select * from v2a_trans ;
 
添加索引
ALTER TABLE t_studentADD INDEX (`stuname `);
 
參考

數據庫常用SQL
-- 插入
insert into t_student(stuid, stuname, stuage, stuphone, stusex, tid) values (1, '張三', 22, '15645663891', 'man', 10086)
 insert into t_student(stuid, stuname, stuage, stuphone, stusex, tid) values (3, 'ww', 22, '15689563891', 'man', 10086)
-- 刪除
 delete from t_student where stuid = 3;
-- 查詢
 select stuid ,stuname,stuage from t_student;
-- 修改
update t_student set stuname = '李奏' ,stuage = 16, stusex = '女' where stuid = 1;
-- 查詢員工工資高於5000的員工姓名
select ename from emp where sal > 5000;

--  多個條件查詢
-- 查詢員工工資高於5000,同時獎金少於2W的員工
select ename from emp where sal > 5000 and comm < 20000

-- 查詢員工工資高於5000,或者獎金少於2W的員工
select ename from emp where sal > 5000 or comm < 20000
-- 查詢員工工資高於5000低於20000
select ename from emp where sal between 5000 and 20000

--  模糊查詢
-- 找出姓張的員工,%代表後面字符數>=0
select ename from emp where ename like '張%'

-- 找出姓張的員工,但名字總共只有兩個字符,_代表後面字符數 ==1
select ename from emp where ename like '張_';

--  查詢出員工的年薪 (表達式查詢)
select sal * 12 + comm as totalSalary from emp;

-- 查詢出員工名及其工資,格式如下:“張三:$1000”
select CONCAT(ename,':$1000', sal) from emp;

-- .函數查詢 :統計函數 avg(), min(), count(), max() 
-- 計算公司員工的平均工資
select avg(sal) avgSalary from emp;
select sum(sal) sumSalary from emp;
select min(sal) minSalary from emp;
select max(sal) maxSalary from emp;
select count(*) avgSalary from emp;

-- 5. 分組查詢 group by 分組查詢select後面只能寫group by 後面的字段
-- 求出每個部門的平均工資, 總工資
select deptno, avg(sal),sum(sal) from emp group by deptno;

-- where只能接字段名(列名)
-- 而having接統計函數
-- 6. 求出每個部門的平均工資,總工資,但只顯示工資超過1W的部門
select deptno, avg(sal), sum(sal) from emp group by deptno having sum(sal) > 10000;

-- 7.限制記錄的條數limit
-- 查詢第五條到第七條數
-- 後面兩個數字代表從哪個索引開始選,選擇幾條
select * from emp limit 4,3; 

-- 8.排序order by asc/desc 升序,降序
select *from emp order by sal asc;
select *from emp order by sal desc;

-- 找出工資最高的前三名
select *from emp order by sal desc limit 0,3

鍵的六大約束
類型    用法    說明
主鍵    primary key    唯一的,不重複的,不能爲空
非空    not null    不爲空必須填寫
唯一    unique不能重複,可爲空    
默認    default    不插入,則默認爲默認約束
檢查    check    檢查數據合法性
外鍵    references    將不想相關的表關聯起來
思維導圖

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