MySQL知识要点3--SQL分类(DDL、DML、DCL、TCL)以及详解

SQL(结构化查询语言)

一般分为数据定义语言DDL、数据操纵语言DML、数据库控制语言DCL、事务控制语言TCL

数据定义语言DDL

DDL一般包括create、drop、use、arter

create 创建
例如:
A. create--创建数据库
   create database student;
B. create--创建数据表
   create table if not exists 'student_score'(
	  'id' int not null auto_increment,
	  'name' varchar(100) not null,
	  'score' int not null,
	  primary key('id')
   )engine=innodb default charset=utf8;

解析:exists 存在, auto_increment 自增,primary key 主键,engine 设置引擎,charset 设置编码
engine一般有两种 myisam与innodb,区别为:myisam读取效率更高,适用于查询次数远远高于更新次数的数据表,此外myisam不支持事务、外键等高级操作,innodb功能更强,同时支持事务、外键等高级操作

drop 删除
例如:
A. drop--删除数据库
   drop database student;
B. drop--删除数据表
   drop table student_score;

解析:drop database 库名; 删除数据库
drop table 表名; 删除数据表

use 使用,定位
use student;

解析:表示定位到student数据库中

alter 修改
A. alter--删除、添加或修改表字段
   alter table student_score add test int;
   alter table student_score drop test;
B. alter--修改字段类型与名称
   alter table student_score modify name char(3);
   alter table student_score change name name varchar(100);
   alter table student_score alter score set default 0;
   alter table student_score alter score drop default;
   alter table student_score rename to student_scores;

解析:修改特征类型及特征名可以使用modify和change,当需要修改特征默认值时,可以使用双重alter进行操作,同时使用set或drop,当需要更好数据库名时,可以使用rename to 进行操作

数据操纵语言DML

DML一般包括insert、updata、delete、select

insert 增加、插入
A.insert into student_scores(id,name,score) values (1,'张三'85);
B.insert into student_scores values(2,'李四',88);

解析:A与B等价,都可以成功插入信息,但A中的具体数据需要与表名之后的特征顺序保持一致,而B中的具体数据需要与数据库的特征顺序保持一致

updata 修改
A. updata student_scores set score=100 where name='张三';

解析:可以同时修改一个样本中的多个信息

delete 删除
A. delete from student_scores where id=2;

解析:语法格式为delete from 表名 where 条件

select 查询
# 查询第二个样本
A. select * from student_scores limit 1 offset 1;
# 查询成绩介于80到100的样本
B. select * from student_scores where score between 80 and 100;
# 查询成绩为66或77或88的样本
C. select * from student_scores where score in(66,77,88);
# 将查询出的工资变为0-1之间的小数
D. select score*0.01 nscore from student_scores ;
# 将查询出的数据去重
E. select distinct score from student_scores;
# 根据成绩降序排序,相同成绩的按照id排序,取第2个到第6个样本 asc 升序, 默认为升序
F. select * from student_scores order by score, id desc limit 5 offset 2;
# 模糊查询  匹配出所有成绩以8开头的数据,一般是作为文本模糊查询, '_l%' 匹配第二个字母为l的样本
G. select * from student_scores where score like '8%';
# 匹配特征不为空的样本
H. select * from student_scores where score is not null;

解析:limit表示查询几条数据,offset表示跳过几条数据,上述语句表示查询第二条数据

拓展

drop、delete、truncate都有删除表的作用,区别点在哪?

A. delete和truncate只是删除表中的数据,该表还存在,例如抹除记忆,但人还活着,
而drop是直接从根源上抹除数据库,相当于这个人已经不存在了;
再细致的划分delete和truncate,delete可以灵活的删除表中的数据,可以是一条、多条甚至是全部,
而truncate是先删除表(相当于drop),随后创建一个包含原有各个特征的新表,表内数据全部为空

B. delete是DML(数据库操纵语言),而drop和truncate是DDL(数据库定义语言),
前者操作完之后可以在提交事务前回滚,后者是操作完马上生效

C. 执行速度上,drop>truncate>delete
DCL 数据库控制语言

一般为管理员进行操作的语言,了解即可,例如新增用户,删除用户,授权等等

TCL 事务控制语言

暂时不讲,会在之后的事务中进行详细讲解

如有错误请留言,万分感激,感谢大家斧正

猜你喜欢

转载自blog.csdn.net/qq_31307291/article/details/87864254
今日推荐