DML、DDl、DQL实战
表的創(chuàng)建與刪除
create table 學(xué)生成績(
id int primary key,
name char(10) not null,
sex char(10) not null,
物理 int not null,
語文 int not null,
英語 int not null,
數(shù)學(xué) int
)DEFAULT CHARSET=utf8mb4;
drop table 學(xué)生成績;
DML
數(shù)據(jù)操縱語言DML主要有三種形式:
M:manipulate 操縱
##增
insert 學(xué)生成績 Values(02,‘天李德’,‘男’,100,90,111,90);
insert 學(xué)生成績 Values(03,‘萬德華’,‘女’,90,90,122,80);
insert 學(xué)生成績 Values(04,‘德瑪西亞’,‘男’,90,80,133,90);
insert 學(xué)生成績 Values(05,‘劉德華’,‘男’,80,20,55,70);
insert 學(xué)生成績 Values(06,‘劉看山’,‘男’,70,30,66,90);
insert 學(xué)生成績 Values(07,‘劉天’, ‘女’,50,20,66,null);
insert 學(xué)生成績(id,name,‘男’,物理,語文,英語) Values(‘08’,‘劉天俊’,20,65,70);
##刪
delete from 學(xué)生成績 where id=8;
delete from 學(xué)生成績;
truncate table 學(xué)生成績; ##刪除表,再創(chuàng)建一個一模一樣的
##改
update 學(xué)生成績 set 語文=2 where id = 2;
update 學(xué)生成績 set 語文=40 ,物理=20 where id = 2;
update 學(xué)生成績 set 語文=2; ##不加條件則全部修改
DDL
數(shù)據(jù)定義語言DDL用來創(chuàng)建數(shù)據(jù)庫中的各種對象-----表、視圖、
索引、同義詞、聚簇等
D:define 定義
##添加一列
alter table student add sex char(2);
select * from student;
##刪除列
alter table student drop sex;
##修改表名
alter table student rename teacher;
alter table teacher rename student;
##修改表的字符集
alter table student character set utf8;
show create table student;
##修改名稱類型
alter table student change sex sex int not null;
alter table student modify sex char(20) null;
desc student;
DQL
數(shù)據(jù)查詢語言DQL基本結(jié)構(gòu)是由SELECT子句,FROM子句,WHERE
子句組成的查詢塊。
Q:query 查詢
##去重
select * from 學(xué)生成績;
select distinct 物理 from 學(xué)生成績;
##計算列
select 物理,語文,物理+語文 from 學(xué)生成績;
select 物理,數(shù)學(xué),物理+數(shù)學(xué) from 學(xué)生成績;
select name 物理,數(shù)學(xué),物理+ ifnull(數(shù)學(xué),0) from 學(xué)生成績;
##起別名
select name,物理,數(shù)學(xué),物理+ ifnull(數(shù)學(xué),0) as 總分 from 學(xué)生成績;
##模糊查詢
##_代表一個占位符,%代表隨意
select * from 學(xué)生成績 where name like ‘德%’;
select * from 學(xué)生成績 where name like ‘_德%’;
select * from 學(xué)生成績 where name like ‘__德%’;
1、排序
##默認(rèn)是asc(升序),記:d:decline(降)
##order by XXX desc
select * from 學(xué)生成績 order by 物理 desc ,語文 asc;
2、聚合函數(shù)(縱向計算)
##count
select count(數(shù)學(xué)) from 學(xué)生成績; ## 排除NULL
select count(ifnull(數(shù)學(xué),0)) as 數(shù)學(xué)人數(shù) from 學(xué)生成績; ## 如果為null值則為0
##max 最高分
select max(數(shù)學(xué)) from 學(xué)生成績;
##min 最低分
select min(數(shù)學(xué)) from 學(xué)生成績;
##sum 全部總和
select sum(數(shù)學(xué)) from 學(xué)生成績;
##avg 平均分
select avg(數(shù)學(xué)) from 學(xué)生成績;
3、分組查詢
##group by
select sex, count(sex) from 學(xué)生成績 group by sex; ##男女生數(shù)量
select sex, avg(數(shù)學(xué)),count(id) from 學(xué)生成績 group by sex; ##男女生數(shù)學(xué)平均成績
– (注意:只加分組字段和聚合函數(shù),不要添加額外字段)
select sex, avg(數(shù)學(xué)),count(id) from 學(xué)生成績 where 數(shù)學(xué)>70 group by sex; ##數(shù)學(xué)<=70并不參與分組
select sex, avg(數(shù)學(xué)),count(id) from 學(xué)生成績 where 數(shù)學(xué)>70 group by sex HAVING count(id)>2;
– where 在分組之前進(jìn)行,have在分組之后
4、分頁查詢
select * from 學(xué)生成績 limit m,n;
##m是索引數(shù)據(jù)的第m+1條
##n是一頁顯示的條數(shù)
##該語句只能在mysql中用
##約束
1、主鍵約束:primary key
2、非空約束:not null
3、唯一約束:unique
4、外鍵約束:foreign key
5、自動增長:auto_increment ##必須配合主鍵使用
##刪除主鍵和唯一的方式
alter table 學(xué)生成績 drop primary key
alter table 學(xué)生成績 drop index name
##添加外鍵
constraint 外鍵名稱 foreign key (外鍵列名稱) references 主表名稱(字段)
4. 數(shù)據(jù)控制語言DCL
數(shù)據(jù)控制語言DCL用來授予或回收訪問數(shù)據(jù)庫的某種特權(quán),并控制
數(shù)據(jù)庫操縱事務(wù)發(fā)生的時間及效果,對數(shù)據(jù)庫實行監(jiān)視等。如:
GRANT:授權(quán)。
ROLLBACK [WORK] TO [SAVEPOINT]:回退到某一點。
回滾—ROLLBACK
回滾命令使數(shù)據(jù)庫狀態(tài)回到上次最后提交的狀態(tài)。其格式為:
SQL>ROLLBACK;
COMMIT [WORK]:提交。
數(shù)據(jù)庫備份:
mysqldump -uroot -p123456 db1> D://a.sql
將數(shù)據(jù)庫db1備份至a.sql
總結(jié)
以上是生活随笔為你收集整理的DML、DDl、DQL实战的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ps3什么型号的机型好?
- 下一篇: 如何改变Idea的背景