增加
语法:
insert into table_name[(column[,column...])]values (value [,value...]);
示例:
1. 创建一张商品表
mysql> create table goods( -> id int unsigned primary key,
-> goods_name varchar(50) not null default '',
-> price float not null default 0.0 -> );
Query OK, 0 rows affected (0.01 sec)
2. 插入两条记录
mysql> insert into goods values(100, '牛排', 78.5);
Query OK, 1 row affected (0.00 sec)
mysql> insert into goods values(101, '披萨', 27.5);
Query OK, 1 row affected (0.00 sec)
mysql> select * from goods;
使用添加语句注意的细节:
插入的数据应与字段的数据类型相同。比如,将‘abc’插入到id列就不行:
mysql> insert into goods values('abc', 'pizza', 72);
ERROR 1366 (HY000): Incorrect integer value: 'abc' for column 'id' at row 1
数据的大小应在规定的范围内,例如:不能将一个长度为80的字符串插入到长度为40的列中。
mysql> create table tt20(name varchar(5));
Query OK, 0 rows affected (0.00 sec)
mysql> insert into tt20 values('abcdef');
ERROR 1406 (22001): Data too long for column 'name' at row 1
在values中列出的数据位置必须与被加入的列位置相对应。
mysql> insert into goods values(200, 3.4,'steak'); --价格和名字写反了
ERROR 1265 (01000): Data truncated for column 'price' at row 1
字符和日期类型应该包含在单引号中。
mysql> insert into goods values(123, fish, 50); --字符串没有用单引号
ERROR 1054 (42S22): Unknown column 'fish' in 'field list'
插入空值,不指定或insert into table values(null)
insert into table values(),(),() 一次性添加多条记录
mysql> insert into goods values(1,'aa',3.3),(2,'bb',4.4),(3,'cc',5.5); --一次性添加三条记录
Query OK, 3 rows affected (0.00 sec)
如果给表中的所有字段添加数据,可以不写前面的字段名称
mysql> insert into goods values(4,'phone',2.5);--如果没有给出字段名称,values中必须给出所有的字段值
Query OK, 1 row affected (0.00 sec)
如果你只给表的某几个字段赋值,则需要制定字段名
mysql> insert into goods(id, goods_name) values(5, 'mouse');
Query OK, 1 row affected (0.01 sec)
增加进阶
在数据插入的时候,假设主键对应的值已经存在:插入失败!
mysql> insert into goods values(101, 'ccc', 20.5);
ERROR 1062 (23000): Duplicate entry '101' for key 'PRIMARY'
当主键存在冲突的时候(duplicate key),可以选择性的进行处理:
1. 更新操作
insert into 表名(字段列表) values(值列表) on duplicate key update 字段=新值;
例:
mysql> insert into goods values(101, 'ccc', 20.5) on duplicate key update goods_name='ccc', price=20.5;
Query OK, 2 rows affected (0.06 sec)
2.替换:主键如果没有冲突,就直接插入
replace into 表名(包含字段) values(值列表);
例:
mysql> replace into goods values(100, 'huawei', 999);
Query OK, 2 rows affected (0.01 sec)
修改
更新表中的数据
语法:
update tbl_name set col_name1=expr1, [, col_name2=expr2 ...] [where conditon]
例:将所有产品的价格修改为300块
mysql> update goods set price=300; -- 没有条件,整表修改
Query OK, 2 rows affected (0.01 sec) Rows matched: 2 Changed: 2 Warnings: 0
mysql> select * from goods;
例:将id为100的产品价格修改为1000
mysql> update goods set price=1000 where id=100;
Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from goods where id=100;
例:将id为101的产品价格增加200块
mysql> update goods set price=price+200 where id=101;
Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from goods where id=101;
update使用细节:
update 语法可以用心值更新原有表中的各列
set子句指示要修改哪些列和要给予哪些值
where子句指定应更新哪些行。如果没有where子句,则更新所有行
如果需要更新多个字段,可以通过 set 字段1=值1,字段2=值2...
更新还可以限制更新数量
update 表名 set 字段=值 [where 条件] [limit 更新数量];
比如,goods表中有5条ccc产品。我们希望将前3条改成ddd
mysql> select * from goods where goods_name='ccc';
执行更新操作:
mysql> update goods set goods_name='ddd' where goods_name='ccc' limit 3;
Query OK, 3 rows affected (0.01 sec) Rows matched: 3 Changed: 3 Warnings: 0
查看结果:
mysql> select *from goods;
删除
删除表中的数据
语法:
delete from tbl_name [where condition]
示例:删除表中id为101的数据
mysql> delete from goods where id=101;
Query OK, 1 row affected (0.00 sec)
额外学一招:在练习删除时,可以复制一份表,避免数据删没了
1. 复制表结构
mysql> create table goods2 like goods;
Query OK, 0 rows affected (0.01 sec)
2. 把goods表的数据复制到goods2
mysql> insert into goods2 select * from goods;
Query OK, 5 rows affected (0.00 sec) Records: 5 Duplicates: 0 Warnings: 0
删除表中的所有记录
mysql> delete from goods; --删除整个表的数据,但是表的结构还存在
使用truncate删除表中的记录
mysql> truncate table goods; --这个指令也把整个表记录删除
上述两种删除整表的区别:
效果一样,truncate速度快
delete可以带where条件,删除更加灵活
delete可以返回被删除的记录数,而truncate返回0
推荐使用delete
delete使用细节:
如果不适用where子句,将删除整个表中所有数据
delete语句不能删除某一列的值(可以用update置null)
使用delete语句仅删除记录,不删除表本身(drop table)
—知识未完,待看下篇!!!