数据库的基本操作增删查改

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wyn126/article/details/82772394

1、启动数据库

service mysqld start

2、连接数据库

mysql -u root -p

3、创建数据库

create database 库名;

4、使用数据库

use database;

5、查看数据库

show databases;

6、删除数据库

drop database 数据库名;

7、增加

insert into table_name[(column[,column...])]    values (value [,value...]); 
例子:
mysql> insert into goods values(101, '披萨', 27.5);  
插入的数据应与字段的数据类型相同。比如,将‘abc'插入到id列就不行:

当主键存在冲突的时候(duplicate key),可以选择性的进行处理
7.1更新操作

insert into 表名(字段列表) values(值列表) on duplicate key update 字段=新值;  
例子:
 insert into goods values(101, 'ccc', 20.5) on duplicate key update goods_name='ccc',  price=20.5;  

7.22.替换:主键如果没有冲突,就直接插入

replace into 表名(包含字段) values(值列表); 
例子
mysql> replace into goods values(100, 'huawei', 999);  Query OK, 2 rows affected (0.01 sec) 

8、修改
语法

update tbl_name set col_name1=expr1, [, col_name2=expr2 ...] [where conditon] 
例子:
1.mysql> update goods set price=300; ‐‐ 没有条件,整表修改     //将所有产品的价格修改为300元
2、update goods set price=1000 where id=100;       //将id为100的产品价格修改为1000
3、update goods set price=price+200 where id=101;  //将id为101的产品价格增加200 

update使用细节:
update 语法可以用心值更新原有表中的各列
set子句指示要修改哪些列和要给予哪些值
where子句指定应更新哪些行。如果没有where子句,则更新所有行
如果需要更新多个字段,可以通过 set 字段1=值1,字段2=值2…
9、删除
语法:

1、delete from  tbl_name [where condition] 
例子:
mysql> delete from goods where id=101;  //删除表中id为101的数据
mysql> create table goods2 like goods;  //复制表的结构
2、删除表中所有记录
mysql> delete from goods; ‐‐删除整个表的数据,但是表的结构还存在 
3、mysql> truncate table goods; ‐‐这个指令也把整个表记录删除 

上述两种删除整表的区别:
效果一样,truncate速度快,delete可以带where条件,删除更加灵活
delete可以返回被删除的记录数,而truncate返回0,推荐使用delete
delete使用细节: 如果不适用where子句,将删除整个表中所有数据 delete语句不能删除某一列的值(可以用update置null) 使用delete语句仅删除记录,不删除表本(drop table)
10、查找selsct

1、可以指定查询哪些列,比如:查询id,姓名,数学成绩
mysql> select id, name, math from student;  
2、*号表示查询所有列( 星号效率很低,用哪些字段就取哪些字段 )
mysql> select * from student; 
3、distinct 如果结果中有完全相同的行,就去除重复行
mysql> select distinct math from student;  
4、select column as 别名 from 表; //a表示起别名
mysql> select name, chinese+math+english+10 as total from student;
5.将所有行唐的学生成绩增加60%(查询总分再增加60%)
mysql> select name, (chinese+math+english)*1.6 as total from student where name like '唐%'; ‐‐ 增 加60%就是 乘以 1.6, like 是模糊查询 



**select的where子句,使用where进行查询过滤**
mysql> select * from student where name like '李%';  //查询姓李学生的成绩
mysql> select * from student where english > 90; //查询英语成绩大于90的同学
mysql> select id, name, math+english+chinese as 'total' from student where math+english+chinese  >200;  //查询总分大于200的所有同学
mysql> select * from student where name like '李%' and id > 10;  //查询姓李并且成绩大于100的学生
mysql> select * from student where english > chinese;  //查询英语成绩大于语文成绩的学生
mysql> select * from student where (math+english+chinese) > 200 and math < chinese and name like  '唐%';    //查询总分大于200分并且数学成绩小于语文成绩的姓唐的学生
mysql> select * from student where english>=80 and english<=90; ‐‐方法1  mysql> select * from student where english between 80 and 90; ‐‐ 方法2 between是闭区间 
//以上两种方法表示查询英语分数在80-90之间的同学
mysql>  select * from student where math in(89,90,91);//查询数学成绩为89,90,91的同学


**select的order by字句**
select column1,column2,... from table order by column  asc|desc,...; 
order by 指定排序的列,排序的列可以使表中的列名,也可以是select语句后指定的别名
 asc升序(默认),desc降序 
 order by 子句应该位于select语句的结尾
mysql> select * from student order by math;‐‐没有指定升序还是降序,默认是升序 
mysql> select id, name, math+english+chinese as total from student order by total desc; //总分排序后,从高到低输出
mysql> select id, name, math+english+chinese as total from student where name like '李%' order by  total;  //对姓李的同学按成绩从低到高排序


**count:返回某一列、行的总数**
select count(*)|count(列名) from tbl_name where condition 
mysql> select count(*) from student; //统计一个班总共有多少个学生
mysql> select count(*) from student where math>=90; //统计数学成绩大于90的学生人数
mysql> select count(*) from student where math+english+chinese>250;  //统计总成绩大于250的学生人数


**sum:sum函数返回满足where条件的行的和**
mysql> select sum(math) from student;//统计一个班级数学总成绩
mysql> select sum(chinese), sum(english), sum(math) from student;  //统计一个班语文英语数学各科的总成绩
mysql> select sum(chinese+english+math) from student;  //统计一个班语文英语数学成绩总和
mysql> select sum(chinese)/count(name) from student;  //统计一个班语文成绩平均分



**avg:agv函数返回满足where条件的一列的平均值**
mysql> select avg(math) from student;  //求一个班数学平均分
mysql> select avg(math+chinese+english) from student;  //求一个班总平均分



**max/min函数返回满足where条件的一列的大/小值**
mysql> select max(chinese+english+math), min(chinese+english+math) from student;  //求一个班的最高分和最低分




**group by 子句对列进行分组**
select column1, column2, .. from table group by column;
为了讲清楚分组,创建一个雇员信息表(来自oracle 9i的经典测试表)
1. EMP员工表 2. DEPT部门表 3. SALGRADE工资等级表
mysql> select deptno,avg(sal),max(sal) from EMP group by deptno;  //显示各部门的平均工资个最高工资
mysql> select avg(sal),min(sal),job, deptno from EMP group by deptno, job;  //显示每个部门的每种岗位的平均工资和低工资

猜你喜欢

转载自blog.csdn.net/wyn126/article/details/82772394