網(wǎng)頁設(shè)計報告前言seo研究學(xué)院
基本概念
數(shù)據(jù)庫DB
database簡稱DB: 存儲數(shù)據(jù)的倉庫,是以某種結(jié)構(gòu)存儲數(shù)據(jù)的文件。指長期保存在計算機的存儲設(shè)備上,按照一定規(guī)則阻止起來,可以被用戶或應(yīng)用共享的數(shù)據(jù)集合。
數(shù)據(jù)庫管理系統(tǒng)DBMS
用于創(chuàng)建,維護(hù),使用數(shù)據(jù)庫的一種大型軟件系統(tǒng)。比如MySQL, Oracle, SQL server等等。
使用
MySQL服務(wù)的開啟
方式1:右鍵任務(wù)欄->任務(wù)管理器->服務(wù)->MySQL80->右鍵即可啟動服務(wù)
方式2:CMD命令行操作:小黑窗里面輸入:net start/stop MySQL80來啟動或關(guān)閉服務(wù)。
MySQL的訪問方式
方式1:外部訪問命令 mysql -h主機名 -P端口號 -u用戶名 -p密碼
如果是訪問本機的數(shù)據(jù)庫,可以省略為mysql -u用戶名 -p密碼
方式2:MySQL安裝自帶的命令行
直接輸入賬號的密碼即可登錄,默認(rèn)是主機是本機,默認(rèn)端口號是3306,默認(rèn)賬號是root.
方式3:圖形化窗口,Navicat, SQLyog, MySQLWorkBench等工具。
數(shù)據(jù)類型
數(shù)值類型:
int : tinyint、smallint、mediumint、int
decimal(5, 2): 表示范圍為-999.99~999.99
字符串類型:
char: 定長字符串類型,默認(rèn)長度為1
varchar(50):變長字符串類型
text: 不支持默認(rèn)值
日期類型:
datatime: 存儲的時間不會隨著時區(qū)變化。
timestamp:日期時間戳,到2038年就不能用了。
json類型
update products
set properties = '{"dimension" : [1,2,3],"weight" : 10,"manufacturer" : {"name" : "sony"}
}'
WHERE product_id = 1;
select product_id, json_extract(properties, '$.weight') as weight
from products
where product_id = 1;
SQL語言
分類
- DDL數(shù)據(jù)定義語言
- DML數(shù)據(jù)操作語言
- DQL數(shù)據(jù)查詢語言
- DCL數(shù)據(jù)控制語言
定義庫
-- 查詢所有的庫
show databases;
-- 創(chuàng)建庫
create database db1;
-- 查詢數(shù)據(jù)庫的字符集
show create database;
-- 指定字符集和校對規(guī)則
create database mydb2 character set gbk;
-- 修改字符集
alter database mydb2 character set utf8;
-- 刪除數(shù)據(jù)
drop database mydb2;
--切換庫
use db1;
定義表
-- 創(chuàng)建表
create table student (id int, name varchar(50), age int, gender char);
-- 查看所有表
show tables;
show create table student; -- 查看完成的創(chuàng)建語句
-- 查看表結(jié)構(gòu)
desc student;
-- 修改表名
alter table student rename to stu;
-- 修改字段名
alter table stu change id sid int;
-- 修改數(shù)據(jù)類型
alter table stu modify gender char(2);
-- 新增字段
alter table stu add score double(5, 2);
-- 修改字段的順序
alter table stu modify gender char(2) after name;
-- 刪除表
drop table stu;
DML數(shù)據(jù)操作
- insert添加數(shù)據(jù)
-- 全字段插入
insert into emp
value(101,'tom','男',12000, '1999-02-23', 'boss');
-- 指定字段插入
insert into emp(id, name)
value(102, 'jack');
-- 批量插入
insert into emp(id, name)
values(103, 'rose'),(104, 'pert');
- update更新數(shù)據(jù)
-- 將所有行的該字段數(shù)據(jù)修改為一個值
update emp set salary = salary + 15000;--指定某一條數(shù)據(jù)
update emp set salary = 19000
where name = 'jack';update emp salary = 1000, gender='女'
where name = 'rose';update emp set birthday = '2000-01-01'
where id = 104;
- delete刪除數(shù)據(jù)
delete from emp where name = 'jack';-- 刪除全部數(shù)據(jù),逐條刪除,刪除后可以恢復(fù),主鍵還是繼續(xù)累加的
delete from emp;-- 刪除表后,全新創(chuàng)建一個空的新表,刪除后無法恢復(fù)
truncate emp;
delete和truncate的區(qū)別:
- 刪除全部數(shù)據(jù),逐條刪除,刪除后可以恢復(fù),主鍵還是繼續(xù)累加
- 刪除表后,全新創(chuàng)建一個空的新表,刪除后無法恢復(fù)
DQL數(shù)據(jù)查詢語句
select 關(guān)鍵字的作用:用于運算,執(zhí)行函數(shù),查詢數(shù)據(jù)。
-
基本查詢
select * from emp;
: 查詢emp的所有數(shù)據(jù)select name, salary from emp;
查詢指定字段的數(shù)據(jù)select distinct salary from emp;
去除重復(fù)數(shù)據(jù)select name, salary + 1000 as new_salary from emp;
起別名
-
多個條件復(fù)合查詢
select * from emp where id = 1 or id = 2;
select * from emp where eid in (1, 3, 5);
集合條件查詢
-
區(qū)間條件查詢
select * from emp where salary >= 8000 and salary <= 15000;
select * from emp where salary between 9000 and 15000;
-
帶有NULL的查詢
select * from emp where salary is null;
select * from emp where salary is not null;
select name, IFNULL(salary, 0) + 1000 from emp;
把null值作為0處理。
-
模糊查詢:使用like關(guān)鍵字,不要用等號
- 下劃線 _ 表示任意一個字符
- 百分號 % 表示任意 多個字符
-
排序:關(guān)鍵字order by 字段名,默認(rèn)是ASC升序排序,DESC是降序。
select * from emp order by salary desc;
按照降序排列select * from emp order by id asc;
asc可以省略select * from emp order by salary asc, id desc;
多個條件排序
-
聚合函數(shù)
select count(*) from t_employee where commission_pct is not null;
統(tǒng)計函數(shù)select * from t_employee where salary * (1 + IFNULL(commission_pct, 0)) > 15000;
如果值可能為空的話,要使用IFNULL()方法設(shè)置默認(rèn)值,否則NULL和其他數(shù)字計算還是NULL.select SUM(salary), SUM(commission_pct * salary) from t_employee;
計算工資總和select SUM(salary * (IFNULL(commission_pct, 0) + 1)) from t_employee;
計算帶傭金的工資總和select max(birthday), min(birthday) from t_employee;
查詢年紀(jì)最小和最大的員工
-
分組查詢
select did, count(*) from t_employee group by did;
查詢部門變化和每個部門的人數(shù)select did, sum(salary) from t_employee group by did having SUM(salary) > 40000;
查詢工資總和大于40000的部門編號及工資和。select did, SUM(salary) sm from t_employee where gender = '女' group by did having sm > 20000;
查詢部門女員工工資總和大于20000的部門編號和工資總和。
-
Limit m, n 關(guān)鍵字
- m 表示查詢的起始索引,n表示需要查詢的記錄數(shù)
select * from emp limit 0, 5;
查詢前5條記錄select * from emp limit (x-1) * n , n;
查詢第x頁的記錄,每頁有n條記錄。