1、數(shù)據(jù)庫類型
關系型數(shù)據(jù)庫
大型:Oracle、DB2
中型:SQL Server、MySQL
小型:Access、SQLite等
非關系型數(shù)據(jù)庫:Memcached、MongoDB、Redis
單詞
數(shù)據(jù)庫:database
行/記錄:row/record
列/字段:column/field
%表示匹配多個字符
_表示匹配單個字符
SQL分為三個部分:
DDL:(Data Definition Language,數(shù)據(jù)定義語言)用來維護存儲數(shù)據(jù)的結構(數(shù)據(jù)庫、表),
代表指令:增create、刪drop、改alter等
DML:(Data Manipulation Language,數(shù)據(jù)操作語言)用來對數(shù)據(jù)進行操作(數(shù)據(jù)表中的內容),
代表指令:插入insert、刪除delete、update等。其中DML內部又單獨進行了一個分類:DQL(Data Query Language,數(shù)據(jù)查詢語言)如select
DCL:(Data Control Language,數(shù)據(jù)控制語言)主要是負責權限管理(用戶),代表指令:分配權限grant、? ?revoke等
連接認證
mysql.exe -h localhost -P3306 -u root -p(簡寫)mysql -u root -p
斷開連接是為了:釋放資源,因為服務器有并發(fā)限制
退出命令
exit、quit、\q
MySQL服務器內部對象分成了四層
系統(tǒng)(DBMS)
數(shù)據(jù)庫(DB)
數(shù)據(jù)表(Table)
字段(Field)
將SQL的基本操作根據(jù)操作對象進行分類,可分為三類:
庫操作
表操作(包含字段操作)
數(shù)據(jù)操作
創(chuàng)建數(shù)據(jù)庫
create database 數(shù)據(jù)庫名字 [庫選項];
字符集設定:charset/character set 具體字符集(數(shù)據(jù)存儲的編碼格式,常用的有:GBK和UTF8)
舉例:create database mydatabase charset utf8;
創(chuàng)建關鍵字的數(shù)據(jù)庫 (關鍵字就是已經(jīng)被系統(tǒng)使用的字符或者保留字,將來系統(tǒng)可能會用到的字符)
create database ·database· charset utf8;
告訴服務器當前中文的字符集是什么(如果中文的不好使就加這句)?
set names gbk;
創(chuàng)建中文的數(shù)據(jù)庫
create database 中國 charset utf8;
查看所有數(shù)據(jù)庫:
show databases;
查看部分數(shù)據(jù)庫
show create database 數(shù)據(jù)庫名字;
%表示匹配多個字符
_表示匹配單個字符
查看以information_開始的數(shù)據(jù)庫(如果想匹配_下劃線就需要被轉義)
%表示匹配多個字符 , _表示匹配單個字符
例子:show databaseslike 'information\_%';
例子:show databaseslike 'information_%';? ?相當于information%
查看數(shù)據(jù)庫的創(chuàng)建語句
show create database 數(shù)據(jù)庫名字;
例子:showcreate database mydatabase;
例子:showcreate database ·database·;(有關鍵字的數(shù)據(jù)庫)
修改數(shù)據(jù)庫informationtest的字符集
alter database 數(shù)據(jù)庫名字 [庫選項];
例子:alter database informationtest charset GBK;
刪除數(shù)據(jù)庫
drop database 數(shù)據(jù)庫名字;
例子drop database informationtest;
創(chuàng)建表
(方案一 )
顯式地指定表所屬的數(shù)據(jù)庫
create table 數(shù)據(jù)庫名.表名();
例子:create table ifnot exists mydatabase.student(
?顯示地將student表放到mydatabase數(shù)據(jù)庫下
namevarchar(10),
gendervarchar(10),
numbervarchar(10),
ageint
)charset utf8;
(方案二)
隱式地指定表所屬數(shù)據(jù)庫,進入數(shù)據(jù)庫環(huán)境:use 數(shù)據(jù)庫名字;
-- 創(chuàng)建數(shù)據(jù)表
-- 進入數(shù)據(jù)庫
use mydatabase;
-- 創(chuàng)建表
create table class(
namevarchar(10),
roomvarchar(10)
)charset utf8;
查看所有表
show tables;
查看以s結尾的表
show tables like 'pattern';
例子:show tables like '%s';
查看表的創(chuàng)建語句
show create table 表名;
例子:showcreate table student;
showcreate table student\g--? -g等價于分號;
showcreate table student\G--? -G將查到的結構旋轉90度變成縱向
查看表結構(表中的字段信息)
desc/describe/show columns from 表名;
例子:desc class;
describe class;
show columns from class;
重命名(student表 -> my_student)
rename table 舊表名 to 新表名;
例子:renametable studentto my_student;
修改表選項
修改表選項(字符集、校對集、存儲引擎都可以修改)
alter table 表名 表選項 [=] 值;
例子:alter table my_student charset = GBK;
新增字段
alter table 表名 add [column] 字段名 數(shù)據(jù)類型 [列屬性] [位置];
舉例:給學生表增加ID,放在第一個位置
alter table my_student
add column idint
first;
修改字段:alter table 表名 modify 字段名 數(shù)據(jù)類型 [列屬性] [位置];
alter table 表名 modify 字段名 數(shù)據(jù)類型 [列屬性] [位置];
舉例:將學生表中的number學號字段變成固定長度,且放到第二位(id之后)(modify調整)
alter table my_student modify numberchar(10) after id;
重命名字段:alter table 表名 change 舊字段 新字段名 數(shù)據(jù)類型 [列屬性] [位置];
舉例:修改學生表中的gender字段為sex(change修改)
alter table my_student change gender sexvarchar(10);
刪除字段:alter table 表名 drop 字段名;
舉例:刪除學生表中的age年齡字段(drop刪除)
?alter table my_student drop age;
刪除數(shù)據(jù)表
drop table 表名1,表名2……;
例子:drop table class;
新增數(shù)據(jù)? ? 插入數(shù)據(jù)
-- 方案一
-- (按照字段順序)
nsert into 表名 values(值列表)[,(值列表)];
例子:insert into my_studentvalues
(1,'bc20190001','Jim','male'),
(2,'bc20190002','csal','women');
插入數(shù)據(jù)
-- 方案二
insert into 表名 (字段列表) values (值列表) [,(值列表)];
例子:insert into my_student (number,sex,name,id)values
('bc20090003','male','Tom',3),
('bc20090004','female','loca',4);
查看所有數(shù)據(jù)
select * from 表名 [where 條件];
例子:select *from my_student;
查看指定字段、指定條件的數(shù)據(jù)
select 字段列表 from 表名 [where 條件];
舉例:查看滿足id為1的學生信息
select id,number,sex,namefrom my_studentwhere id=1;
更新數(shù)據(jù)
update 表名 set 字段 = 值 [where 條件];
建議都有where,否則就是更新全部
例子:update my_studentset sex='female' where name='Tom';
刪除數(shù)據(jù)
刪除是不可逆的,謹慎刪除
delete from 表名 [where條件];
例子:delete from my_studentwhere sex='male';