DML 語言 增刪改

2021-10-08 04:33:59 字數 3349 閱讀 3193

總體而言資料操作語言分為三種:插入insert,更新update,刪除delete

一,插入語句

語法:方式一:insert into 表名(列名,…) values(值1,…) -->支援插入多行/支援子查詢

方式二:insert into 表名 set 列名=值,列名=值,…

特點:插入值型別要與列型別保持一致

不可以為null的列必須插入值,可以為null的列如何插入

(1).有列名值為null

(2).列名和值都省略

列的順序可以顛倒

列數和值個數必須一致

可以省略列名,此時為表中順序的所有列

## 1

insert

into beauty(

`id`

,`name`

,`***`

,`borndate`

,`phone`

,`photo`

,`boyfriend_id`

)values(13

,'李明'

,'女'

,'1990-4-23'

,'18988888888'

,null,2

);## 2

insert

into beauty(

`id`

,`name`

,`***`

,`borndate`

,`phone`

,`boyfriend_id`

)values(14

,'李華'

,'女'

,'1990-4-23'

,'18988888888',2

);## 3

insert

into beauty(name,***,id,phone,boyfriend_id)

values

('韓磊磊'

,'女',15

,'15788667789'

,'3'

)## 5

insert

into beauty values(16

,'李梅'

,'女'

,null

,'18976554344'

,null

,null

)##6

insert

into beauty

set id=

17,name=

'劉濤'

,phone=

'17856435677'

;##插入多行

insert

into beauty values(19

,'李梅1'

,'女'

,null

,'18976554344'

,null

,null),

(20,'李梅2'

,'女'

,null

,'18976554344'

,null

,null

)## 子查詢

insert

into beauty(id,name,phone)

select id,boyname,

'17867542345'

from boys where id<

3;

二,更新語句

修改單錶記錄

update 表名 set 列=新值,… where 篩選條件;

修改多表記錄

sq192語法:

update 表1, 表2

set 列=新值,…

where 連線條件 and 篩選條件;

sq199語法:

update 表1

inner/left outer/right outer join 表2

on 連線條件

set 列=新值,…

where 篩選條件;

## 1

update beauty

set phone=

'1110'

where name like

'李%'

## 2

update boys

set boyname=

'張飛'

,usercp=

10where id=2;

## 3

update boys bo

inner

join beauty b

on b.

`boyfriend_id`

=bo.

`id`

set b.

`phone`

='114'

where bo.

`boyname`

='張無忌'

;## 4

update beauty b

left

join boys bo

on b.

`boyfriend_id`

=bo.

`id`

set b.

`boyfriend_id`=2

where bo.

`id`

isnull

三,刪除語句

兩種方式區別:

delete 可以加刪除條件

truncate 刪除效率高

假如要刪除的表中有自增長列,使用delete時新插入語句從斷點開始,使用truncate從1開始

truncate 不會返回受影響的行數

truncate 刪除不能回滾,delete可以回滾

# 方式一

## 1

delete

from beauty where phone like

'%9'

## 2

delete b from beauty b join boys bo on b.

`boyfriend_id`

=bo.

`id`

where bo.

`boyname`

='張無忌'

;##

delete b,bo from beauty b join boys bo on b.

`boyfriend_id`

=bo.

`id`

where bo.

`boyname`

='黃曉明'

;# 方式二

truncate

table boys;

mysql之dml語言 增刪改

語法 insert into 表名 欄位名1,欄位名2,欄位名3.value 值1 值2 值3 示例insert into grade gradename values 大四 insert into grade gradename values 大二 大一 insert into student n...

mysql基礎 DML語言,增刪改

dml語言即資料操作語言 插入 insert 修改 undate 刪除 delete 一.插入語句 方式一 語法 insert into 表名 欄位名,values 值1,案例1.插入的值的型別與列的型別一致或相容 insert into beauty id,name,borndate,phone,...

DML(增刪改查)

在實際專案中,使用最多的是讀取操作,但是插入資料和刪除資料同等重要,而修改操作相對較少 插入操作 元組值的插入 查詢結果的插入 最基本的插入方式 insert into tablename values val1,val2,如果表名之後沒有列,那麼只能將所有的列都插入 insert into tab...