当前位置:网站首页>DDL和DML的补充
DDL和DML的补充
2022-08-04 11:16:00 【parker_001】
【1】sql展示:
-- 创建表:
create table t_student(
sno int(6) primary key auto_increment,
sname varchar(5) not null,
sex char(1) default '男' check(sex='男' || sex='女'),
age int(3) check(age>=18 and age<=50),
enterdate date,
classname varchar(10),
email varchar(15) unique
);
-- 添加数据:
insert into t_student values (null,'张三','男',21,'2023-9-1','java01班','[email protected]');
insert into t_student values (null,'李四','男',21,'2023-9-1','java01班','[email protected]');
insert into t_student values (null,'露露','男',21,'2023-9-1','java01班','[email protected]');
-- 查看学生表:
select * from t_student;
-- 添加一张表:快速添加:结构和数据跟t_student 都是一致的
create table t_student2
as
select * from t_student;
select * from t_student2;
-- 快速添加,结构跟t_student一致,数据没有:
create table t_student3
as
select * from t_student where 1=2;
select * from t_student3;
-- 快速添加:只要部分列,部分数据:
create table t_student4
as
select sno,sname,age from t_student where sno = 2;
select * from t_student4;
-- 删除数据操作 :清空数据
delete from t_student;
truncate table t_student;
【2】delete和truncate的区别:
从最终的结果来看,虽然使用TRUNCATE操作和使用DELETE操作都可以删除表中的全部记录,但是两者还是有很多区别的,其区别主要体现在以下几个方面:
(1)DELETE为数据操作语言DML;TRUNCATE为数据定义语言DDL。
(2) DELETE操作是将表中所有记录一条一条删除直到删除完;TRUNCATE操作则是保留了表的结构,重新创建了这个表,所有的状态都相当于新表。因此,TRUNCATE操作的效率更高。
(3)DELETE操作可以回滚;TRUNCATE操作会导致隐式提交,因此不能回滚(在第十章中会讲解事务的提交和回滚)。
(4)DELETE操作执行成功后会返回已删除的行数(如删除4行记录,则会显示“Affected rows:4”);截断操作不会返回已删除的行量,结果通常是“Affected rows:0”。DELETE操作删除表中记录后,再次向表中添加新记录时,对于设置有自增约束字段的值会从删除前表中该字段的最大值加1开始自增;TRUNCATE操作则会重新从1开始自增。
边栏推荐
- 将博客搬至CSDN
- 使用.NET简单实现一个Redis的高性能克隆版(二)
- Mysql高级篇学习总结13:多表连接查询语句优化方法(带join语句)
- 『快速入门electron』之实现窗口拖拽
- datax oracle to oracle incremental synchronization
- Leetcode刷题——543. 二叉树的直径、617. 合并二叉树(递归解决)
- JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers
- 图文手把手教程--ESP32 MQTT对接EMQX本地服务器(VSCODE+ESP-IDF)
- 【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库
- 剑指长城炮? 长安全新皮卡官方谍照
猜你喜欢

Leetcode刷题——543. 二叉树的直径、617. 合并二叉树(递归解决)

学会使用set和map的基本接口

面试蚂蚁(P7)竟被MySQL难倒,奋发图强后二次面试入职蚂蚁金服

【飞控开发高级教程7】疯壳·开源编队无人机-编队飞行

Leetcode——利用先序遍历特性完成114. 二叉树展开为链表

Using .NET to simply implement a high-performance clone of Redis (2)

深度强化学习与APS的一些感想

【励志】复盘的重要性

图文手把手教程--ESP32 一键配网(Smartconfig、Airkiss)

Advanced transcriptome analysis and R data visualization hot registration (2022.10)
随机推荐
JUC (1) threads and processes, concurrency and parallelism, thread state, locks, producers and consumers
Leetcode brush questions - 543. Diameter of binary trees, 617. Merging binary trees (recursive solution)
Oracle中对临时表空间执行shrink操作
深度强化学习与APS的一些感想
音频编辑 合唱
Leetcode刷题——路径总和
光盘刻录步骤
学会使用set和map的基本接口
Super Learning Method
深度学习100例 —— 卷积神经网络(CNN)天气识别
Win11 file types, how to change?Win11 modify the file suffix
喂,你知道节流是什么吗?
iMeta | German National Cancer Center Gu Zuguang published a complex heatmap visualization method
小程序实战(一)- 骨架屏的应用与实现
六石编程学:编程中的直线思维与自然思维
遍历Map的四种方法
SkiaSharp 之 WPF 自绘 粒子花园(案例版)
MATLAB程序设计与应用 3.1 特殊矩阵
shell变量
【LeetCode】701.二叉搜索树中的插入操作