码迷,mamicode.com
首页 > 数据库 > 详细

MYSQL数据库常用语句

时间:2018-12-18 00:00:26      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:databases   student   连接数   tag   math   tab   school   rip   tables   

1.查看数据库服务器已有的数据库

show databases;
2.查看数据库(school)定义的信息

show create database school;
3.连接数据库

use school;
4.查看当前连接的数据库

select database();
5.数据库创建

create database school;
6.数据库修改

alter database school character set utf8;
7.数据库删除

drop database school;
8.查看当前库已有表

show tables;
9.创建表 

create table student(
id int,
name varchar(20),
age int
);

 


10.查看表定义信息

show create table student;
11.数据库的增、删、改(这里我是在图形化软件dataGrip里添加的数据)

/*增*/
insert into student(`num`,`name`,`age`,`class`,`math`) values (3,"赵子龙",20,3,100);
/*删*/
delete from student where name = "张三";
/*改*/
update student set age = 100 , num = 6 where name = "赵子龙";
12.数据库的查询(比较多)

/*查询表所有数据*/
select * from student;
/*查询表带条件数据*/
select * from student where age = 100;
/*查询表某个字段数据*/
select age,name from student;
/*查询表数据总数*/
select count(1) from student;
/*查询表数据带条件数目*/
select count(1) from student where age = 20 and class = 2; /*且*/
select count(1) from student where age = 20 or class = 2; /*或*/
/*查询表数据所有人数带条件(age)总和*/
select sum(age) from student;
/*查询表数据带条件(age)的平均数*/
select avg(age) from student;
/*查询表数据带条件(age)的平均数并且改名(as)为age_avg*/
select avg(age) as age_avg from student;
/*查询表数据按条件(class)进行分组求总数*/
select count(1) from student from group by class;
13.数据库分页查询

/*按10个一页查询第二页数据*/
select * from student limit 10,2;
/*按正序排*/
select * from student order by id;
/*按倒序排*/
select * from student order by id desc;
 
---------------------

MYSQL数据库常用语句

标签:databases   student   连接数   tag   math   tab   school   rip   tables   

原文地址:https://www.cnblogs.com/Anderson-An/p/10134383.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!