程序媛计划——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语句中列出所有字段 ...
随机推荐
- maven打包部署到私服
转载地址:http://blog.csdn.net/stormragewang/article/details/43407471 心得 apache的开源maven插件对我们使用maven进行打包,发 ...
- 清除Pycharm设置的方法
Mac 按需运行下面的 rm 删除命令 # Configuration rm -rf ~/Library/Preferences/PyCharm* # Caches rm -rf ~/Library/ ...
- dir/
dos窗口输入dir命令是显示磁盘目录命令: addslashes()使用反斜线转义字符串: exec($command,$output,$return)执行一个外部程序 $command:要执行的命 ...
- How to use jQuery countdown plugin
Install We provide two installation methods: Bower bower install jquery.countdown Manual Download th ...
- JAVA Get UUID
UUID是通用唯一标识码(Universally Unique Identifier),通过开源软件基金会(OSF)设立的一种算法生成.它的主要作用就是保证生成的字符串在同一时空中所有机器上都是唯一的 ...
- Angular 通过注入 $location 获取与修改当前页面URL
//1.获取当前完整的url路径 var absurl = $location.absUrl(); //http://172.16.0.88:8100/#/homePage?id=10&a=1 ...
- Libevent学习之SocketPair实现
Libevent设计的精化之一在于把Timer事件.Signal事件和IO事件统一集成在一个Reactor中,以统一的方式去处理这三种不同的事件,更确切的说是把Timer事件和Signal事件融合到了 ...
- VBA替换函数
Sub test() On Error Resume Next Dim arr1, arr2, i, j arr1 = Range("T1:EI3") arr2 = Range(& ...
- python按行读取并替换
fp = open(''test2.txt','w') #打开你要写得文件test2.txt lines = open('test1.txt').readlines() #打开文件,读入每一行 f ...
- MySQL】存储过程、游标、循环简单实例
create procedure my_procedure() -- 创建存储过程 begin -- 开始存储过程 declare my_id varchar(32); -- 自定义变量1 decla ...