數(shù)據(jù)分析-SQL基礎(chǔ)

1 前言

數(shù)據(jù)庫(DB)是按照數(shù)據(jù)結(jié)構(gòu)存儲數(shù)據(jù)的倉庫,數(shù)據(jù)庫管理系統(tǒng)(DBMS)是操縱和管理數(shù)據(jù)庫的一套軟件,可分為關(guān)系型的數(shù)據(jù)庫管理系統(tǒng)和非關(guān)系型的數(shù)據(jù)庫管理系統(tǒng)。數(shù)據(jù)庫管理系統(tǒng)采用結(jié)構(gòu)化查詢語言(SQL)來管理數(shù)據(jù)庫。結(jié)構(gòu)化查詢語言按照功能分類,可分為數(shù)據(jù)定義語言(DDL)、數(shù)據(jù)操縱語言(DML)、數(shù)據(jù)查詢語言(DQL)、事務(wù)控制語言(TCL)、數(shù)據(jù)控制語言(DCL)。SQL語句不區(qū)分大小寫,語句最后的分號(;)代表運行結(jié)束。

image-20200827233705462

2 SQL語言

2.1 DDL(數(shù)據(jù)定義語言)

  • 創(chuàng)建數(shù)據(jù)庫、表(create)
create database if not exists testDB;--創(chuàng)建數(shù)據(jù)庫
--創(chuàng)建表
create table testtable(
    sid int(4) DEFAULT NULL COMMENT '學(xué)生ID',
  sex bit(1) DEFAULT NULL COMMENT '性別',
    name varchar(10) DEFAULT NULL COMMENT '姓名',
  birthday date DEFAULT NULL COMMENT '生日'
)ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='學(xué)生清單'
create table testtable1 select * from testtable -- 另一種創(chuàng)建表的形式
  • 修改表結(jié)構(gòu)(alter)
-- 添加字段
alter table testtable add email varchar(20) first;--first | after 決定字段的位置
-- 修改字段名稱
alter table testtable add email semail varchar(20) after sid;
--修改字段類型
alter table testtable add email varchar(50);
--刪除字段
alter table testtable drop email;
  • 表刪除(drop)
drop table testtable
-- 重命名
rename table testtable to test
-- 清空表
truncate table testtable</pre>

2.2 DML(數(shù)據(jù)操縱語言)

  • 添加數(shù)據(jù)(insert)
insert into testtable values(1,0,'張三','2020-05-21')
insert into testtable(sid,sex,name,birthday) values(1,0,'張三','2020-05-21')--也可單獨為某個字段賦值
insert into testtable(sid,sex,name,birthday) values(1,0,'張三','2020-05-21'),(2,0,'李四','2020-06-1');--批量插入
  • 更新數(shù)據(jù)(update)
update testtable set sex=1 where sid =1;
  • 刪除數(shù)據(jù)(delete)
delete from  testtable where sid =1;

2.3 DQL(數(shù)據(jù)查詢語言)

  • 查詢(select)
select * from testtable 
select sid,sex,name,birthday from testtable
  • 條件(where)
?select sid,sex,name,birthday from testtable where sid =1
  • 排序(order by)
select sid,sex,name,birthday from testtable order by sid asc --升序  , sid desc 降序
  • 去重(distinct)
select distinct name from testtable 
  • 分組(group by)
select sum(sid),sex from testtable GROUP BY sex  
  • 分組之后條件(having)
select sum(sid),sex from testtable GROUP BY sex having sex=1
  • 限制(limit)
select sid,sex,name,birthday from testtable limit 0,5 --前5

2.4 TCL (事務(wù)控制語言)

  • 存儲引擎
show engines -- 查看存儲引擎 默認為InnoDB
image-20200827222902777
  • 事務(wù)
    事務(wù)就是用戶定義的一個數(shù)據(jù)庫操作序列,這些操作要么全做要么全都不做,是一個不可分割的工作單位,一般是指要做的或所做的事情。我們也可以理解為事務(wù)是由一個或多個SQL語句組成,如果其中有任何一條語句不能完成或者產(chǎn)生錯誤,那么這個單元里所有的sql語句都要放棄執(zhí)行,所以只有事務(wù)中所有的語句都成功地執(zhí)行了,才可以說這個事務(wù)被成功地執(zhí)行!

Mysql默認一條SQL語句當做一個事務(wù),自動進行事務(wù)提交。如果想把多條SQL語句看做一個事務(wù),請參考如下代碼。

set Autocommit =0;

start TRANSACTION;

insert into testtable(sid,sex,name,birthday) values(1,0,'張三','2020-05-21');

insert into testtable(sid,sex,name,birthday) values(1,0,'張三','2020-05-21');

commit;
rollback;

2.5 DCL(數(shù)據(jù)控制語言)

  • 授權(quán)(grant)
grant 查找權(quán)限(select), 更新權(quán)限(update),刪除權(quán)限(delete),全部權(quán)限(ALL) on 數(shù)據(jù)庫名.表名 to ‘用戶名’@’允許其登錄的地址’ identified by ‘密碼’;

grant all on test.*  to user@'192.168.1.150' identified by "password";    

show grants -- 查看權(quán)限,下圖是root的權(quán)限。
image-20200827232128667
  • 刪除權(quán)限(revoke)
revoke 查找權(quán)限(select), 更新權(quán)限(update),刪除權(quán)限(delete),全部權(quán)限(ALL)on 數(shù)據(jù)庫名.表名 from ‘用戶名’@’允許其登錄的地址’;

revoke all on test.* from user@'192.168.1.150';

flush privileges;--刷新權(quán)限

不求點贊,只求有用

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

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