MySQL學習筆記 增刪改查

2021-10-25 00:01:28 字數 2789 閱讀 3637

##目錄增刪

改查語法:

insert into 表名(欄位1,欄位2,……)

values(值1,值2,……);

特點:

(1) 插入單行資料

#方式一:insert into ...values...

#案例1:傳統的插入

insert into beauty(id,name,***,borndate,phone,photo,boyfriend_id)

values(100,'趙麗穎','女','1986-1-1','15811111111',null,10);

#案例2:欄位的插入順序和表中的順序不一致

insert into beauty(***,borndate,phone,id,name,photo,boyfriend_id)

values('女','1986-1-1','15811111111',101,'小燕子',null,10);

#案例3:不可以為null的字段如何處理

insert into beauty(***,borndate,phone,id,name,photo,boyfriend_id)

values('女','1986-1-1','15811111111',null,'小鴿子',null,10);

#案例4:可以為null的字段的處理

insert into beauty(id,name,phone)

values(103,'紫薇','119');

#案例5:字段可以省略【可讀性較差】

insert into beauty

values(104,'趙麗蓉','女','1986-1-1','15811111111',10);

#方式二:insert into...set

insert into beauty set id=105,name='關雎爾',phone='999';

(2) 插入多行資料
#方式一:insert into...values

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

values(106,'古力娜扎','119',1),

(107,'范冰冰','112',1),

(108,'吉克雋逸','888',1);

#方式二:insert into...select

insert into beauty(name,phone)

select '唐藝昕2','999' union

select '李沁2','888' union

select '董潔2','777'

語法:

delete from 表名 [where 條件];
#案例1:刪除**號碼的長度小於11位的女神資訊

delete from beauty

where length(phone)<11;

#案例2:刪除小燕子

delete from beauty

where name='小燕子';

#多表級聯刪除

delete 別名1,別名2 from 表1 別名1,表2 別名2

where 連線條件

and 篩選條件

#案例3:將張無忌的女朋友都刪除

delete b from beauty b,boys bo

where b.boyfriend_id=bo.id

and bo.boyname='張無忌';

#案例4:將黃曉明的女朋友資訊和黃曉明都刪除

delete b,bo from beauty b,boys bo

where b.`boyfriend_id`=bo.`id`

and bo.`boyname`='黃曉明';

#案例5:將沒有男朋友的女神刪除

delete b from beauty b

left join boys bo on b.`boyfriend_id`=bo.`id`

where bo.`id` is null

#刪除的方式二:

#語法:

#truncate table 表名

#案例1:刪除beauty表的記錄

truncate table beauty

兩種刪除方式區別:

語法:

update 表名 set 欄位名=新值,欄位名=新值

[where 條件];

#案例1:將趙麗蓉更新為 關曉彤

update beauty set name='關曉彤'

where name='趙麗蓉';

#案例2:將11號的女神,更改為名字是楊紫,生日1998-1-1

update beauty set name = '楊紫',borndate='1998-1-1'

where id=11;

#更新多表的資料

#語法update 表1 別名1,表2 別名2

set 欄位1=新值,欄位2=新值

where 連線條件

and 篩選條件;

#案例:將張無忌的女朋友的**都更改為88888

update beauty b,boys bo

set phone='88888'

where b.`boyfriend_id`=bo.`id`

and bo.`boyname`='張無忌'

查詢這一部分在我的另外一篇部落格mysql學習筆記-查詢裡面進行了詳細的講解,有興趣的同學可以去看一下。

MySQL學習筆記 增刪改查

有關資料庫的dml操作 insert into delete truncate update select 條件查詢 查詢排序 聚合函式 分組查詢 drop truncate delete delete刪除資料,保留表結構,可以回滾,如果資料量大,很慢,回滾就是因為備份刪除的資料 truncate刪...

MySQL學習筆記 增刪改查

增刪改 查語法 insert into 表名 欄位1,欄位2,values 值1,值2,特點 1 插入單行資料 方式一 insert into values.案例1 傳統的插入 insert into beauty id,name,borndate,phone,photo,boyfriend id ...

學習筆記 mySQL的增刪改查

1.增加庫 create database dbname 2.增加表 create table tname 欄位1 資料型別 約束 欄位2 資料型別 約束 建立表的同時也可以指定引擎和預設字符集 create table tname id int 4 engine innodb default ch...