DDL:資料庫模式定義語言DDL(Data Definition Language),是用于描述資料庫中要存儲的現實世界實體的語言。
DML:資料操縱語言DML(Data Manipulation Language),使用者通過它可以實作對資料庫的基本操作。
DCL:資料控制語言 (Data Control Language) 在SQL語言中,是一種可對資料通路權進行控制的指令,它可以控制特定使用者賬戶對資料表、檢視表、存儲程式、使用者自定義函數等資料庫對象的控制權。由 GRANT 和 REVOKE 兩個指令組成。
DQL:Data QueryLanguage 資料查詢語言。
DQL:資料庫查詢語言。關鍵字:SELECT … FROM … WHERE。
DDL :資料庫模式定義語言(資料庫、資料表)。關鍵字:CREATE,DROP,ALTER。
DML:資料操縱語言(資料)。關鍵字:INSERT、UPDATE、DELETE。
DCL:資料控制語言(權限) 。關鍵字:GRANT、REVOKE。
TCL:事務控制語言。關鍵字:COMMIT、ROLLBACK、SAVEPOINT。
DDL,DML,DCL,DQL,TCL共同組成資料庫的完整語言。
登入資料庫伺服器:
mysql -u賬号 -p密碼 //-u賬号之間無空格,密碼同樣
資料庫操作:
建立資料庫:
create database 資料庫名字;
建立資料庫的時候,指定字元集:
create database 資料庫名字 character set 字元集;
例: create database 資料庫名字 character set utf8 ;
create database 資料庫名字 character set 字元集 collate 校對規則;
例: create database 資料庫名字 character set 字元集 collate utf8_bin ;
檢視資料庫:
show databases ;
(information_schem , performance_schema ,mysql 三個庫不要動!)
檢視資料庫的定義:
show create database 庫名 ;
修改資料庫:
alter database 資料庫名字 character set 字元集 ;
删除資料庫:
drop database 資料庫名字 ;
切換資料庫:
use 資料庫名字 ;
檢視目前正在使用的資料庫:
select database() ;
表的操作:
列的類型: int, char, varchar, float, double, boolean, date …
列的限制:
主鍵限制:primary key
唯一限制:unique
非空限制:not null
建立表:
create table 表名(列名1 列的類型 限制,列名2 列的類型 限制) ;
例:
creat table table1(
sid int primary key,
sname varchar(16),
sex int,
age int,
class int,
score float
) ; //注意标點
檢視表:
show tables ;
檢視表的定義:
show create table 表名 ;
檢視表結構:
desc 表名 ;
例: desc student ;
修改表:
添加列(add):
alter table 表名 add 列名 列的類型 列的限制 ;
例: alter table student add score int not null ;
修改列(modify):
alter table 表名 modify 列名 列的類型 ;
例: alter table student modify sex varchar(2);
修改列名(change):
alter table 表名 change 舊列名 新列名 列的限制 ;
例:alter table student sex gender varcher(3) ;
删除列(drop):
alter table 表名 drop score ;
修改表名(rename):
rename tabel 舊表名 to 新表名 ;
例: rename table student to stu ;
修改表字元集:
alter table 表名 character set 字元集;
alter table stu character set gbk ;
删除表:
drop table 表名 ;
例: drop table stu ;
表中資料的操作:
表中插入資料:
insert into 表名(列名1,列名2,列名3) values(值1,值2,值3) ;
例:insert into student(sid,name,sex,age) values(1,’james’, 1,23) ;
簡單寫法:
insert into student values(2,’lucy’,2 ,25)
選擇插入:
insert into student(sid,sname) values(3,’Lebron’,1,25) ;
批量插入:
insert into student values(4,’zhangsan’,1,25),(5,’lisi’,2,24),(6,’wangwu’,2,30) ;
删除資料:
delete from 表名 [where 條件]
例:delete from student where sid=1 ;
delete from student ; //全删
修改(更新 )表資料:
update 表名 set 列名=列的值,列名2=列的值2 [where 條件]
例:update student set sname=’張三’ where sid=2 ;
update student set sname=’李四’,sex=3; //全改
查詢表中資料:
select [distinct] [*] [列名1,列名2] from 表名 [where 條件] //distinct:去除重複的資料
select * from 表名 ;
例:select * from student ; //檢視表中所有内容
select sname,sex from student ; //選擇檢視
别名查詢,as關鍵字,as關鍵字可以省略
表别名:select s.sname, s.sex from student as s; //主要用在多表查詢
列别名:select 原列名1 as 新别名,原列名2 as 新别名 from product ;
例:select sname as 姓名,sex as 性别 from student ; //就是用”姓名”、”性别”,在表中替換掉sname、sex 。(as可省略)
去掉重複的值查詢:
select age from student; //查詢學生的所有年齡
select distinct age from student; //去重
運算查詢:
select , age+1 as 年齡加一 from student ; //在查詢結果做±/運算,作為新的一列展示
select * from student where age>18 ; //查詢大于18歲的人
select * from student where age <> 22 ; //查詢不等于22的人(!=也可)
select * from student where age > 18 and age <25 ; //查詢年齡介于18-25的人
select * from student where age between 18 and 25 ; //同上
select * from student where age <18 or age >25; //查詢年齡小于18或者大于25的人
like:模糊查詢
_:代表的是一個字元
%:代表的是多個字元
select * from student where sname like ‘%張%’ ; //查詢所有名字帶有”張”的人
select * from product where sname like ‘_兆%’; //查詢所有名字中第二個字是”兆”的人
in: 在某個範圍中查詢
select * from student where class in (1,2,4); //查詢在1,2,4班的學生
排序查詢: (order by 關鍵字)
asc : ascend 升序(預設)
desc: descend 降序
例: select * from student order by sid desc ;
聚合函數:(where 條件後不能接聚合函數)
sum():求和
avg():求平均值
count():統計數量
max():最大值
min():最小值
select sum(score) from student; //擷取學生成績總和
select avg(score) from student; //擷取學生成績平均分
select count(*) from student; //擷取所有學生人數
select * from student where score > (select avg(score) from student); //查詢成績大于平均分的人
分組:group by
having關鍵字:可以接聚合函數,出現在分組之後
where關鍵字:不可以接聚合函數,出現在分組之前
根據班級分組,分組後統計學生的個數
select class,count(*) from student group by class;
根據class分組,分别統計每組學生的平均分,并且平均分 >60
select class,avg(score) from student group by class having avg(score) >60 ;
編寫順序:
select…from…where…group by…having…order by
執行順序:
from…where…group by…having…select…order by