安裝步驟:
如附件:https://download.csdn.net/download/weixin_44180787/12702303
啓動和停止mysql服務:
windows 開啓mysql:
net start mysql57
windows 關閉mysql:net stop mysql57
進入MySQL:mysql -u root -p
退出:exit、quit
檢視所有數據庫:show databases;
建立數據庫:create database 數據庫名 charset=utf8;
進入數據庫:use 數據庫名;
檢視當前使用的數據庫名:select database();
刪除數據庫:drop database 數據庫名;
檢視所有表:show tables;
建立表(標準):create table 表名(列欄位1,列欄位2……)
create table user(
id int primary key auto_increment,
name varchar(20),
age int
);
說明:
1、primary key:主鍵,確保該欄位的數據不能重複。一個表可有多個主鍵,建議一個表一個主鍵。
2、auto_increment: 自動增長(+1),用於整數。只增不減,把前面生成的刪除後新增加的還是從後遞增。
3、varchar(M): 可變字串型別。M取值:0~65535,varchar最大實際長度由最長行的大小決定。
4、char(M):固定長度的字串。必須指定長度,若輸入字串長度不夠,會在右邊用空格填充。
檢視錶結構:desc 表名;
查詢表數據:詳細如後面查詢部分;
刪除表:drop table 表名;
語法:insert into 表名(列名1, 列名2, …) values(值1, 值2, ……)
說明:
1.查詢姓名由3個字元組成的記錄,並且最後一個字母爲c的記錄:
select * from student where name like '__c';
2.查詢以a開頭的記錄:
select * from student where name like 'a%';
3.查詢姓名中包含b的記錄:
select * from student where name like '%b%';
select distinct name from user;
select name as 姓名, age as 年齡 from user;
select name 姓名, age 年齡 from user;
1.將tom的薪水改爲3000元:
update worker set salary=3000 where name='tom';
2.在原數據基礎上增加:如工資加1000:
update worker set salary=salary+1000 where name='rose';
3.將所有員工的薪水修改爲5000:
update worker set salary=5000;
delete、truncate是用來刪除表中數據的,不是刪除表;drop是刪除整個表!
1.刪除id爲102的數據:
delete from user where id=102;
2.刪除表中的所有數據:
mysql> delete from worker;
不帶where是刪除整個表的數據,但是不會刪除表,也不會清空auto_increment欄位計數,重新插入還是會從原計數開始遞增。
1.刪除表數據worker:
truncate table worker;
1.刪除表worker:
drop table worker;
注意:where子句其實就是一個操作符,類似於Python中的 if 語句,可以做數據的篩選。
操作符 | 說明 |
---|---|
= | 相等 |
<> / != | 不相等 |
< | 小於 |
<= | 小於等於 |
> | 大於 |
>= | 大於等於 |
IN(A,B) | A 和 B 之間 |
between A and B | 位於兩值之間 |
AND | 連線多個表達式 並且的關係 |
1.查詢所有的記錄,按照年齡升序排序:
select * from student order by age asc;
2. 降序
select * from user order by age desc;
3.多欄位組合排序
select * from user order by name desc, age asc;
4.先篩選再排序
select * from user where id>1 order by age asc;
select count(name) from user;
select count(*) from user;
select count(age) from user;
select sum(age) from student;
select max(age) from user;
select min(age) from user;
select avg(age) from user;
group by分組後將表數據分成幾部分,用count()這些聚合函數進行計算時,每個部分會單獨計算一次。
1.查詢各個部門的人數;
select deptno, count(*) from emp group by deptno;
2. 查詢每個部門的部門編號和每個部門的工資和;
select deptno, sum(sal) from emp group by deptno;
3. 查詢每個部門的部門編號和每個部門的人數;
select deptno, count(*) from emp group by deptno;
4.查詢每個部門的部門編號和每個部門工資大於1500的人數;
select deptno, count(*) from emp where sal>1500 group by deptno;
5.查詢工資總和大於7000的部門編號以及工資和;
select deptno,sum(sal) from emp group by deptno having sum(sal)>7000;
6.查詢員工工資大於1500,工資總和大於6000的部門編號和工資和;
select deptno, sum(sal) from emp where sal>1500 group by deptno having sum(sal)>6000;
llimit a, b;
a:表示頁數,頁碼爲:a=(a-1)*b
b:表示每頁顯示的數量。
1.取前3行內容:0預設不寫
select * from emp limit 3;
2.跳過前3個,再取4行:
select * from emp limit 3,4;
select * from emp limit 6,3;
select deptno, sum(sal) from emp where sal>1500 group by deptno having sum(sal)>2000 order by sum(sal) asc limit 2;
書寫順序:select =》from =》where =》group by =》having =》order by =》limit
執行順序:from =》where =》group by =》having =》order by =》limit =》select
where 和having的區別: