8.MySQL 数据操作 DML
8.MySQL?數(shù)據(jù)操作?DML
數(shù)據(jù)的DML操作:添加數(shù)據(jù),修改數(shù)據(jù),刪除數(shù)據(jù)
添加數(shù)據(jù)
格式:?insert into?表名[(字段列表)] values(值列表...);
--標(biāo)準(zhǔn)添加(指定所有字段,給定所有的值)
mysql>?insert?into?stu(id,name,age,sex,classid)?values(1,'zhangsan',20,'m','lamp138');
Query OK,?1?row affected (0.13?sec)
mysql>
--指定部分字段添加值
mysql>?insert?into?stu(name,classid) value('lisi','lamp138');
Query OK,?1?row affected (0.11?sec)
--?不指定字段添加值
mysql>?insert?into?stu value(null,'wangwu',21,'w','lamp138');
Query OK,?1?row affected (0.22?sec)
--?批量添加值
mysql>?insert?into?stu?values
-> (null,'zhaoliu',25,'w','lamp94'),
-> (null,'uu01',26,'m','lamp94'),
-> (null,'uu02',28,'w','lamp92'),
-> (null,'qq02',24,'m','lamp92'),
-> (null,'uu03',32,'m','lamp138'),
-> (null,'qq03',23,'w','lamp94'),
-> (null,'aa',19,'m','lamp138');
Query OK,?7?rows affected (0.27?sec)
Records:?7?Duplicates:?0?Warnings:?0
修改數(shù)據(jù)
格式:update?表名?set?字段1=值1,字段2=值2,字段n=值n... where?條件
--?將id為11的age改為35,sex改為m值
mysql>?update?stu?set?age=35,sex='m'?where?id=11;
Query OK,?1?row affected (0.16?sec)
Rows matched:?1?Changed:?1?Warnings:?0
--?將id值為12和14的數(shù)據(jù)值sex改為m,classid改為lamp92
mysql>?update?stu?set?sex='m',classid='lamp92'?where?id=12?or?id=14?--等價(jià)于下面
mysql>?update?stu?set?sex='m',classid='lamp92'?where?id?in(12,14);
Query OK,?2?rows affected (0.09?sec)
Rows matched:?2?Changed:?2?Warnings:?0
刪除數(shù)據(jù)
格式:delete from?表名?[where?條件]
--?刪除stu表中id值為100的數(shù)據(jù)
mysql> delete from stu where id=100;
Query OK, 0 rows affected (0.00 sec)
--?刪除stu表中id值為20到30的數(shù)據(jù)
mysql> delete from stu where id>=20 and id<=30;
Query OK, 0 rows affected (0.00 sec)
--?刪除stu表中id值為20到30的數(shù)據(jù)(等級(jí)于上面寫法)
mysql> delete from stu where id between 20 and 30;
Query OK, 0 rows affected (0.00 sec)
--?刪除stu表中id值大于200的數(shù)據(jù)
mysql> delete from stu where id>200;
Query OK, 0 rows affected (0.00 sec)
總結(jié)
以上是生活随笔為你收集整理的8.MySQL 数据操作 DML的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【每日SQL打卡】
- 下一篇: 最短路径实现