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
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/224344.html
標籤:其他
