3W1H(what纳击?why朱灿?when昧识?how?)
1盗扒、什么是數(shù)據(jù)庫(kù)跪楞?DB
? ? ? ? 數(shù)據(jù)庫(kù)(database)數(shù)據(jù)存儲(chǔ)的倉(cāng)庫(kù),簡(jiǎn)稱DB
????????DBMS(databaseManagers)數(shù)據(jù)庫(kù)管理的軟件
2侣灶、為什么學(xué)習(xí)數(shù)據(jù)庫(kù)甸祭?
? ? ? ? 方便存儲(chǔ)使用數(shù)據(jù)
3、數(shù)據(jù)庫(kù)在軟件開(kāi)發(fā)的地位褥影?
? ? ? ? 數(shù)據(jù)存儲(chǔ)層
4池户、安裝MySQL
|-- window:
? ? ? ? 1、使用壓縮包安裝需要把bin配置到環(huán)境變量path ? 注意解壓路徑不能包含特殊字符和中文
? ? ? ? 2凡怎、以管理員身份打開(kāi)cmd 切換盤符到 文件解壓的那個(gè)盤符下 ?指令:盤符:
? ? ? ? 3校焦、
? ? ? ? 4、重啟 net start mysql
? ? ? ? 5统倒、mysql -h(ip地址) ?-p(端口=3306) ?-u(用戶) ?-p(密碼)
? ? ? ? 6寨典、配置my.ini文件中的basedir 和 datadir
|-- Linux
? ? ? ? 1、sudo apt ?install ?mysql ?--server mysql cilent?
? ? ? ? 2檐薯、安裝路徑: /user/sbin ? ? 配置路徑:/etc
? ? ? ? 3凝赛、查看進(jìn)程 ps -ef | grep mysql
????????????????安裝ssh 指令 apt install openssh-server
? ? ? ? ? ? ? ? ?which 應(yīng)用名稱 ?查看安裝路徑
? ??????Linux下遠(yuǎn)程連接MySQL:
????????????????1、修改mysql庫(kù)中user表中的host字段坛缕,有l(wèi)ocalhost? %
????????????????2墓猎、修改/etc/mysql/mysql.conf.d/mysqld.cnf文件
????????????????3、重啟mysql
????????啟動(dòng)mysql的四個(gè)參數(shù) ?-h主機(jī)(默認(rèn)localhost) ?-P端口(默認(rèn)3306) -u用戶 ?-p密碼
mysql在linux下是默認(rèn)關(guān)閉遠(yuǎn)程連接的(安全)開(kāi)啟遠(yuǎn)程連接
????????????update mysql.user set host='%' where user="root"
????????????????linux將配置文件中mysql.cnf文件????????/etc/mysql/mysql.conf.d/mysql.cnf????????bind-address 注釋掉
? ? ? ? ? ? ? ? ?重啟mysql????????????sudo service restart mysql????????????????????ps -ef |grep mysql
5赚楚、簡(jiǎn)單的MySQL的操作
? ? ? ? 常見(jiàn)的DDL語(yǔ)句
? ? ? ? ? ? ? ? show databases ?查看所有數(shù)據(jù)庫(kù)
? ? ? ? ? ? ?create database db_py ?創(chuàng)建db_py數(shù)據(jù)庫(kù)
? ??????????????drop databse ?databaseName ?刪除數(shù)據(jù)庫(kù)
????????????use db_py 使用db_py數(shù)據(jù)庫(kù)
? ??????????????show tables 查看當(dāng)前數(shù)據(jù)庫(kù)下的表(還沒(méi)有創(chuàng)建表)
????????????????創(chuàng)建表 ?
? ? ? ? ? ? ? 刪除表 drop table tableName
? ??????????????????drop table if exists 表名稱 ????? ?truncate [table] 表名稱(刪除后不可恢復(fù)) ? ? ? ? ??delete from 表名(刪除后有辦法恢復(fù))
? ? ? ? ? ? ? ? ? ? desc tableName; 查看表結(jié)構(gòu)
6?Mysql的數(shù)據(jù)類型整數(shù)?
????????int ??浮點(diǎn)數(shù) float decimal ? ? ?字符串 char varchar ? ?文本類型 text ? ?
????????日期 data(只能表示年月日)毙沾、time (只能表示時(shí)分秒)、datatime(年月日時(shí)分秒)
? ? ? ? ? ?binary 二進(jìn)制 ? ?布爾 bit
7宠页、Mysql的創(chuàng)建左胞、刪除、授權(quán)用戶(DBA)
????????grant all[select,update,drop,create] on *.* to ‘用戶名稱’@’地址’ identified by 密碼
????如:?grant all on db_py1710.*? to? ‘cmt’@’%’? identified by? ‘cmtt’
????刪除用戶 ?use mysql ? ? ?Delete from user where user=‘cmt’ and host=‘%’
8举户、數(shù)據(jù)庫(kù)的備份
????????????mysqldump 命令
????????????mysqldump –u root –p dbname > 保存路徑.sql
? ? ? ? ? ? mysqldump –u root –p db_bbs > d:/db_bbs.bak.sql
????????????????????????[將數(shù)據(jù)庫(kù)db_bbs以腳本的形式保存到D盤]
????????????????????????將sql腳本還原為數(shù)據(jù)庫(kù)
????????????????????????mysql –u root –p dbname < 保存路徑
9烤宙、sql(CRUD 增刪改查)
? ? ? ? ?插入一條數(shù)據(jù) insert into tableName(字段1,字段2俭嘁,躺枕,,) values(值1,值2拐云,罢猪,,)
? ?????? ????????????insert into t_stu values(null,'laowang',36);
????????????????????insert into t_py1712 values(uuid(),'laowang',36);
?????????????????select * from tableName;
????????刪除一條數(shù)據(jù) ?delete ?from tableName where 條件叉瘩;
? ? ? ?修改數(shù)據(jù) update tableName set 字段1=‘’膳帕,字段2=‘’,薇缅,危彩, where 條件
? ?????????????????????? update t_py1712 set name='xiaoming' where age = 18;
? ? ? ? 修改表結(jié)構(gòu) alter?
? ? ? ? ? ? ? ? ? ? ? ? alter table t_py1712 add className varchar(10) not null; 增加一個(gè)字段
????????????????????????????????????alter table t_py1712 drop className; 刪除字段
????????????????????????????????????alter table t_py1712 modify column age int default 18 not null; ? 修改字段
? ? ? ? ? ? 查詢select
????????????????????????????? select name from t_py1712;
? ? ? ? ? ? ? ? ? ? ? ? ? ?1、條件查詢?
? ??????????????????????????????????關(guān)系運(yùn)行符 > 泳桦、< 恬砂、=、>=蓬痒、<= 、!=或者<>表示不等于
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? where 查詢
?????????????????????????????????????? ? ? ? ?select * from t_py1712 where age > 23;
????????????????????????????????????????select * from t_py1712 where age in(23,20);
? ??????????????????????????????select * from t_py1712 where age between 20 and 30;
? ? ? ? ? ? ? ? ? ? ? ? and ? select * from t_py1712 where name='zhangsan' and age >21;
? ? ? ? ? ? ? ? ? ? ? ? ? ? or????????????????select * from t_py1712 where name='zhangsan' or age=21;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? is ? ? select * from t_py1712 where name is null;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? is not null ?select * from t_py1712 where name is not null;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?聚合函數(shù) sum ? ??select sum(age) from t_py1712;
????????????????????????????????????????????????select max(age) from t_py1712; ?
? ?????????????????????????????????????????????? select min(age) from t_py1712;
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?select avg(age) from t_py1712;
? ?????????????????????? select count(age) from t_py1712; ? 統(tǒng)計(jì)這個(gè)表的總記錄數(shù)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? as重命名 select name as n from t_py1712 as t_py;
? ? ? ? ? ? ? ? ? ? ? ? ? 模糊查詢? like : ? ? ? ? ? ?
????????????????????????????????????????????????select * from t_py1712 where name like 'l%';
????????????????????????????????????????????select * from t_py1712 where name like '%o%';
????????????????????????????????????select * from t_py1712 where name like '_i%';
排序 order by ? ? ? ? ?select* from 表名? where 條件 order by 字段名稱 [asc|desc]
????????????????????????????select * from t_py1712 where name like '%' order by age;
分組 group by ? ? ?查詢部門員工的男女?dāng)?shù)量 ? ? ??SELECT sex, COUNT(*) from `user` GROUP BY sex
Having 篩選
????????????????SELECT age,COUNT(age) from stu GROUP BY age HAVING COUNT(age)>1;
? ? ? ? ? ? ? ? ? having 是分組后的篩選
去重 distinct ? ? ? ?select distinct 字段 from 表名漆羔;
????????????????????????????????????????????????????????????????????select distinct sex from t_py1712;
Limit ?分頁(yè)顯示
Select * from users limit 0,10 #查詢前十條數(shù)據(jù)
Select * from users? limit (pageNow -1)*pageSize, pageSize
????????????????select * from t_py1712 limit 0,3;
外鍵 foreign key 完成外鍵的約束
CONSTRAINT FOREIGN key(新表的字段) REFERENCES? 主表(引用字段)
子查詢 ? ?將一個(gè)查詢結(jié)果作為條件的一部分寫入到第二個(gè)sql中
如 姓名為zs的用戶的部門名稱:?Select deptname from dept where deptid = (select deptid from emp where empname = ‘zs’)
多表關(guān)聯(lián)—連接查詢
內(nèi)連接查詢(inner join) ? ?????????等值查詢????????不等值查詢 ??????????外連接查詢(outer join)????????左外連接(left join)
右外連接(right join)????????????????自然連接????????????交叉連接(cross join) # 笛卡爾積????????????????????自連接
字符串函數(shù)
length梧奢、char_length、trim演痒、substring亲轨、ascii、concat鸟顺、upper惦蚊、replace
????????????????????????????????????????????????????select length((select name from t_py1712 where age = 22));
數(shù)學(xué)函數(shù)
ceil、floot讯嫂、round蹦锋、mod、sin欧芽、cos莉掂、sqrt
? ? ? ? 1、format(x,y) ?函數(shù)千扔,功能是將一個(gè)數(shù)字x憎妙,保留y位小數(shù),并且整數(shù)部分用逗號(hào)分隔千分位曲楚,小數(shù)部分進(jìn)行四舍五入厘唾。
? ? ? ? ?2、abs(); ?求一個(gè)數(shù)的絕對(duì)值龙誊;absolute
? ? ? ? ? 3抚垃、sqrt();求一個(gè)數(shù)的平方根。sqrt是sqruar(平方,矩形) 讯柔,root(根)的縮寫抡蛙。 select sqrt(9); ==3
? ? ? ? ? 4、mod(x,y) ?x除數(shù)魂迄,y被除數(shù)粗截。結(jié)束是余數(shù)。select mod(10,3);===1
? ? ? ? ? 5捣炬、ceil() ?進(jìn)一取整熊昌。? ? select ceil(5.1);?===6
????????????????floor()舍一取整 ? ? select floor(5.3);===5
? ? ? ? ? ? ? ? ? ?這兩個(gè)函數(shù)是鏡子函數(shù),比較有點(diǎn)意思湿酸。這兩個(gè)函數(shù)并不進(jìn)行四舍五入婿屹,比較強(qiáng)硬。
? ? ? ? ? ?6推溃、rand() ?顧名思義昂利,是用來(lái)生成隨機(jī)數(shù)用的。select rand();
? ? ? ? ? ? 8铁坎、truncate(x,y) ?比較霸道蜂奸,不管四舍五入,直接把x硬萍,的y位小數(shù)直接干掉扩所。 select truncate(1233.154585,3);==1233.154
????????????9、sign() 返回當(dāng)前結(jié)果得符號(hào)朴乖,如果是負(fù)數(shù)返回-1祖屏,如果是0 返回0 如果是正數(shù),返回1.
????????????10买羞、power() ?冪運(yùn)算????????select power(5,3);===125
日期函數(shù)
Year袁勺、month、week哩都、curdate魁兼、curtime、date_format? str_to_date漠嵌、now
SELECT DATE_FORMAT(CURDATE(),'%Y年%m月%d日')
表與表之間的關(guān)聯(lián)關(guān)系:?在一對(duì)一的關(guān)聯(lián)關(guān)系中咐汞,外鍵加在那個(gè)表中都行。? ? ? ? 1:n ? n:1 ? ?n:n