什么是數(shù)據(jù)庫?
數(shù)據(jù)庫(Database):存儲(chǔ)數(shù)據(jù)的倉庫
高效地存儲(chǔ)和處理數(shù)據(jù)的介質(zhì)(介質(zhì)主要是兩種:磁盤和內(nèi)存)
數(shù)據(jù)庫的分類
基于存儲(chǔ)介質(zhì)的不同:分為關(guān)系型數(shù)據(jù)庫(SQL)和非關(guān)系型數(shù)據(jù)庫(NoSQL:Not Only SQL)
不同數(shù)據(jù)庫陣營的產(chǎn)品
關(guān)系型數(shù)據(jù)庫
大型:Oracle、DB2
中型:SQL Server、MySQL
小型:Access、SQLite等
非關(guān)系型數(shù)據(jù)庫:Memcached、MongoDB、Redis
兩種數(shù)據(jù)庫陣營的區(qū)別
關(guān)系型數(shù)據(jù)庫:安全(保存磁盤,基本不可能丟失),容易理解,比較浪費(fèi)空間(二維表)
非關(guān)系型數(shù)據(jù)庫:效率高,不安全(斷電丟失)
什么是關(guān)系型數(shù)據(jù)庫?
是一種建立在關(guān)系模型(數(shù)學(xué)模型)上的數(shù)據(jù)庫
關(guān)系模型:一種所謂建立在關(guān)系上的模型
關(guān)系模型包含三個(gè)方面:
數(shù)據(jù)結(jié)構(gòu):用于解決數(shù)據(jù)存儲(chǔ)的問題,二維表(有行和列)
操作指令集合:所有SQL語句
完整性約束:表內(nèi)數(shù)據(jù)約束(字段與字段)、表與表之間約束(外鍵)
從需要存儲(chǔ)的數(shù)據(jù)需求中分析,如果是一類數(shù)據(jù)(實(shí)體)應(yīng)該設(shè)計(jì)成一張二維表:表是由表頭和數(shù)據(jù)部分組成
表頭:即字段名,用來規(guī)定數(shù)據(jù)的名字
數(shù)據(jù)部分:實(shí)際存儲(chǔ)的數(shù)據(jù)單元
關(guān)系型數(shù)據(jù)庫,需要維護(hù)實(shí)體內(nèi)部、實(shí)體與實(shí)體之間的聯(lián)系
show databases; --查看所有數(shù)據(jù)庫
exit quit \q --退出命令
mysql.exe? -h? localhost -P 3306 -u root -p --進(jìn)入數(shù)據(jù)庫
--創(chuàng)建數(shù)據(jù)庫
create database mydatebase charset utf8;
--關(guān)鍵字?jǐn)?shù)據(jù)庫
create database mydatebase charset utf8;
--中文數(shù)據(jù)庫
create database 中國 mydatebase charset utf8;
--告訴服務(wù)器當(dāng)前中文字符集
set names gbk;
--創(chuàng)建數(shù)據(jù)庫
create database informationest charset utf8;
--查看數(shù)據(jù)庫
show databases;
--查看以information_開始的數(shù)據(jù)庫(_需要被轉(zhuǎn)義)
show databases like 'information\_%';
show databases like 'information\_%'; --相當(dāng)于information%
--查看數(shù)據(jù)庫的創(chuàng)建語句
show create database mydatebase;
show create database ‘database’;--查看帶有關(guān)鍵字?jǐn)?shù)據(jù)庫
--修改數(shù)據(jù)庫
alter database informationtest charset charset utf8;
--刪除數(shù)據(jù)庫
drop database + 數(shù)據(jù)庫名字;
--新增數(shù)據(jù)表
create table if not exists student(
name varchar(10),
gender varchar(10),
number varchar(10),
age int
)charset utf8;
--創(chuàng)建數(shù)據(jù)表
--進(jìn)入數(shù)據(jù)庫
use mydatebase;
--創(chuàng)建表
create table class()charset utf8;
--創(chuàng)建表
create table class(
name varchar(10),
room varchar(10)
)charset utf8;
--查看所有表
show tables;
--查看所有以s結(jié)尾的表、
show tables like'%s';盡量不要用效率低的,不知效率低
--查看表的創(chuàng)建語句
show create table student;
show create table student;\g
show create table student;\G--將查到的結(jié)構(gòu)縱向
--查看表結(jié)構(gòu)
desc class;
describe class;
show columns from class;
--重命名表(student表 --> tb1 my_student)
rename table student to my_student;
--修改表選項(xiàng): 字符集
alter table my_student charset = GBK;
--給學(xué)生表加ID,放到第一個(gè)位置
alter table my_student
add column id int
first;
--將學(xué)生表中的number學(xué)號(hào)字段變成固定長度,且放到第二位(ID之后)
alert table my_student modify number char(10) after id ;
--修改學(xué)生表中的gender字段為sex
alert table my_student change gender sex varchar(10);
--刪除學(xué)生表中的age年齡字段
alert table my_student drop age;
--刪除數(shù)據(jù)表
drop table class;
--插入數(shù)據(jù)
insert into my_student value
(1,'bc20190001','jim','male'),
(2,'bc20190002','lili','female');
--插入數(shù)據(jù),指定字段列表
insert into my_student(number,sex,name,id) value
(1,'bc20190003','male','Tom'),
(2,'bc20190002','female','Lucy');
--查看所有人數(shù)據(jù)
select * from my_student
--查看指定字段,指定條件的數(shù)據(jù)
--查看滿足ID為1的學(xué)生信息
select id,number,sex,name from my_student where id=1;
--更新數(shù)據(jù)
update my_student set sex = 'female' where name='Jim';
--刪除數(shù)據(jù)
delete from my_student where sex='male';