数据库php
数据库软件java
数据仓库python
sudo /etc/init.d/mysql start|stop|restart|status sudo service mysql start|stop|restart|status
mysql -hIP地址 -u用户名 -p密码 本地链接可省略 -h 选项
库管理mysql
一、查看已有库; show databases; 二、建立库并指定字符集; create database 库名 charset utf-8; create database 库名 character set utf-8; 三、查看当前所在库; select database(); 四、切换库; use 库名; 五、查看库中已有表; show tables; 六、删除库; drop database 库名;
表管理面试
一、建立表并指定字符集; create table 表名(字段名,字段类型,其余) charset = utf-8; 二、查看建立表的语句 (字符集、存储引擎); show create table 表名; 三、查看表结构; desc 表名; 四、删除表; drop table 表名,表名2;
表记录管理sql
一、增 : insert into 表名(字段名) value(),(); 二、删 : delete from 表名 where 条件; 三、改 : update 表名 set 字段名=值 where 条件; 四、查 : select 字段名 from 表名 where 条件;
表字段管理(alter table 表名)数据库
一、增 : alter table 表名 add 字段类型 first|after 字段名; 二、删 : alter table 表名 drop 字段名; 三、改 : alter table 表名 modify 字段名 字段类型; 四、表重命名: alter table 表名 rename 新表名;
四大数据类型编程
int [4字节] smallint[2字节] bigint[8个字节] tinyint[1个字节]
char() 定长: char(4) 存3个字符 abc; 'abc '【长度不足,填充空格】 注意:select 取值时mysql将空格去掉! 'ddd '---预期显示'ddd ',实际显示---'ddd' varchar(4) 多出一个字节,专门存储当前这个字段实际存储长度
enum set
date datetime timestamp time year
日期时间函数iphone
NOW()当前时间 CURDATE() YEAR(字段名) DATE(字段名) TIME(字段名)
日期时间运算编程语言
select * from 表名 where 字段名 运算符(NOW()-interval 间隔); 间隔单位: 1 day | 3 month | 2 year eg1:查询1年之前的用户充值信息 select * from user where time < (NOW()-interval 1 year);
> >= < <= = != students表名 score 字段名 eg1 : 查询成绩不及格的学生 select * from students where score < 60; eg2 : 删除成绩不及格的学生 delete from students where score < 60; eg3 : 把id为3的学生的姓名改成 周芷若 update students set name = '周芷诺' where id = 3;
and or eg1 : 查询成绩不及格的男生 select * from students where score < 60 and gender = 'male'; eg2 : 查询成绩在60-70之间的学生 select * from students where score >= 60 and score <= 70;
between 值1 and 值2 、in() 、not in() eg1 : 查询不及格的学生姓名及成绩 select * name,score from students where score between 0 and 59; eg2 : 查询AID19和AID18班的学生姓名及成绩 select name,score from students where class in ('AID19','AID18');
where 字段名 like 表达式(%_) eg1 : 查询北京的姓赵的学生信息 select * from students where name like '赵%';
is NULL 、is not NULL eg1 : 查询姓名字段值为NULL的学生信息 select * from students where name is NULL;
给查询的结果进行排序(永远放在SQL命令的倒数第二的位置写)
order by 字段名 ASC/DESC eg1 : 查询成绩从高到低排列 select * from students order by score DESC;
限制显示查询记录的条数(永远放在SQL命令的最后写)
limit n :显示前n条 limit m,n :从第(m+1)条记录开始,显示n条 分页:每页显示10条,显示第6页的内容 limit (6-1)*10,10; 每页显示a条,显示第b页的内容 limit (b-1)*a,a;
建立库 :country(指定字符编码为utf8)
建立表 :sanguo 字段:id 、name、attack、defense、gender、country
要求 :id设置为主键,并设置自增加属性
id int primary key auto_increment,
插入5条表记录(id 1-5,name-诸葛亮、司马懿、貂蝉、张飞、赵云),攻击>100,防护<100)
查找全部蜀国人的信息
select * from sanguo where country='蜀国';
将赵云的攻击力设置为360,防护力设置为68
update sanguo set attack = 360,defense=68 where name='赵云';
将吴国英雄中攻击值为110的英雄的攻击值改成100,防护力改成60
update sanguo set attack =100,defense=60 where attack=110 and country='吴国';
找出攻击值高于200的蜀国英雄的名字、攻击力
select name,attack from sanguo where attack>200 and country='蜀国';
将蜀国英雄按攻击值从高到低排序
select * from sanguo where country='蜀国' order by attack DESC;
魏蜀两国英雄中名字为三个字的按防护值升序排列
select * from sanguo where country in('蜀国','魏国') and name like '___' order by defense ASC;
在蜀国英雄中,查找攻击值前3名且名字不为 NULL 的英雄的姓名、攻击值和国家
select name,attack,country from sanguo where country='蜀国' and name is not NULL or order by attack DESC limit 3;
三、select ...聚合函数 from 表名 一、where ... 二、group by ... 四、having ... 五、order by ... 六、limit ...; 左边是执行顺序,右边是写的顺序
方法 | 功能 |
---|---|
avg(字段名) | 该字段的平均值 |
max(字段名) | 该字段的最大值 |
min(字段名) | 该字段的最小值 |
sum(字段名) | 该字段全部记录的和 |
count(字段名) | 统计该字段记录的个数 |
eg1 : 找出表中的最大攻击力的值?
select max(attack) from sanguo;
eg2 : 表中共有多少个英雄?
select count(name) from sanguo;
eg3 : 蜀国英雄中攻击值大于200的英雄的数量
select count(id) from sanguo where country='蜀国' and attack >200;
给查询的结果进行分组
eg1 : 计算每一个国家的平均攻击力
select country,avg(attack) from sanguo group by country;
eg2 : 全部国家的男英雄中 英雄数量最多的前2名的 国家名称及英雄数量
select country,count(id) as number from sanguo where gender='m' group by country order by number DESC limit 2;
注意:
group by后字段名必需要为select后的字段一致
查询字段和group by后字段不一致,则必须对该字段进行聚合处理(聚合函数)
对分组聚合后的结果进行进一步筛选
eg1 : 找出平均攻击力大于105"having avg (attack) >105"的国家的前2名,显示国家名称和平均攻击力 select country,avg(attack) from sanguo group by country having avg(attack)>105 order by avg(attack) DESC limit 2;
注意
having语句一般与group by联合使用 having语句存在弥补了where关键字不能与聚合函数联合使用的不足,where只能操做表中实际存在的字段,having操做的是聚合函数生成的显示列
不显示字段重复值
eg1 : 表中都有哪些国家 select distinct country from sanguo; eg2 : 计算一共有多少个国家 select count(distinct country) from sanguo;
注意
distinct和from之间全部字段都相同才会去重 distinct不能对任何字段作聚合处理
运算符 : + - * / % **
eg1: 查询时显示攻击力翻倍 select name,attack*2 from sanguo; eg2: 更新蜀国全部英雄攻击力 * 2 update sanguo set attack=attack*2 where country='蜀国';
对数据库表的一列或多列的值进行排序的一种结构B+树(Btree方式)
传统B树的特色:
1,每一个节点能存储多个索引且包含数据,因为该特性,促使树的高度比二叉树矮,从而下降了磁盘的IO查找。
2,可是因为每一个节点存储了数据。。。
B+树
1,节点内只存储索引,不存储数据,从而单个节点能存储的索引数量远远大于B树
2,数据均存储在叶子节点中,而且有序的相连【范围查询效果棒!】
加快数据检索速度
占用物理存储空间(/var/lib/mysql) 当对表中数据更新时,索引须要动态维护,下降数据维护速度
# cursor.executemany(SQL,[data1,data2,data3]) # 以此IO执行多条表记录操做,效率高,节省资源 一、开启运行时间检测 mysql>show variables like '%pro%'; mysql>set profiling=1; 二、执行查询语句(无索引) select name from students where name='Tom99999'; 三、查看执行时间 show profiles; 四、在name字段建立索引 create index name on students(name); 五、再执行查询语句 select name from students where name='Tom88888'; 六、查看执行时间 show profiles;
一、可设置多个字段 二、普通索引 :字段值无约束,KEY标志为 MUL 三、惟一索引(unique) :字段值不容许重复,但可为 NULL KEY标志为 UNI 四、哪些字段建立索引:常常用来查询的字段、where条件判断字段、order by排序字段
建立表时
create table 表名( 字段名 数据类型, 字段名 数据类型, index(字段名), index(字段名), unique(字段名) );
已有表中建立
create [unique] index 索引名 on 表名(字段名);
一、desc 表名; --> KEY标志为:MUL 、UNI 二、show index from 表名\G;
drop index 索引名 on 表名;
一、只能有一个主键字段 二、所带约束 :不容许重复,且不能为NULL 三、KEY标志(primary) :PRI 四、一般设置记录编号字段id,能惟一锁定一条记录
建立表添加主键
create table student( id int auto_increment, name varchar(20), primary key(id) )charset=utf8,auto_increment=10000;##设置自增加起始值
已有表添加主键
alter table 表名 add primary key(id);
已有表操做自增加属性
一、已有表添加自增加属性 alter table 表名 modify id int auto_increment; 二、已有表从新指定起始值: alter table 表名 auto_increment=20000;
一、删除自增加属性(modify) alter table 表名 modify id int; 二、删除主键索引 alter table 表名 drop primary key;
有一张文章评论表comment以下
comment_id | article_id | user_id | date |
---|---|---|---|
1 | 10000 | 10000 | 2018-01-30 09:00:00 |
2 | 10001 | 10001 | ... ... |
3 | 10002 | 10000 | ... ... |
4 | 10003 | 10015 | ... ... |
5 | 10004 | 10006 | ... ... |
6 | 10025 | 10006 | ... ... |
7 | 10009 | 10000 | ... ... |
以上是一个应用的comment表格的一部分,请使用SQL语句找出在本站发表的全部评论数量最多的10位用户及评论数,并按评论数从高到低排序
备注:comment_id为评论id
article_id为被评论文章的id
user_id 指用户id
综述:两张表,一张顾客信息表customers,一张订单表orders
表1:顾客信息表,完成后插入3条表记录
c_id 类型为整型,设置为主键,并设置为自增加属性 c_name 字符类型,变长,宽度为20 c_age 微小整型,取值范围为0~255(无符号) c_sex 枚举类型,要求只能在('M','F')中选择一个值 c_city 字符类型,变长,宽度为20 c_salary 浮点类型,要求整数部分最大为10位,小数部分为2位
表2:顾客订单表(在表中插入5条记录)
o_id 整型 o_name 字符类型,变长,宽度为30 o_price 浮点类型,整数最大为10位,小数部分为2位 设置此表中的o_id字段为customers表中c_id字段的外键,更新删除同步 insert into orders values(1,"iphone",5288),(1,"ipad",3299),(3,"mate9",3688),(2,"iwatch",2222),(2,"r11",4400);
增删改查题
一、返回customers表中,工资大于4000元,或者年龄小于29岁,知足这样条件的前2条记录 二、把customers表中,年龄大于等于25岁,而且地址是北京或者上海,这样的人的工资上调15% 三、把customers表中,城市为北京的顾客,按照工资降序排列,而且只返回结果中的第一条记录 四、选择工资c_salary最少的顾客的信息 五、找到工资大于5000的顾客都买过哪些产品的记录明细 六、删除外键限制 七、删除customers主键限制 八、增长customers主键限制c_id