走向DBA之SQL语句应用(DDL\DML)

2019-06-17  本文已影响0人  国王12
(前篇已经介绍了SQL语句的定义和介绍等基本信息,相信大家对SQL语句有了基本的了解和掌握,接下来干正事)

一、DDL类语句应用(增删改)

针对库:

create database oldguo charset utf8mb4;
创建库:oldguo 同时指定规范字符集:utf8mb4

drop database oldguo;
删除oldguo库

alter database oldguo charset utf8mb4;   修改字符集为utf8mb4,默认是拉丁文

查(不属于DDL语句,但是在这里需要用到)

show databases;                  查看所有库
show create database oldguo;     查看oldguo库的详细信息

针对表:

首先需要说一下,关于建表的规范

1.库名,字母必须小写。表名尽量也全小写。为啥?一般开发人写代码在windos写,windos不区分大小写,上线代码到linux,linux系统是严格区分大小写的,那么就会产生代码大小写问题,导致找不到库名或者表名。
2.不能以数字开头。
3.不支持中划线,支持下划线。
4.内部函数名不能使用
5.名字和业务功能有关的
6.敏感字符不能创建的话,用反引号引起来

复习一下上篇简书写到的数据类型(常用)

int                数字取值范围 -2^31到2^31-1
tinyint            数字取值范围 0-255
primary key        主键
auto_increment     自增
comment            注释说明
default            默认值
unsigned           无符号
engine            存储数据类型   一般定义为:engine=innodb
charset           指定字符集    一般定义为:charset=utf8mb4
为什么要复习呢?因为这些比较常用,重点。

建表

create table oldguo (
ID int not null primary key AUTO_INCREMENT comment '学号',
name varchar(255) not null comment '姓名', 
age tinyint unsigned not null default 0 comment '年龄',
gender enum('m','f','n') NOT null default 'n' comment '性别'
)charset=utf8mb4 engine=innodb;
说明:oldguo为表名。
注意:信息栏中最后一行就是gender那一行信息,结尾不需要逗号。

建立一个相同表结构的空表

create table oldboy like oldguo;
注意:空表是:oldboy  原表:oldguo

改表:

(增加列)

1.增加列(默认在最后边添加)
alter table oldguo add telnum char(11) not null unique comment '手机号';
在oldguo表下添加telnum列

2.在指定列后边添加列

alter table oldguo add qq varchar(255) not null unique comment 'qq' after name;
在oldguo表下添加qq列,添加在name列后边。

3.添加列到首列

alter table oldguo add wechat varchar(255) not null unique comment '微信' first;
在oldguo表下添加wechat列,位置在首列

(删除列)

alter table oldguo drop state;
删除oldguo表下的state列

(修改表属性)

1.更改基本属性

alter table oldguo modify name varchar(128) not null;
把oldguo表下的name列的属性信息更改为varchar(128),not null
注意:本次修改,会把之前定义该列的一切信息覆盖,即为重新定义

2.更改包括列名的属性

alter table oldguo chage gender gg char(1) not null default 'n' ;
修改oldguo列属性,把列名改为gg,其他属性一并更改为char(1),not null 默认值n

二、DML数据操作语言

desc oldguo;
查看oldguo表的列的属性信息
select * from oldguo;
查看oldguo表的具体内容

1.insert插入

1.1简单的插入数据的方法:

insert into oldguo values(1,'oldboy','480070779','25');
这种插入方法比较简单,但是需要知道每一列都是代表什么内容,插入的数据才有意义;
分别是:序列号,名字,qq号,年龄

1.2.专业插入数据的写法:

insert into oldguo(name,qq,age)values('oldboy','480070779','25');
前边指定列,后边依次写入插入该列的具体内容,为专业写法。

1.2.1同时插入多行信息:

values可同时插入多行数据,一个括号代表一行之,括号之间逗号隔开即代表不同的行。比如:
insert into oldguo(name,qq,age)values('oldboy','480070779','25'),('xiaobai','33044034','18');

2.update更新

update oldguo set age='25' where id=1;
在oldguo表里,更新id号为1的(1对应oldboy这个人),age列改成25,
where相当于匹配条件

3.delete删除

3.1匹配删除

delete from oldguo where id=4;
在oldguo表里,删除id号等于4的一行

3.2删除全表

delete from oldguo;
不写匹配条件,即为删除oldguo表的所有内容(极其危险)

3.3删除全表

truncate table oldguo;
删除oldguo表所有内容
上述两种删除全表的方法都是及其危险的,慎用!若非要用,他们二者的区别是:
delete删除 :逻辑逐行删除,不会降低自增长的起始值。效率很低,碎片较多,会影响到性能。(日常删除使用)
truncate删除:属于物理删除,将表段中的区进行清空,不会产生碎片,性能较高(清空使用)

在企业中,一般不会删除什么数据(危险),下边教你使用update替换delete

第一步:添加state状态列(0代表数据存在,1代表数据删除)

alter table oldguo add state tinyint not null default 0;
定义默认值为0,即为存在

第二步:举例

delete from oldguo where id=6; 把删除id=6的行的行为替换为:
update oldguo set state=1 where id=6; 把id=6的行的状态码改为1,即代表删除

第三步:业务语句修改

select * from
select * from oldguo ; 把原本查看表数据的SQL语句改为:
select * from oldguo WHERE state=0; 匹配状态码为0的数据。
上一篇下一篇

猜你喜欢

热点阅读