SQL三类语句:DML DDL DCL 及相关+

SQL(Structured Query Language)结构化查询语言。
是用于处理和访问数据库的语言。
DB:database 数据库。

常见的数据库
IBM:DB2
Oracle:Oracle、Mysql
Microsoft:SQL server

数据库分为:关系型数据库和非关系型数据库。

对于关系型数据库:通常包含1-多张表。
所谓的关系就是一张二位表。
一张表包含列(字段-field)和行(记录-record)

SQL对大小写不敏感,关键字可大写,可小写。
每条SQL语句最好在结尾加上分号,如果只有1行语句,分号可以不写,
建议写。
数据库中的注释:
// --**
// #
// /**/

SQL语句分为3种类型:
DML:Data Manipulation Language 数据操纵语言
查询和修改数据记录的。
对数据的增(insert)删(delete)改(update)查(select)。
DDL:Data Defintion Language 数据定义语言
用于定义数据库结构的。例如创建、修改、删除表等。
DCL:Data Control Language 数据控制语言。
用于控制数据库的访问。设置访问权限,取消权限,确认,回滚等。

DML:
insert into
delete
update
select
= > >= < <= !=(<>)
and or not
between … and
in()
like % _
distinct
order by
DDL:
create database
drop database
create table
alter table
change
modify
rename
add
drop
drop table
DCL:
commit
rollback

多表连接查询:
等值连接 非等值连接
内连接 外连接
自连接 非自连接

函数
日期函数:
now()-----获取当前时间
字符串函数:
lower(string)----字符串小写
upper(string)----字符串大写
concat(string,string,…)----字符串连接
substr(string,index,count)----求字串,index从1开始。
length(string)----求字符串长度
instr(string,substring)----查找字符串位置。没有返回0
lpad(str,len,padstr)----左补齐
rpad(str,len,padstr)----右补齐
trim(str)----去除左右两侧的空白
replace(orignStr,replace,target)----字符串替换
数值函数:
round(num,dotnum)----四舍五入
truncate(num,dotnum)----截断
mod(num,num2)----求余
条件表达式:
(1)简单表达式
case 字段 when 值 then 操作
when 值 then 操作

end 列的别名
(2)复杂表达式
case when 表达式 then 操作
when 表达式 then 操作

end
分组函数
max()
min()
avg()
sum()
count()

分组:
group by
出现在select后的字段(刨出分组函数),必须出现在group by子句后。

分组后不能用where过滤数据,只能用having过滤,having在group by后

select from where group by having order by limit

子查询也叫内查询,指的是在查询里嵌套查询。

约束:
所谓约束就是表中数据的限制条件。
添加约束的目的:
保证数据的完整性和有效性

约束的种类:
非空约束 not null
not null 约束的字段不能为空值。
可以在创建表的时候为添加字段添加not null约束
唯一性约束 unique
unique约束的字段值必须唯一。但是可以为null,null可以有多个。
主键约束 primary key
主键约束的字段 既要唯一也不能为null。
一个表中只能有一个主键,用来唯一标识一条记录。
一旦你把一个列设置为了主键,你就可以使用auto_increment进行自增
外键约束 foreign key
外键往往关联另外一个表的主键。出现在外键表中的数据一定要出现在主键表中。
检查约束。check—mysql不支持
默认值 default

列级约束
在定义字段的时候添加约束。
表级约束
在定义表的时候添加约束。

创建表create table 表名
1、正常创建
2、基于现有表创建
as select语句
like
修改表alter table 表名
1、添加列 alter table 表名 add 列名 类型(长度) 约束 默认值
2、修改列
(1)修改 类型,长度,约束,默认值等
alter table 表名 modify 列名 类型(长度) 约束 默认值
(2)修改列名等信息
alter table 表名 change 旧列名 新列名 类型(长度) 约束 默认值
3、删除列
alter table 表名 drop column 列名
4、修改表名
alter table 表名 rename to 新表名 —to可以省略
5、添加约束
alter table 表名 add constraint 约束名 约束类型(字段列表)
6、删除约束
alter table 表名 drop index 约束名 —系统会为unique约束的列建索引。删的时候删除索引
7、删除主键约束
alter table 表名 drop primary key
8、删除外键约束
alter table 表名 drop foreign key 约束名
清空表
truncate table 表名 ----无法rollback
delete from 表名------可以rollback
commit;-- 提交 commit之前的所有命令在commit后将永久生效。
ROLLBACK;-- 回滚 在上次commit到本次rollback之间的所有命令将失效,回滚到上次commit的状态。
commit与rollback中间的DML命令称为一个事务。事务具有原子性。
mysql 默认自动commit
删除表 drop table 表名

向表中插入数据
insert into 表名(列名…) select … from 表名 where …

create table teacher10(
id int(5) primary key ,
name varchar(10) not null
)
insert into teacher10(name)
values(‘陈高飞’),(‘金赛赛’)

– DDL 为表添加列
alter table teacher10 add age int(3) not null default 18;

– DDL 为表修改列
1、修改 列的类型,长度,约束等
alter table teacher10 modify name char(20) null default ‘张三’;
desc teacher10
alter table teacher10 modify name varchar(20) not null;
2、修改列的名字和其他信息
alter table teacher10 change name tname varchar(15) not null default ‘李四’;
3、删除列
alter table teacher10 drop column age;
4、修改表名
alter table t100 rename to teacher10;
5、添加约束
alter table teacher10
add constraint teacher10_name_un unique(tname);
insert into teacher10(tname) values(‘高国伟’);
6、删除唯一约束
alter table teacher10 drop index teacher10_name_un;
添加unique约束的时候,mysql会自动给你创建一个index,
删除的时候删除index即可,它会把unique也删掉。
7、删除主键约束
alter table teacher10 drop primary key;
alter table teacher10 add constraint teacher10_pk primary key(id);
desc teacher10
8、删除外键约束
alter table teacher10 drop constraint
create table course10(
id int(3) primary key auto_increment,
name varchar(15) not null unique,
tid int(5)
);
desc course10
alter table course10 add constraint course10_tid_fk
foreign key(tid) references teacher10(id)
on delete cascade on update cascade;

alter table course10 drop foreign key course10_tid_fk;

清空表
DML的delete清空表

delete from t20

truncate table t20
DDL的清空表无法恢复
DML的清空表可以rollback,前提是你没有commit

begin;-- 开始事务
一堆DML
commit;-- 提交事务 让begin和commit之间的DML永久生效

begin;-- 开始事务
一堆DML
rollback;-- 回滚事务 让begin和rollback之间的DML不生效。

insert into t20 values(100,‘aaa’,20,‘男’);
insert into t20 values(105,‘bbb’,18,‘男’);
select * from t20;

delete from t20;

mysql默认每一条DML执行完后自动commit,除非你执行写了begin
或者关闭自动提交

set autocommit = 0

drop table t20;

猜你喜欢

转载自blog.csdn.net/weixin_43791238/article/details/90106189