MENU

sql笔记补充整理

• April 13, 2019 • Read: 1114 • Web Program

MySQl

数据库种类

1.关系型数据库

mysql oracle sqlserver...

2.非关系型数据库

nosql redis mongoDB......读取速度快

sql语句

存值setValue("memeda",[])

取值get("memeda")

CMD进入数据库

mysql -uroot -proot//登录到本地数据库
命令作用
show databases;查看所有库数据库
use name;打开指定的数据库
select database();查看自己在哪个库
show tables;查看数据库所有表
create database xiaohuwei charset utf8;创建一个数据库并=指定编码
drop database xiaohuwei;删掉一个数据库
show create database xiaohuwei;查看一个数据库的编码
create table xiaohuwei(字段名 数据类型 约束);创建一个表

层级关系

mysql

数据库

数据表

存数据

数据类型

名称表达式
整数int tinyint(255) smallint bigint
字符型定长存储char(0-255) 变长存储 varchar(长度0-65535) text
枚举类型enum('男','女')
小数float(m,n)m 位数 n小数位
 创建一张表
create table student(
    id tinyint ,
    name char(10) ,
    sex enum('男','女') ,
    age tinyint ,
    tel char(11)
    );  

数据库操作

表达式作用
drop table student;删除数据表
set names gbk;处理中文数据之前要临时转换数据库表的格式
select * from xiaohuwei;查看表的数据
insert into xiaohuwei(id,name) values(1,"XXX");向数据表插入数据
insert student values(1,'么么哒');简化
insert table(字段) values(值);指定字段加值
unsigned正整数约束
unique唯一性约束/唯一索引
not null非空约束
primary key主键(每张表都存在有且只有一个)
auto_increment自增/只对主键有效

条件查询

查询id为 1.2.3.4.5.6 的数据

select *from xiaohuwei where id in(1,2,3,4,5,6);//in 表示取值

查询id范围为 88 到 122 的数据

select *from xiaohuwei where id>=88 and id<=122;//逻辑运算符 效率不高
select *from xiaohuwei where id between 88 and 122;//between表示范围语句 

查询除了id为 1.2.3.4.5.6 的数据

select *from xiaohuwei where id not in(1,2,3,4,5,6);//in 表示取值

排序查询

select *from xiaohuwei order by id desc;// 表示降序查询 针对id
select *from xiaohuwei order by id asc;// 表示升序查询 针对id

limit拿数据(分页查询)

select *from xiaohuwei limit  0,10;//一定拿10条 前面是索引 后面是数量

案例

select *from xiaohuwei where id<100 order by id desc limit 10;
//找到针对id的前一百条数据降序排列取前面10条

聚合函数

函数作用
avg()平均数
sum()
count()总数
min() / max()最小/最大
group by分组

语法

select count(*)as sum from xiaohuwei;//查询这张表的总数据个数并用别名sum打印(给字段别名)
select count(*)sum from xiaohuwei;//简化
select sum(price) from xiaohuwei;//在表xiaohuwei中对price字段求和
Archives QR Code
QR Code for this page
Tipping QR Code
Leave a Comment