您的位置:首页 > 数据库

数据库常用基本操作

2018-01-05 23:57 302 查看
启动数据库
sudo service mysql start
停止数据库
sudo service mysql stop
重启数据库
sudo service mysql restart
查看所有数据库
show databases;
创建一个数据库
create database 数据库名 charset=utf8;
查看数据库的创建语句
show create database 数据名;
使用数据库
use 数据库名;
查看数据库版本
select version();
查看当前时间
select now();
查看当前使用数据库名称
select database();
删除数据库
drop database *;全部删除
drop database 数据库名;删除指定数据库
数据库表数据类型

整数 int、bit
小数 decimal 表示浮点数,如decimal(5,2)表示共存5位,小数占2位
字符串 char、varchar
日期时间 date(年月日)time(时间)datetime(年月日时间)
枚举类型 enum 
主键primary key:物理上存储顺序
非空not null:此字段不允许写空值
默认default:默认值
自增auto_increment
创建表结构

create table students(id int unsigned auto_increment primary key not null,
    name varchar(10) not null,age int not null,high decimal(5,2),gender enum(’男’,’女’));
修改字段-添加字段birthday date
alter table students add birthday date;
修改字段-修改字段属性,不重命名birthday datetime not null
alter table students modify birthday datetime not null;
修改字段-修改字段名称,重命名birth date not null
alter table students change birthday birth date not null;
修改字段-删除字段drop
alter table students drop birthday;
删除表
drop table 表名;
查看表的创建语句
show create table 表名;
查看表字段
desc 表名;
数据的增删改查
增加

insert into 表名 values(值1,值2...);注:有几列values中就有几个值
insert into 表名(列1,列2...) values(值1,值2...);
insert into 表名 values(值1,值2...),(值1,值2...),(值1,值2...),…;
查询所有字段

select * from 表名;
查询指定字段
select 列1,列2 from 表名;
使用as给字段重命名
select 字段名 as 重命名 from 表名;
消除重复行
select distinct 列1,... from 表名;
条件查询
select * from 表名 where 条件;

where后面支持多种运算符,进行条件的处理:1、比较运算符2、逻辑运算符3、模糊查询4、范围查询5、空判断

比较运算符
大于>、小于<、等于=、大于等于>=、小于等于<=、不等于!=或<>
查询id大于3的所有学生
select * from students where id<3;
查询编号不大于4的学生
select * from students where id<=4;
查询姓名不是”黄蓉“的学生
select * from students where name!=“黄蓉”;
查询没有被删除的学生
select * from students where is_delete=0;
逻辑运算符
and、or、not
查询编号大于3的女同学
select * from students where id>3 and gender=0;
查询编号小于4或没有被删除的同学
select * from students where id<4 or is_delete=0;
模糊查询
like、%表示任意多个字符、_表示一个任意字符
查询姓黄的学生
select * from students where name like “黄%”;
查询姓黄且名只有一个字的学生
select * from students where name like “黄_”;
查询姓黄或者叫靖的学生
select * from students where name like “黄%” or name like “%靖”;
范围查询
in表示在一个非连续的范围内、between...and...表示在一个连续的范围内
查询编号1、3或者8的学生
select * from students where id in(1,3,8);
查询编号3至8的学生
select * from students where id between 3 and 8;
查询编号3至8的男生
select * from students where (id between 3 and 8) and gender=1;
空判断
注意null和’’是不同的
判空is null
查询没有填写身高的学生
select * from students where height is null;
判非空is not null
查询写了身高的学生
select * from students where height is not null;
查询填写了身高的男生
select * from students where height is not null and gender=1;
修改数据
update 表名 set 列1=值1,列2=值2,...;
删除数据
delete from 表名 where 条件;
排序 asc升序/desc降序(不写默认是asc)
select * from 表名 order by 列1 asc|desc,列2 asc|desc …;
聚合函数
count(*) 表示总行数,括号中写*与列名结果是相同的
max(列) 表示求此列的最大值
min(列) 表示求此列的最小值
sum(列) 表示求此列的和
avg(列) 表示求此列的平均值
group by 分组
将查询结果按照1个或多个字段进行分组,字段相同的为一组
可用于单个字段分组,也可用于多个字段分组,注意与distinct的区别,后者只能做去重
group by + group_concat()
group_concat(字段名)可以作为一个输出字段使用,表示分组以后,根据分组结果,使用group_concat()来放置每一组的某个字段的集合
group by + 集合函数
通过集合函数对这某一组字段的集合进行操作
group by + having
用来指定一些条件来输出查询结果,having作用和where一样,但having只能用于group by
group by + with rollup
在最后新增一行,用来记录当前列里所有记录的和
获取部分行
select * from 表名 limit start,count;
连接查询
select * from 表1 inner/left/right join 表2 on 表1.列=表2.列;
完整的select语句
select * from 表名
where … group by … having … 
order by … limit start,count;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: