mysql数据库
Posted a199706
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql数据库相关的知识,希望对你有一定的参考价值。
一、数据库
查询所有数据库
show databases;
查询单个数据库:
show create database emp;
修改数据库:
alter database emp1 character set utf8;
修改emp1的字符集为utf8;
删除数据库:
drop database emp1;
二、表操作
show tables;
查询所有表;
创建表:
使用之前先use mydb01;
create table emp(
id int;
name varchar(20),
age int;
time data;
price double 浮点型不需要加‘’
);
查询表结构:
desc emp1;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid | int(11) | YES | | NULL | |
| sname | varchar(20) | YES | | NULL | |
| sage | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
删除表:
drop table emp1;
修改表:
1、添加字段
alter table emp1 add column age int;
在emp1表中添加age值为int的一列;
2、修改字段类型
alter table emp1 modify column age int;
修改age的类型为int;
3、修改字段名称
alter table emp1 change age column age1 varchar(10);
修改字段名称为age1
4、删除字段
alter table emp1 drop column age1;
三、增删改数据
1、增加
insert intostudent values(5,‘帅气‘,‘2017-08-09‘,‘女‘);
插入整行
INSERT INTO student (pid,aname) VALUES (2,‘帅气‘);
插入部分pid和aname值
二、修改
UPDATE student SET qdate=‘1997-05-14‘,aname=‘帅气的我‘ WHERE pid=2;
修改第二行qdate和aname的值。
三、删除
delete from student where pid=2;
删除第二行
delete from student;
删除student表
TRUNCATE TABLE student;
清空表,并且清空约束
四、查询
select * from student;
四、查询数据 12种
1、查询所有的行
select * from student;
查询指定的列
select pid,aneme * from student;
查询pid和aname列
2、查询的时候添加常量
select pid as ‘梦想’,aname as ‘种种’ *from student;
查询时将pid变为梦想,aname变为种种
3、查询时合并量
SELECT aname,(html+js+jquery) AS ‘grade‘ FROM score;
4、查询时去掉重复列
select distinct sex from student;
5、条件查询
逻辑条件与或
and与,左右全部满足;or或左右只满足一个即可
select * from student where sex=‘女’ and aname=‘帅气的我’;
因为需要满足两边,所以选不到名称为男的帅气的我
非:or
select * from student where sex=‘女’ or aname=‘美丽’;
因为只满足其一,所以美丽为男也被入选
判断条件
><<>>=<= between and等同>=,<=
select * from score where js<>20;
select * from score where jq between 30 and 80;
select * from score where jq >=30 and jq<=80;
SELECT * FROM score WHERE html<60;
判断为空:
select * from score where sex is null;
不为空
SELECT * FROM student WHERE sex IS NOT NULL;
查询性别为空的字符串
select * from student where sex=‘’;
判断性别不为空字符串的数据
select * from student where sex <>‘’ and sex is not null;
首先判断sex不为空值,且不为空字符串
模糊查询
% 任意个
_一个
select * from student where aname like‘张%‘;
姓张的同学
select * from student where aname like‘张_’;
姓张并两位名
SELECT *FROM student WHERE aname LIKE(‘%丽%‘);
SELECT *FROM student WHERE aname LIKE(‘梦%丽%‘);
-- 求js的总成绩 html+js+jq
SELECT SUM(js+jquery+html) FROM score;
-- 求js的平均成绩
SELECT AVG(js) FROM score;
-- 求html的最大成绩
SELECT MAX(html) FROM score;
-- 求html中的最小成绩
SELECT MIN(html) FROM score;
-- 查询本班有多少学生
SELECT COUNT(pid) FROM student;
-- 分页查询 x,x 起始行=(当前页-1),每页显示的条数
SELECT * FROM student LIMIT 0,2;
SELECT * FROM student LIMIT 2,2;
SELECT * FROM student LIMIT 4,2;
-- js成绩从大到小排序 ↓
SELECT * FROM score ORDER BY js DESC;
-- jq成绩从小到大排序 ↑
SELECT * FROM score ORDER BY jquery ASC;
-- 按照js的成绩从小到大,jq成绩从大到小,谁前面谁为主,如果js成绩一样,则jq按照jq为主。
SELECT *FROM score ORDER BY js ASC ,jquery DESC;
分组查询
-- 按照性别分组
SELECT sex FROM student GROUP BY sex;
-- 按照分组来统计人数,和聚合函数count(*)一起
SELECT sex,COUNT(*) FROM student GROUP BY sex;
SELECT class, COUNT(*) FROM score GROUP BY class;
-- 统计每个班的js总分
SELECT class,SUM(js) FROM score GROUP BY class;
以上是关于mysql数据库的主要内容,如果未能解决你的问题,请参考以下文章