mysql 數(shù)據(jù)庫簡介

常用數(shù)據(jù)庫類型
網(wǎng)狀數(shù)據(jù)庫、層次數(shù)據(jù)庫、關(guān)系型數(shù)據(jù)庫、非關(guān)系型數(shù)據(jù)庫

常用的關(guān)系型數(shù)據(jù)庫產(chǎn)品 sql語句
sqlserver 中小型 收費(fèi)
Oracle、DB2 大型 收費(fèi)
mysql 中小型 6.0之后 付費(fèi)版本
sqlite

阿里、百度 服務(wù)端結(jié)構(gòu) LAMP=Linux apache mysql php/python

SQL語句:結(jié)構(gòu)化查詢語言,非過程性語言。
DDL定義 DML操縱 DCL控制 DQL查詢

電腦安裝了mysql就是一個(gè)mysql server,通過命令行或者mysql命令行

安裝成功后默認(rèn)有4個(gè)數(shù)據(jù)庫。儲(chǔ)存的是元數(shù)據(jù)、核心數(shù)據(jù)、額外數(shù)據(jù),前3個(gè),盡量不操作。

使用sql對數(shù)據(jù)庫進(jìn)行操作
創(chuàng)建數(shù)據(jù)庫 create database 數(shù)據(jù)庫名字;
顯示所有數(shù)據(jù)庫 show databases;
使用某個(gè)數(shù)據(jù)庫 use 數(shù)據(jù)庫的名字;
查看當(dāng)前使用的數(shù)據(jù)庫是哪一個(gè) select database();
刪除數(shù)據(jù)庫 drop database 數(shù)據(jù)庫名字;

使用sql對表進(jìn)行操作
創(chuàng)建一張表 create table 表名(字段名字 字段類型, 字段名字 字段類型); create table info(id int,name varchar(20));
查看表結(jié)構(gòu) desc 表名字; desc info;
查看當(dāng)前數(shù)據(jù)庫有哪些表 show tables;
刪除表 drop table 表名; drop table info;
修改表結(jié)構(gòu) 添加一列 alter table 表名 add 字段名字 字段類型; alter table info add phone varchar(20);

sql支持的數(shù)據(jù)類型
字符串型
VARCHAR 可變長度的字符串 varchar(20) 13888888888; 在申請的空間范圍內(nèi) 用多少分配多少 CHAR char(20);

大數(shù)據(jù)類型 BLOB TEXT 比較大的文本

數(shù)值型 TINYINT byte SMALLINT short INT int BIGINT long FLOAT float DOUBLE double

邏輯型 BIT boolean

日期型 DATE 日期 TIME 時(shí)間 DATETIME 日期和時(shí)間 TIMESTAMP 時(shí)間戳

使用sql對表中的記錄進(jìn)行操作 CRUD
1.插入記錄
insert into 表名 values(要添加的值…);
當(dāng)添加的字段的數(shù)據(jù)類型是int類型,直接寫值
如果添加的字段類型是varchar類型和日期類型,使用單引號(hào)把值包起來
insert into info values(1,'zhangsan','110');
insert into test (name) values('lisi');
插入記錄的時(shí)候可以在表名的后面加上(列名 ) 可以向指定的列中插入數(shù)據(jù)

2.修改記錄
update 表名 set 字段名1 = 值1 , 字段名2 = 值2 where 條件;
update info set phone='12345' where name='zhangsan';

3.刪除記錄
delete from 表名 where 條件 如果沒有where條件 刪除表中的所有數(shù)據(jù)
delete from info where id = 1;
delete from info; 刪除表中的所有數(shù)據(jù)

4.查詢記錄
select 字段名 (*) as別名 from 表名 where 條件 select * from info;
查詢所有字段的內(nèi)容 select name,phone from info where id = 1;

5.去除表中重復(fù)記錄(只會(huì)影響查詢的結(jié)果 不會(huì)修改表中的記錄)
select distinct * from 表名;
select distinct name from info; 只顯示名字 去除重復(fù)的記錄

6.MySQL的約束
a.非空約束 not null * 表示數(shù)據(jù)不能為空
b.唯一性約束 unique * 表中的記錄不能重復(fù)的
c.主鍵約束 primary key * 表示非空,唯一性
d 自動(dòng)增長 auto_increment
create table test(id int primary key auto_increment,name varchar(20));

where子句使用
a. 運(yùn)算符 <, >, >=, <=
b. in 在范圍內(nèi) select * from student where Android in (70,90); 注意in不是指定起始和結(jié)束的范圍 而是在()中的數(shù) 據(jù)中做選擇
如果想指定起始和結(jié)束的范圍要使用and 條件
c. and 條件同時(shí)滿足 select * from student where Android>=70 and Android<=90;
d. like 模糊查詢
% 表示占位符(通配符) %替換若干文字 select * from student where name like '%zha%'; //查詢表中名字里含有zha的記錄
排序
order by 字段名 asc 升序
order by 字段名 desc 降序
默認(rèn)升序的 如果想降序 需要加上desc select * from student order by English;

聚合函數(shù),
分組查詢&limit關(guān)鍵字
1.count()函數(shù)
select count(*) from ...where....
2.sum()函數(shù)
select sum(要進(jìn)行求和字段) from ...where....

  1. avg()函數(shù)
    select avg(要計(jì)算平均數(shù)的字段名稱) from …
  2. max()函數(shù)
    select max(字段) from...
  3. min()函數(shù)
    select min(字段) from... 分組查詢 group by
    分組查詢
    group by
    select name,sum(price) from orders group by name; 按照名字進(jìn)行分組 查詢每種商品的總價(jià)格
    分組查詢?nèi)绻由蠗l件不能使用where 而要使用having select name,sum(price) from orders group by name having sum(price)>5000;

limit
select * from orders limit 2; 從第一條開始顯示 顯示2條
select * from orders limit 2,3; 從第2+1條開始顯示 顯示3條記錄; 通過limit關(guān)鍵字 可以實(shí)現(xiàn)數(shù)據(jù)的分頁查詢(只查詢一部分?jǐn)?shù)據(jù))

JDBC(Java DataBase Connectivity) (代替命令行)
Java數(shù)據(jù)庫的連接 java定義的接口 解決通過Java操作關(guān)系型數(shù)據(jù)庫的問題 由于操作關(guān)系型數(shù)據(jù)庫 一定要通過sql
實(shí)際上 jdbc過程就是要獲取一個(gè)對象 這個(gè)對象可以執(zhí)行一個(gè)sql語句 DriverManager 加載驅(qū)動(dòng) 創(chuàng)建鏈接 Connection 代表了一個(gè)java客戶端跟mysql服務(wù)端的一個(gè)連接 Statement/prepareStatement 通過connection獲取的 可以執(zhí)行sql語句的對象 ResultSet 如果執(zhí)行的是查詢數(shù)據(jù)庫的操作 查詢返回的結(jié)果會(huì)封裝成一個(gè)resultset 結(jié)果集 通過操 作這個(gè)結(jié)果集就可以獲取到查詢的所有內(nèi)容

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容