MySQL 數據操作 DML

MySQL 數據操作 DML

數據的DML操作:添加數據,修改數據,刪除數據

添加數據

格式: insert into 表名[(字段列表)] values(值列表...);

<code>--標準添加(指定所有字段,給定所有的值)
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/<code>

修改數據

格式:update 表名 set 字段1=值1,字段2=值2,字段n=值n... where 條件

<code>-- 將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的數據值sex改為m,classid改為lamp92
mysql> update stu set sex='m',classid='lamp92' where id=12 or id=14 --等價於下面
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/<code>

刪除數據

格式:delete from 表名 [where 條件]

<code>-- 刪除stu表中id值為100的數據
mysql> delete from stu where id=100;
Query OK, 0 rows affected (0.00 sec)

-- 刪除stu表中id值為20到30的數據
mysql> delete from stu where id>=20 and id<=30;
Query OK, 0 rows affected (0.00 sec)

-- 刪除stu表中id值為20到30的數據(等級於上面寫法)
mysql> delete from stu where id between 20 and 30;
Query OK, 0 rows affected (0.00 sec)

-- 刪除stu表中id值大於200的數據
mysql> delete from stu where id>200;
Query OK, 0 rows affected (0.00 sec)/<code>


分享到:


相關文章: