SQL基本操作 - 新增、查看、更新、删除表中数据
16lz
2021-01-22
新增数据
有两种方案
方案1:给全表字段插入数据,不需要指定字段列表,要求数据的值出现的顺序必须与表中设计的字段出现的顺序一致;凡是非数值数据,都需要使用引号(建议是单引号)
insert into 表名 values(值列表)[,(值列表)]; --可以一次性插入多条记录
mysql> insert into t_student values('zhangsan',22),
-> ('lisi',23);
Query OK, 2 rows affected (0.05 sec)
Records: 2 Duplicates: 0 Warnings: 0
方案2:给部分字段插入数据,需要选定字段列表,字段列表出现的顺序与字段的顺序无关,但是值列表的顺序必须与选定的字段的顺序一致。
insert into 表名 (字段列表) values (值列表)[,(值列表)];
mysql> insert into t_student (age, name) values (12, 'Tom'),(15, 'Anonymous');
Query OK, 2 rows affected (0.00 sec)
Records: 2 Duplicates: 0 Warnings: 0
查看数据
select */字段列表 from 表名 [where 条件];
--查看t_student表中的所有数据
mysql> select * from t_student;
+-----------+------+
| name | age |
+-----------+------+
| zhangsan | 22 |
| lisi | 23 |
| Tom | 12 |
| Anonymous | 15 |
+-----------+------+
4 rows in set (0.00 sec)
--查看指定字段, 指定条件的数据
mysql> select age,name from t_student where age < 20;
+------+-----------+
| age | name |
+------+-----------+
| 12 | Tom |
| 15 | Anonymous |
+------+-----------+
2 rows in set (0.00 sec)
更新数据
update 表名 set 字段 = 值 [where 条件]; --若不加where, 则是更新全部
mysql> update t_student set age = 26 where name = 'Anonymous';
Query OK, 1 row affected (0.17 sec)
Rows matched: 1 Changed: 1 Warnings: 0
删除数据
(删除不可逆, 操作需谨慎!)
delete from 表名 [where 条件];
mysql> delete from t_student where age > 18;
Query OK, 3 rows affected (0.09 sec)
更多相关文章
- Mysql完全手册(笔记二,使用数据与性能优化)
- 求指导,PB12.0 如何用ini文件 连接sql 2005数据库
- MySQL Cluster在线添加数据节点
- logstash-jdbc-input与mysql数据库同步
- Python将MySQL表数据写入excel
- Spark踩坑记——数据库(Hbase+Mysql)转
- 如何构建将被输入下拉框的数据?
- 10个MySQL数据库备份教程推荐
- 请教用sql loader导入数据,最后一位是整数的问题