第68节:Java中的MYSQL运用从小白到大牛
第68節(jié):Java中的MYSQL運用從小白到大牛
前言
學(xué)習(xí)java必備要求,學(xué)會運用!!!
常見關(guān)系化數(shù)據(jù)庫
BootStrap是輕量級開發(fā)響應(yīng)式頁面的框架,全局css組件,js插件.柵格系統(tǒng)是將頁面的寬度分為12等分.
bootstrap教程,SQL
col-xs-*: 手機(jī)屏幕 col-sm-*: 平板 col-md-*: PC電腦 col-lg-*: 超大屏幕數(shù)據(jù)庫crud操作
數(shù)據(jù)庫,什么是數(shù)據(jù)庫,數(shù)據(jù)的作用有哪些呢?
數(shù)據(jù)庫就是一個文件系統(tǒng),如文件夾目錄一樣,存儲數(shù)據(jù),很久很久以前是用sql命令來操作.
數(shù)據(jù)庫介紹,sql數(shù)據(jù)庫是structured query language 結(jié)構(gòu)化查詢語言的縮寫,sql是為數(shù)據(jù)庫而建立的操作命令集,是一種功能齊全的數(shù)據(jù)庫語言.我們在使用它的時候,只要發(fā)出命令,就可以展示數(shù)據(jù)了,sql功能強(qiáng)大啊,但是也是很容易進(jìn)行學(xué)習(xí),使用起來也很方便呢,sql已經(jīng)成為了數(shù)據(jù)庫操作的基礎(chǔ),并且所有的數(shù)據(jù)庫基本上都支持sql.
sql數(shù)據(jù)庫 是結(jié)構(gòu)化查詢語言, 簡稱為sql數(shù)據(jù)庫, 英文名: structured query language, 功能齊全的數(shù)據(jù)庫語言.
sql數(shù)據(jù)庫的優(yōu)點:
它是非過程化語言,是統(tǒng)一的語言,是所有關(guān)系數(shù)據(jù)庫的共同語言.
一個sql數(shù)據(jù)庫是表的集合,它是由一個或多個sql模式定義的,sql數(shù)據(jù)表由行集構(gòu)成,一行是列的序列集合,每行與行對應(yīng)的一個數(shù)據(jù)項,一個表或者一個基本表或者是視圖,.基本表是實際尺寸在數(shù)據(jù)庫的表,而視圖是由若干基本表或其他視圖構(gòu)成的表的定義.一個基本表可以拷一個或多個存儲文件,一個存儲文件也可以存放一個或多個基本表,每個存儲文件與外部存儲上的一個物理文件對應(yīng).
數(shù)據(jù)庫是按照數(shù)據(jù)結(jié)構(gòu)來組織,存儲和管理數(shù)據(jù)的倉庫,它產(chǎn)生于距今六十多年前.數(shù)據(jù)庫的基本結(jié)構(gòu)分三個層次,英文名database數(shù)據(jù)庫,有三種數(shù)據(jù)種類.
數(shù)據(jù)庫按照數(shù)據(jù)結(jié)構(gòu)來組織,存儲和管理數(shù)據(jù)的建立在計算機(jī)存儲設(shè)備上的倉庫.數(shù)據(jù)庫是可以在計算機(jī)內(nèi)進(jìn)行長期存儲的,有組織,可共享的數(shù)據(jù)集合.
作用:用來存儲數(shù)據(jù),具有訪問權(quán)限可以限制不同人的不同操作.
java開發(fā)工程師
2年以上的經(jīng)驗,基礎(chǔ)號
理解io,多線程,集合等基礎(chǔ)框架
對jvm原理的了解.
mvc,spring,mybatis,Webservice等
主流開發(fā)框架和技術(shù),各種算法,數(shù)據(jù)庫查詢語言
熟悉mysql,oracle等數(shù)據(jù)庫,有一定的數(shù)據(jù)庫設(shè)計經(jīng)驗,了解Linux,Unix等.
html,css,javascript,jquery,
eclipse,myeclipse等
瀏覽器: html/css/js用于顯示頁面
服務(wù)器: javaee后臺程序
數(shù)據(jù)庫
常見的數(shù)據(jù)庫:
mysql:
是開源免費的, 適用于中小型企業(yè)的數(shù)據(jù)庫.
sun收購mysql, sun公司被oracle收購了.
然后就開始收費了.
mariadb: 是mysql開源版的一個分支.
oracle: 甲骨文公司.
收費軟件,適用于大型電商網(wǎng)站.
sun公司擁有了java.google收購了安卓.
就導(dǎo)致了 sun->google
db2: IBM公司,解決方案:軟件和硬件.銀行系統(tǒng)大多采用db2.
sqlserver: 用于windows里面.大學(xué)教學(xué)采用sqlserver,圖形化工具.
非關(guān)系型數(shù)據(jù)庫: key : value
mongodb:
redis:
關(guān)系型數(shù)據(jù)庫:
用來描述實體與實體之間的關(guān)系.
E-R關(guān)系圖
E-R代表:
實體:方框,屬性:橢圓,關(guān)系:菱形.
mysql數(shù)據(jù)庫服務(wù)器
mysql server
進(jìn)行刪除mysql
MYSQL的安裝
有“Typical(默認(rèn))”、“Complete(完全)”、“Custom(用戶自定義)”三個選項,選擇“Custom”,按“next”鍵繼續(xù)
MySQL Server, Client Programs, Documentation同理,點擊.
“Detailed Configuration(手動精確配置)”、“Standard Configuration(標(biāo)準(zhǔn)配置)”
“Developer Machine(開發(fā)測試類,mysql占用很少資源)”、“Server Machine(服務(wù)器類型,mysql占用較多資源)”、“Dedicated MySQL Server Machine(專門的數(shù)據(jù)庫服務(wù)器,mysql占用所有可用資源)”
“Decision Support(DSS)/OLAP(20個左右)”、“Online Transaction Processing(OLTP)(500個左右)”、“Manual Setting(手動設(shè)置,自己輸一個數(shù))”
一般選UTF-8
mysql語句
DDL數(shù)據(jù)定義語言,用于定義數(shù)據(jù)庫,
create創(chuàng)建 drop刪除 alter修改DML: 數(shù)據(jù)操作語言,用于操作數(shù)據(jù)
insert 插入 update 修改 delete 刪除DCL: 數(shù)據(jù)控制語言,定義訪問權(quán)限,取消訪問權(quán)限,安全設(shè)置.
grantDQL: 數(shù)據(jù)查詢語言:
select 查詢 from where create database 創(chuàng)建數(shù)據(jù)庫 create database dashucoding;create database dashucoding character set字符集 collate校對規(guī)則 create database create database dashucoding character set utf8;數(shù)據(jù)庫的創(chuàng)建
登錄數(shù)據(jù)庫
mysql -uroot -padmin
創(chuàng)建數(shù)據(jù)庫
create database stu;
創(chuàng)建數(shù)據(jù)庫同時指定字符集
create database 數(shù)據(jù)庫的名字 character set 字符集
create database stu character set utf8
字符集是一套符號和編碼,校對規(guī)則 是在字符集內(nèi)用于比較字符的一套規(guī)則.
create database stu character set utf collate utf8_bin;
展示數(shù)據(jù)庫
show databases;
查看數(shù)據(jù)庫定義時的語句
show create database 數(shù)據(jù)庫的名字
show create database stu;
修改數(shù)據(jù)庫
修改數(shù)據(jù)的字符集
alter database 數(shù)據(jù)庫的名字 character set 字符集
alter database stu character set gbk;
刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫的名字
drop database stu;
切換數(shù)據(jù)庫
use 數(shù)據(jù)庫名字
use student;
查看當(dāng)前使用的數(shù)據(jù)庫
select database();
創(chuàng)建表
create table 表名(列名 列的類型 約束,列名2 列的類型 約束);create table student ( sid int primary key, sname varchar(31), sex int, age int);
列的約束:
主鍵的約束: primary key,
唯一的約束: unique,
非空約素: not null,
列的類型:
char: 固定長度
varchar: 可變長度
date:
date : YYYY-MM-DD time : hh:mm:ss datetime : YYYY-MM-DD hh:mm:ss 默認(rèn)值為null timestamp : YYYY-MMM-DD hh:mm:ss 默認(rèn)使用當(dāng)前時間text:用了存放文本
blob存放二進(jìn)制
查看表
show tables
查看表的定義
show create table student;
查看表的結(jié)構(gòu)
desc student;
修改表
add添加列
modify修改列
change修改列名
drop刪除列
renamme修改表名
添加列
alter table 表名 add 列名 列的類型 列的約束
alter table student add chengji int not null;
修改列
alter table student modify sex varchar(2);
desc student;
修改列名
alter table student change sex gender varchar(2);
刪除列
alter table student drop chengji;
修改表名
rename table student to dashucoding;
修改表的字符集
show create table student;
alter table student character set gbk;
刪除表
drop table student;
數(shù)據(jù)庫:
sql的分類:
DDL數(shù)據(jù)庫的定義,與表的結(jié)構(gòu):
create, drop, alter
DML數(shù)據(jù)操縱語言
insert update delete
DCL數(shù)據(jù)控制語言
設(shè)置用戶的訪問權(quán)限
DQL數(shù)據(jù)查詢語言
select form where
數(shù)據(jù)庫
創(chuàng)建數(shù)據(jù)庫
create database 數(shù)據(jù)庫的名字
查看數(shù)據(jù)庫
show databases;查看所有的數(shù)據(jù)庫
查看數(shù)據(jù)庫定義
show create database 數(shù)據(jù)庫名字
select database();查看當(dāng)前使用的數(shù)據(jù)庫
修改數(shù)據(jù)庫
alter database character set 字符集
刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名字
選中數(shù)據(jù)庫
use數(shù)據(jù)庫的名字
表數(shù)據(jù)的操作
創(chuàng)建表create table 表名(列名 列的類型 約素);
列的約素:
主鍵的約素:primary key
唯一約素: unique
非空約素: not null
刪除表:drop table 表名
查看表:show tables
查看當(dāng)前數(shù)據(jù)庫中所有的表名
show create table 表名
查看表的定義結(jié)構(gòu)
desc 表名;查看表的結(jié)構(gòu)
添加列
alter table 表名 add 列名 列的類型 列的約束
修改列
alter table 表名 modify 列名 列的類型 列的約束
修改列名
alter table 表名 change 舊列名 新列名 列的類型 列的約束
刪除列
alter table 表名 drop 列名
表的字符集
alter table 表名 character set 字符集
修改表名
rename table 舊表名 to 新的表名
表中數(shù)據(jù)的插入
sql完成對表中數(shù)據(jù)的操作
插入數(shù)據(jù):
insert into 表名 (列名1, 列名2, 列名3) values (值1, 值2, 值3);
insert into studnet(sid,sname,sex,age) values (1, 'dashu', 1, 23);
簡單寫法
insert into student values(2,'dashu', 1, 23);
查看表中的數(shù)據(jù)
select * from student;
批量插入
insert into student values (3,'da',1,32),(4,'coding',1,32);
解決亂碼的問題:
修改my.ini配置,先暫停服務(wù),57行修改gbk,保存重啟即可.
刪除的操作:
delete from 表名 [where條件];
delete from student where sid=6;
delete from student;會刪除所有的數(shù)據(jù);
delete刪除數(shù)據(jù)和truncate刪除數(shù)據(jù)有什么區(qū)別?
delete: 屬于DML 一條一條的刪除表中的數(shù)據(jù).
truncate: 屬于DDL 先刪除表再重建表.
如何刪除的數(shù)據(jù)比較少,delete比較高效,刪除的數(shù)據(jù)多,就truncate比較高效.
查詢
select [distinct] [*] [列名,列名2] from 表名 [where 條件];
更新表:
update 表名 set 列名=列的值, 列名2=列的值2 [where 條件];
update student set sname='dahsu' where sid=5
表數(shù)據(jù)的操作
insert into 表名 values(值1,值2,值3)
刪除記錄
delete from 表名 [where 條件]
更新
update 表名 set 列名=列到的值 [where 條件];
查看數(shù)據(jù)
select * from student;
select查詢
數(shù)據(jù)查詢的操作
select [distinct 可選參數(shù),去除重復(fù)的數(shù)據(jù)] [*] [列名,列名2] from 表名 [where 條件];
distinct: 去除重復(fù)的數(shù)據(jù).
create table category(cid int primary key auto_increment, cname varchar(10), cdesc varchar(31) );
插入數(shù)據(jù)
insert into category values (null, 'dashu', 'dashucoding');
查詢數(shù)據(jù)
select * from category;
select * from product;
查詢商品名稱和商品價格:
select pname,price form product;
別名查詢,關(guān)鍵字為as,可以省略
別名:
select p.pname, p.price form product p;
select p.pname, p.price from product as p;
列別名:
select pname as 商品名稱, price as 商品價格 from product;
select pname 商品名稱, price 商品價格 from product;
去除重復(fù)內(nèi)容:
select price from product;
select distinct price from product;
select *,price*1.5 as 折后價 from product;
select * from product where price > 100;
like: 模糊查詢:
_: 代表一個字符 %: 代表多個字符in查詢 在某個范圍內(nèi)的值
select * from product where con in(1,2,3);
排序查詢:order by關(guān)鍵字
asc: ascend升序
desc: descend降序
按照價格排序:
select * from product order by price;
select * from product order by price desc降序
select * from product where pname like '%dashu%';
select * from product where pname like '%dashu%' order by price asc;
聚合函數(shù)
// 一些函數(shù) sum(): 求和 avg(): 求平均值 count(): 統(tǒng)計數(shù)量 max(): 最大值 min(): 最小值select sum(price) from product;
select avg(price) from product;
select count(*) from product;
select avg(price) from product;
select * from product where price > (select avg(prive) from product);
分組:
group by
select con, count(*) from product group by cno;
select cno, avg(price) from product group by con;
having關(guān)鍵字
select cno, avg(price) from product group by con having avg(price) > 60;
select ... from ... where .. group by .. having .. order by ..from.. where.. group by ... having.. select .. order by ...復(fù)習(xí)
SQL分類: DDL: 數(shù)據(jù)庫的定義: create, drop, alter DML: 數(shù)據(jù)操縱語言: insert, update, delete DCL: 數(shù)據(jù)控制語言: 設(shè)置用戶的訪問權(quán)限 DQL: 數(shù)據(jù)查詢語言: select, from, where 表中數(shù)據(jù)操作: CRUD 插入數(shù)據(jù): insert into 表名 (列名1,列名2) values(值1,值2); insert into 表名 values(值1,值2); 批量插入: insert into 表名 values (值1,值2),(值1,值2); 刪除數(shù)據(jù): delete from 表名 where 條件 truncate table 表名: 先刪表,再重建 update 表名 set 列名=值, 列名=值 where 條件 select [distinct] [*] [ 列名1,列名2 ] from 表名 where 條件 group by ... having 條件過濾 order by 排序 asc: ascend desc: descend order by: order by price asc group byand or not 關(guān)系運算 <> 不等于 int 范圍 like 查詢 sum(); avg(); min(); max(); count();MySQL數(shù)據(jù)庫的安裝和配置
配置Mysql按照bin目錄到Path中
命令:
mysql –u用戶名 –p密碼一般會在數(shù)據(jù)庫創(chuàng)建多個表,以保存程序中實體的數(shù)據(jù).Structured Query Language, 結(jié)構(gòu)化查詢語言,SQL是用來存取關(guān)系數(shù)據(jù)庫的語言.
功能:
查詢、操縱、定義和控制關(guān)系型數(shù)據(jù)庫
DDL (數(shù)據(jù)定義語句)-Data Definition Language
DML (數(shù)據(jù)操縱語句)-Data Manipulation Language
DCL (數(shù)據(jù)控制語句)-Data Control Language
DQL (數(shù)據(jù)查詢語句)-Data Query Language
約束:
實體關(guān)系:
多對多: n:m 一對多: 1:n 一對一: 1:1重置Root密碼:
// 案例: 創(chuàng)建數(shù)據(jù)庫 create database stu; 使用utf8字符集 create database stu character set utf8; 帶規(guī)則 create database stu character set utf8 collate utf8_general_ci;修改字符集 alter database stu character set gbk; 刪除數(shù)據(jù)庫 drop database stu; 查看所有數(shù)據(jù)庫 show databases; 查看數(shù)據(jù)庫的字符集 show create database stu;操作員工表:create table employee (id int primary key auto_increment,name varchar(20) not null,gender varchar(10),birthday date,entry_date date,job varchar(30),salary double,resume text);刪除表 drop table student; 添加 alter table student add name varchar(20); 修改 alter table student modify name varchar(30); 刪除列 alter table student drop gender; 修改表名 rename table student to student_s; 修改表的 字符集 alter table student character set utf8; 修改列名name -> username alter table student change name username varchar(20) not null;查看表結(jié)構(gòu): show tables; show create table student; desc student; update student set salary = 2000; update student set salary = 3000 where name = 'dashu'; update student set salary = 2000, job = 'java' where name= 'dashu'; update student set salary = salary + 1000 where name='dashu';delete from student where name='da'; 刪除表中所有記錄 delete from student;查詢表中所有信息 select * from student; select name, sex from student;select *,chinese+math+english from exam; 使用別名 select *,chinese+math+english as 總分 from exam;select * from student where name='dashu'; select * from exam where chinese+math+english>200; in: select * from exam where math in(89,75,91);使用order by select *,chinese+math+english as 總分 from exam order by 總分 desc;select count(id) from exam;統(tǒng)計總分大于250的人數(shù)有多少? select count(id) from exam where chinese+math+english>250;統(tǒng)計數(shù)學(xué)成績大于或等于90的學(xué)生有多少個? select count(math) from exam where math>=90;使用avg函數(shù) select avg(math) from exam;查詢購買了幾類商品,并且每類總價大于100的商品 select product,sum(price) from orders where price>100 group by product;自己下載軟件:
mysql-5.5.27-winx64,查百度.
如果看了覺得不錯
點贊!轉(zhuǎn)發(fā)!
達(dá)叔小生:往后余生,唯獨有你
You and me, we are family !
90后帥氣小伙,良好的開發(fā)習(xí)慣;獨立思考的能力;主動并且善于溝通
簡書博客: 達(dá)叔小生
https://www.jianshu.com/u/c785ece603d1
結(jié)語
- 下面我將繼續(xù)對 其他知識 深入講解 ,有興趣可以繼續(xù)關(guān)注
- 小禮物走一走 or 點贊
轉(zhuǎn)載于:https://www.cnblogs.com/dashucoding/p/11140353.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的第68节:Java中的MYSQL运用从小白到大牛的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql-in关键字,分组查询,分页查
- 下一篇: 8、Python MySQL - mys