數(shù)據(jù)庫
學(xué)習(xí)數(shù)據(jù)庫就是學(xué)習(xí)如何和數(shù)據(jù)庫軟件進(jìn)行交流,SQL語言就是用于程序員和數(shù)據(jù)庫軟件進(jìn)行交流的語言. DBMS:DataBaseManagementSystem 數(shù)據(jù)庫管理系統(tǒng)(數(shù)據(jù)庫軟件),包括:MySQL/Oracle/SQLServer,DB2,SQLite等 常見DBMS介紹:
MySQL:開源 Oracle公司產(chǎn)品,08年MySQL被Sun公司收購,09年Sun公司被Oracle, 原MySQL創(chuàng)始人離開Oracle創(chuàng)建新的數(shù)據(jù)庫MariaDB 市場占有率第一 Oracle:閉源 Oracle公司產(chǎn)品, 性能最高價(jià)格最貴的數(shù)據(jù)庫. 市占率第二 SQLServer:閉源 微軟公司產(chǎn)品,應(yīng)用在微軟的整套解決方案中 市占率第三 DB2:閉源 IBM公司產(chǎn)品,應(yīng)用在IBM整套解決方案中. SQLite:輕量級(jí)數(shù)據(jù)庫,只提供基礎(chǔ)的增刪改成操作.安裝包幾十k,主要應(yīng)用在移動(dòng)設(shè)備和嵌入式設(shè)備中.
開源:開發(fā)源代碼 免費(fèi), 盈利方式:通過賣服務(wù) , 會(huì)有程序員無償?shù)奶峁┥?jí)和維護(hù) 閉源:不開放源代碼 盈利方式:通過賣產(chǎn)品+賣服務(wù), 會(huì)有技術(shù)大拿攻擊,但是沒關(guān)系閉源產(chǎn)品的公司會(huì)養(yǎng)著一群人負(fù)責(zé)維護(hù)和升級(jí).
SQL語言
Structured Query Language:結(jié)構(gòu)化查詢語言, 用于程序員和數(shù)據(jù)庫軟件(DBMS)進(jìn)行交流
如何連接數(shù)據(jù)庫軟件: 檢查mysql服務(wù)開啟 window鍵+r 輸入services.msc
開始菜單中找到Mysql/MariaDB文件夾 里面的 Mysql Client 打開后直接輸入密碼
如果是linux或mac系統(tǒng) 先打開終端 在終端中輸入 mysql -u用戶名 -p回車 回車后輸入密碼 回車
斷開連接: 關(guān)閉窗口 或執(zhí)行 exit;
Access denied for user 'root'@'localhost' (using password: YES) 密碼錯(cuò)誤
數(shù)據(jù)庫相關(guān)SQL語句
往數(shù)據(jù)庫軟件中保存數(shù)據(jù),需要先建庫再建表,最后再操作表里面的數(shù)據(jù)
查詢所有數(shù)據(jù)庫
創(chuàng)建數(shù)據(jù)庫
格式: create database 數(shù)據(jù)庫名; 使用默認(rèn)字符集創(chuàng)建數(shù)據(jù)
create database db1;
指定字符集格式: create database 數(shù)據(jù)庫名 character set utf8/gbk;
create database db2 character set utf8;
create database db3 character set gbk;
查看數(shù)據(jù)庫詳情
格式: show create database 數(shù)據(jù)庫名;
show create database db1;
刪除數(shù)據(jù)庫
格式: drop database 數(shù)據(jù)庫名;
drop database db4;
使用數(shù)據(jù)庫
對(duì)表和數(shù)據(jù)進(jìn)行操作時(shí)必須先使用了數(shù)據(jù)庫才可以 不然會(huì)報(bào)錯(cuò) 格式: use 數(shù)據(jù)庫名;
use db1;
數(shù)據(jù)庫相關(guān)練習(xí):
分別創(chuàng)建mydb1和mydb2 第一個(gè)字符集utf8 第二個(gè)gbk
create database mydb1 character set utf8;
create database mydb2 character set gbk;
查詢所有數(shù)據(jù)庫檢查是否創(chuàng)建成功
show databases ;
分別查詢兩個(gè)數(shù)據(jù)庫的字符集是否成功
show create database mydb1;
show create database mydb2;
先使用mydb1 再使用mydb2
use mydb1;
use mydb2;
刪除兩個(gè)數(shù)據(jù)庫
drop database mydb1;
drop database mydb2;
表相關(guān)的SQL
操作表時(shí)一定保證已經(jīng)使用了某個(gè)數(shù)據(jù)庫 不然會(huì)報(bào)以下錯(cuò): ERROR 1046 (3D000): No database selected
創(chuàng)建表
格式: create table 表名(字段名 字段類型,字段名 字段類型);
create table student( name varchar ( 10 ) , age int ) ;
指定字符集格式: create table 表名(字段名 類型,字段名 類型) charset=utf8/gbk;
create table person( name varchar ( 10 ) , gender varchar ( 5 ) ) charset = gbk;
查詢所有表
查詢表詳情
格式: show create table 表名;
show create table person;
查看表字段
desc student;
刪除表
drop table student;
修改表名
格式: rename table 原名 to 新名;
rename table person to t_person;
添加表字段
最后添加格式: alter table 表名 add 字段名 類型; 最前面添加:alter table 表名 add 字段名 類型 first; 在某個(gè)字段后面添加 alter table 表名 add 字段名 類型 after xxx;
alter table t_person add salary int ;
alter table t_person add id int first ;
alter table t_person add age int after name;
刪除表字段
格式: alter table 表名 drop 字段名;
alter table t_person drop salary;
修改表字段
格式: alter table 表名 change 原名 新名 新類型;
alter table t_person change age salary int ;
表相關(guān)SQL語句回顧
創(chuàng)建 create table t1(name varchar(10),age int)charset=utf8;
查詢所有 show tables;
查詢表詳情 show create table t1;
查詢表字段 desc t1
刪除表 drop table t1;
修改表名 rename table t1 to t2;
添加表字段 alter table t1 add salary int first/ after xxx;
刪除表字段 alter table t1 drop salary;
修改表字段 alter table t1 change
原名 新名 新類型;
表相關(guān)練習(xí)題:
創(chuàng)建數(shù)據(jù)庫mydb1 字符集utf8 并使用該數(shù)據(jù)庫
create database mydb1 character set utf8;
use mydb1;
在mydb1中創(chuàng)建員工表emp 字段有name 表字符集也是utf8
create table emp( name varchar ( 10 ) ) charset = utf8;
alter table emp add age int ;
alter table emp add id int first ;
alter table emp add gender varchar ( 5 ) after name;
alter table emp change gender sal int ;
alter table emp drop age;
rename table emp to t_emp;
drop table t_emp;
drop database mydb1;
###數(shù)據(jù)相關(guān)SQL
執(zhí)行數(shù)據(jù)相關(guān)的SQL 必須保證已經(jīng)使用了某個(gè)數(shù)據(jù)庫,并且存在數(shù)據(jù)所對(duì)應(yīng)的表格
create database mydb2 character set utf8;
use mydb2;
create table person( name varchar ( 10 ) , age int ) charset = utf8;
插入數(shù)據(jù)
全表插入格式(要求值的數(shù)量和順序必須和表字段一致): insert into 表名 values(值1,值2,值3);
insert into person values ( 'Tom' , 18 ) ;
指定字段插入格式(要求值的數(shù)量和順序必須和指定的一致): insert into 表名(字段名1,字段名2)values(值1,值2);
insert into person( name) values ( 'Jerry' ) ;
批量插入數(shù)據(jù)格式: 在values后面寫多組值即可f insert into person values('Lucy’,20),('Lily’,21);
insert into person( name) values ( 'zhangsan' ) , ( 'lisi' ) ;
insert into person values ( '劉德華' , 30 ) ;
如果執(zhí)行以上代碼出現(xiàn)錯(cuò)誤提示,提示里面包含16進(jìn)制的錯(cuò)誤信息 執(zhí)行以下SQL
set names gbk;
查詢數(shù)據(jù)
格式: select 字段信息 from 表名 where 條件; 舉例: 查詢person表中所有的名字 select name from person;
查詢person表中年齡大于20的名字和年齡 select name,age from person where age>20;
查詢person表中所有數(shù)據(jù)的所有字段信息 select * from person;
修改數(shù)據(jù)
格式: update 表名 set 字段名=xxx,字段名=xxx where 條件; 舉例:
update person set age= 8 where name= 'Tom' ;
update person set age= 10 where age is null ;
刪除數(shù)據(jù)
格式: delete from 表名 where 條件; 舉例:
刪除Tom
delete from person where name= 'Tom' ;
2. 刪除年齡小于20歲的
delete from person where age< 20 ;
3. 刪除所有數(shù)據(jù)
delete from person;
增刪改查回顧:
插入數(shù)據(jù) insert into 表名 values(值1,值2); 查詢數(shù)據(jù) select 字段信息 from 表名 where 條件; 修改數(shù)據(jù) update 表名 set 字段名=xxx where 條件; 刪除數(shù)據(jù) delete from 表名 where 條件;
數(shù)據(jù)類型
整數(shù)類型: int(m)和bigint(m) bigint等效java中的long, m代表顯示長度 需要結(jié)合zerofill關(guān)鍵字使用
create table t1( name varchar ( 10 ) , age int ( 10 ) zerofill) ;
insert into t1 values ( 'Tom' , 18 ) ;
select * from t1;
浮點(diǎn)數(shù): double(m,d) m代表總長度 d代表小數(shù)長度 58.234 m=5 d=3 ,超高精度的浮點(diǎn)數(shù)decimal(m,d)只有涉及超高精度運(yùn)算時(shí)使用. 字符串:
char(m): 固定長度 m=10 存"abc" 占10,執(zhí)行效率略高 最大255 varchar(m):可變長度 m=10 存"abc" 占3,更節(jié)省存儲(chǔ)空間, 最大65535 超過255建議使用text text(m):可變長度,最大值65535.
日期:
date: 只能保存年月日 time: 只能保存時(shí)分秒 datetime:保存年月日時(shí)分秒,默認(rèn)值是null,最大值9999-12-31 timestamp:時(shí)間戳(距離1970年毫秒數(shù)),保存年月日時(shí)分秒,默認(rèn)值當(dāng)前系統(tǒng)時(shí)間,最大值2038-1-19 舉例:
create table t_date( t1 date , t2 time , t3 datetime , t4 timestamp ) ;
insert into t_date values ( '2020-1-18' , null , null , null ) ;
insert into t_date values ( null , '17:35:18' , '2020-3-17 12:30:23' , null ) ;
Java、Python、算法知識(shí)體系 | PPT、簡歷模板 | 游戲源碼 | IT行業(yè)趣味資訊 | 裝機(jī)必備軟件
關(guān)注下方公號(hào)回復(fù)【資料】獲取 👇🏻👇🏻👇🏻