欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

MySQL教程DML數(shù)據(jù)操縱語言示例詳解

 更新時(shí)間:2021年10月26日 15:22:03   作者:數(shù)據(jù)分析與統(tǒng)計(jì)學(xué)之美  
這篇文章主要為大家介紹了MySQL教程中DML數(shù)據(jù)操縱語言的示例詳解,要想學(xué)好MySQL最重要的是要先學(xué)好數(shù)據(jù)操縱語言DML,本文對其進(jìn)行了全面的講解

1.數(shù)據(jù)操縱語言(DML)

數(shù)據(jù)操縱語言全稱是Data Manipulation Language,簡稱是DML。DML主要有四個(gè)常用功能,如下表所示,其中DML中的查詢功能是作為一名數(shù)據(jù)分析師常用的操作。查詢知識會穿插在之后的所有文章中講述,因?yàn)檫@個(gè)問題不是一下子可以講的完的。今天的文章主要是講述增、刪、改這幾個(gè)技能的用法。

insert delete update select

下面的操作都是基于這個(gè)student表進(jìn)行的。

# 創(chuàng)建數(shù)據(jù)庫
create database if not exists stu;
# 使用數(shù)據(jù)庫
use stu;
# 創(chuàng)建一個(gè)表
create table student(
    sid int primary key auto_increment,
    sname varchar(20) not null,
    sex varchar(10)
)charset=utf8;

2.增添數(shù)據(jù)(insert)

情況一:給全部字段添加數(shù)據(jù);
-- 有以下兩種添加方式:當(dāng)給所有字段插入數(shù)據(jù)的時(shí)候,可以不寫字段名。
insert into student(sid,sname,sex) values (1,"張三","男");
insert into student values (2,"李莉","女");

情況二:給部分字段添加數(shù)據(jù);
insert into student(sname) values ("王五");
insert into student(sname,sex) values ("趙六","男");

情況三:一次性插入多條數(shù)據(jù);
insert into student(sname,sex) values 
("劉備","男"),("貂蟬","女"),("諸葛亮","男");

結(jié)果如下:

在這里插入圖片描述

3.復(fù)制已有表,生成新表

1)復(fù)制已有表的結(jié)構(gòu)和數(shù)據(jù)。

"創(chuàng)建一個(gè)student1表,表的結(jié)構(gòu)和數(shù)據(jù)均來自于student表。"
mysql> create table student1 select * from student;

操作結(jié)果如下:

在這里插入圖片描述

2)只復(fù)制已有表的結(jié)構(gòu)(得到的是一個(gè)空結(jié)構(gòu)表)。

"創(chuàng)建一個(gè)student2表,只復(fù)制student表的結(jié)構(gòu),不要里面的數(shù)據(jù)。"
mysql> create table student2 select * from student where 0;

操作結(jié)果如下:

在這里插入圖片描述

3)在2的基礎(chǔ)上,向空結(jié)構(gòu)表中插入數(shù)據(jù)。

"在2基礎(chǔ)上,向student2表中插入數(shù)據(jù),數(shù)據(jù)來自于student表"
mysql> insert into student2 select * from student;

操作結(jié)果如下:

在這里插入圖片描述

4.修改數(shù)據(jù)update

update和delete語句要配合where篩選,進(jìn)行使用,否則刪除的就是整張表的記錄。

"語法格式:多個(gè)列之間用逗號隔開"
update 表名 set 列1=值1 [列2=值2,列3=值3…… ] where條件;

"演示示例如下"
-- 把sid為3的王五的姓名,改為王八。
update student set sname="王八" where sid = 3;
-- 把sid為7的諸葛亮的名字改為孔明,性別改為猛男。
update student set sname="孔明",sex="猛男" where sid=7;

操作結(jié)果如下:

在這里插入圖片描述

5.刪除數(shù)據(jù)delete:物理刪除(一旦刪除就徹底沒有了)。

update和delete語句要配合where篩選,進(jìn)行使用,否則刪除的就是整張表的記錄。

"語法格式:"
delete from 表名 where 條件;

"演示示例如下"
delete from student where sname="張三";

操作結(jié)果如下:

在這里插入圖片描述

6.truncate和delete的區(qū)別

用如下數(shù)據(jù)講述這兩個(gè)的區(qū)別:

在這里插入圖片描述

1)delete

在這里插入圖片描述

2)truncate

在這里插入圖片描述

3)truncate和delete的區(qū)別

① 都是不修改結(jié)構(gòu),只清除數(shù)據(jù)。
② delete刪除不釋放資源,truncate釋放表占用的空間(會重置主鍵自增)
③ delete是逐行刪除,刪除記錄是作為事務(wù)記錄在日志文件中,可進(jìn)行回滾操作。truncate一次性刪除表中所有數(shù)據(jù),刪除記錄不會記錄在日志文件中,無法恢復(fù),刪除效率高于delete。

以上就是MySQL教程DML數(shù)據(jù)操縱語言示例詳解的詳細(xì)內(nèi)容,更多關(guān)于DML數(shù)據(jù)操縱語言的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論