程序媛计划——mysql 插入、查找、修改、删除数据
#插入、查找数据
[mysql>create table if not exists exam_score(
..>id int(4) not null primary key auto_increment,
..>name char(20) not null,
..>score double(6,2)); #用多个list插入多行数据
[mysql> insert into exam_score values (1,'Zhao',95.33),(2,'Qian',94.33),(3,'Sun',44.55),(4,'Li',33.55);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
#展示整张表
mysql> select * from exam_score;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | Zhao | 95.33 |
| 2 | Qian | 94.33 |
| 3 | Sun | 44.55 |
| 4 | Li | 33.55 |
+----+------+-------+
4 rows in set (0.00 sec)
mysql> insert into exam_score values (5,'Zhou',66.33),(6,'Wu',99.32),(7,'Zheng',33.2),(8,'Wang',99.3);
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0
#查询按照字段id在0到2之间的行数据
mysql> select * from exam_score order by id limit 0,2;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | Zhao | 95.33 |
| 2 | Qian | 94.33 |
+----+------+-------+
2 rows in set (0.00 sec)
#查询整张表中的前两行
mysql> select * from exam_score limit 0,2;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | Zhao | 95.33 |
| 2 | Qian | 94.33 |
+----+------+-------+
2 rows in set (0.00 sec)
#按照name字段升序显示整张表
mysql> select * from exam_score order by name asc;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 4 | Li | 33.55 |
| 2 | Qian | 94.33 |
| 3 | Sun | 44.55 |
| 8 | Wang | 99.30 |
| 6 | Wu | 99.32 |
| 1 | Zhao | 95.33 |
| 7 | Zheng | 33.20 |
| 5 | Zhou | 66.33 |
+----+-------+-------+
8 rows in set (0.01 sec)
#按照name降序显示
mysql> select * from exam_score order by name desc;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 5 | Zhou | 66.33 |
| 7 | Zheng | 33.20 |
| 1 | Zhao | 95.33 |
| 6 | Wu | 99.32 |
| 8 | Wang | 99.30 |
| 3 | Sun | 44.55 |
| 2 | Qian | 94.33 |
| 4 | Li | 33.55 |
+----+-------+-------+
8 rows in set (0.00 sec)
#where条件查询
mysql> select * from exam_score where name='Li';
+----+------+-------+
| id | name | score |
+----+------+-------+
| 4 | Li | 33.55 |
+----+------+-------+
1 row in set (0.00 sec) mysql> select * from exam_score where id=3;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 3 | Sun | 44.55 |
+----+------+-------+
1 row in set (0.00 sec) mysql> select * from exam_score where name='Zhao' and score<=99.0;
+----+------+-------+
| id | name | score |
+----+------+-------+
| 1 | Zhao | 95.33 |
+----+------+-------+
1 row in set (0.00 sec)
#修改数据
#连接表所在的数据库
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A Database changed
mysql> select * from exam_score;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 1 | Zhao | 95.33 |
| 2 | Qian | 94.33 |
| 3 | Sun | 44.55 |
| 4 | Li | 33.55 |
| 5 | Zhou | 66.33 |
| 6 | Wu | 99.32 |
| 7 | Zheng | 33.20 |
| 8 | Wang | 99.30 |
+----+-------+-------+
8 rows in set (0.00 sec)
#用update修改符合条件的字段的值
#通过条件定位到行,修改行的某些字段的值
#也可以修改多个字段的值,中间用逗号隔开
mysql> update exam_score set score =62.5 where name='Zheng' and score<60.0;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
#配合replace修改符合条件的字段的值
#replace(字段,旧值,新值)
mysql> update exam_score set name=replace(name,'Wu','Xie') where id=6;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0 mysql> select * from exam_score;
+----+-------+-------+
| id | name | score |
+----+-------+-------+
| 1 | Zhao | 95.33 |
| 2 | Qian | 94.33 |
| 3 | Sun | 44.55 |
| 4 | Li | 33.55 |
| 5 | Zhou | 66.33 |
| 6 | Xie | 99.32 |
| 7 | Zheng | 62.50 |
| 8 | Wang | 99.30 |
+----+-------+-------+
8 rows in set (0.00 sec)
删除记录/数据表
#删除表
mysql> delete from exam_score where id=4;
Query OK, 1 row affected (0.00 sec)
#delete清空整张表,表还在只是空了
mysql> delete from exam_score;
Query OK, 7 rows affected (0.00 sec)
mysql> select * from exam_score;
Empty set (0.00 sec)
#drop清空表
mysql> drop table exam_score;
Query OK, 0 rows affected (0.01 sec) mysql> select * from exam_score;
ERROR 1146 (42S02): Table 'test.exam_score' doesn't exist
程序媛计划——mysql 插入、查找、修改、删除数据的更多相关文章
- 程序媛计划——mysql基本操作
本文适用于mac 在官网上下载community 版mysql,选择dmy这种.在终端中安装好mysql. #进入mysql /usr/local/mysql/bin/mysql -uroot -p ...
- 程序媛计划——mysql外键
定义 外键:如果一个表的某个字段指向另一个表的主键,就称之为外键.被指向的表,称之为主表,也叫父表,那么另一个表就是从表,也叫子表 #先新建两个表 mysql> create table aut ...
- mySQL 插入,更新和删除数据
插入数据: 语法: INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN ); 如 ...
- 程序媛计划——mysql修改表结构
#查看表的结构 mysql> desc score; +------------+--------------+------+-----+---------+----------------+ ...
- 程序媛计划——mysql索引
定义: 索引是一种单独的.物理的对数据库表中一列或多列的值进行排序的一种存储结构 #为字段创建索引 #在表中的字段中创建索引mysql> create index ind_score on ...
- 程序媛计划——mysql连接表
#inner join等值连接/内连接 mysql> select * from info; +------+-------------+----------+ | name | phone | ...
- 教你如何6秒钟往MySQL插入100万条数据!然后删库跑路!
教你如何6秒钟往MySQL插入100万条数据!然后删库跑路! 由于我用的mysql 8版本,所以增加了Timezone,然后就可以了 前提是要自己建好库和表. 数据库test, 表user, 三个字段 ...
- Mysql中查找并删除重复数据的方法
(一)单个字段 1.查找表中多余的重复记录,根据(question_title)字段来判断 代码如下 复制代码 select * from questions where question_title ...
- MySQL入门很简单: 9 插入 更新与删除数据
1. 插入数据:INSERT 1)为表的所有字段插入数据 第一种: 不指定具体的字段名 INSERT INTO 表名 VALUES(值1,值2,...,值n): 第二种:INSERT语句中列出所有字段 ...
随机推荐
- 如何进入/home/user/.wine
命令行输入 :cd /home/user/.wine/drive_c/windows/fonts /home是linux的用户目录,/user是用户名/.wine是隐藏目录,凡是以.开头的都是隐藏目录 ...
- spring开发Eclipse需要做设置
1. 统一工作空间的编码,选择UTF-8 2. 把创建JSP页面的编码修改UTF-8 3. 重新配置Tomcat服务器 * 先配置Tomcat服务器 * 选择服务器 --> open --> ...
- Tomcat:A cookie header was received[xxxxxx] that contained an invalid cookie. That cookie will be ignored.
搬运来源:https://blogs.yahoo.co.jp/dk521123/36721868.html 原因: *从Tomcat 8,Cookie的解析已经符合RFC 6265. *由于RFC 6 ...
- loadrunner12-运行报错原因及解决办法整理集合
1.错误:已超过该load generator的CPU使用率80%: 答:机器内存过小,更换配置更好的机器来执行测试. 是因为虚机的内存过小,运行Controller需要消耗的CPU过高,超过了80% ...
- Jmeter发送某个request时而成功,时而失败(处理办法:失败的时候尝试重新发送这个HTTP request)
Jmeter发送某个request时而成功,时而失败 Maybe it’s Jmeter’s problem, after all, is not a commercial software. And ...
- redmine2.6.5 邮件配置
打开configuration.xml (路径:apps/redmine/htdocs/config/) production: email_delivery: delivery_method: :s ...
- sci-hub 下载地址更新
# 2017-12-14 可用 http://www.sci-hub.tw/ 文献共享平台
- Silverlight StoryBoard 动态切换ImageSource
Silverlight StoryBoard 动态切换ImageSource <StackPanel Grid.Row="1" Orientation="Horiz ...
- XE7 里面添加自定义View
经过xe4,xe5,xe6 这么几个版本的磨合,易博龙终于在今年9月推出了统一的多平台开发版本-XE7. 经过最近几天的测试,非常不错.如果各位同学在做移动开发,强烈建议使用XE7. 前面几个版本可以 ...
- 2018.09.16 bzoj3757: 苹果树(树上莫队)
传送门 一道树上莫队. 先用跟bzoj1086一样的方法给树分块. 分完之后就可以莫队了. 但是两个询问之间如何转移呢? 感觉很难受啊. 我们定义S(u,v)" role="pre ...