MySQL教程DML數(shù)據(jù)操縱語言示例詳解
1.數(shù)據(jù)操縱語言(DML)
數(shù)據(jù)操縱語言全稱是Data Manipulation Language,簡稱是DML。DML主要有四個(gè)常用功能,如下表所示,其中DML中的查詢功能是作為一名數(shù)據(jù)分析師常用的操作。查詢知識(shí)會(huì)穿插在之后的所有文章中講述,因?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è)列之間用逗號(hào)隔開" 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釋放表占用的空間(會(huì)重置主鍵自增)
③ delete是逐行刪除,刪除記錄是作為事務(wù)記錄在日志文件中,可進(jìn)行回滾操作。truncate一次性刪除表中所有數(shù)據(jù),刪除記錄不會(huì)記錄在日志文件中,無法恢復(fù),刪除效率高于delete。
以上就是MySQL教程DML數(shù)據(jù)操縱語言示例詳解的詳細(xì)內(nèi)容,更多關(guān)于DML數(shù)據(jù)操縱語言的資料請關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。