MySQL
- MySQL
- 访问数据库:
-
mysql -u root -p
-
- 程序运行流程:
Socket 客户端:
2. 根据IP和端口进行连接
3. 发送指令(send)
XXXXXX
7. 接受结果
Socket 服务端:
1. 运行起来
4. 接受指令(receive)
5. 解析命令
文件夹操作;
文件操作;
文件内容操作;
6. 返回结果 - 数据库管理软件:
-客户端:
Java
Python
Mysql自带
PS:发送指令命令:SQL语句
-服务端:
-mysqld-SQL语句:
文件夹操作(数据库操作)
创建:
create database db1;
create database db1 default charset utf-8;
删除:
drop database db1;
进入文件夹:
use db1; -
文件操作(数据表操作)
创建:
单表:
create table userinfo(
id int not null atuo_increment primary key,
name char(20),
age int default 18,
gender char(1)
)engine=innodb defatult charset utf8
操作表:
insert into userinfo(name,age,gender) values('alex',19,'man')
多表,两个表相关联,一对多:
create table userinfo(
id int not null atuo_increment primary key,
name char(20),
age int default 18,
gender char(1),
department_id int,
constraint XXXX foreign key(department_id) reference department(id)
)engine=innodb defatult charset utf8
create table department(
id int not null atuo_increment primary key,
title char(32)
)
多表,多对多:
create table gril(
id int not null auto_increment primary key,
name char(32)
)engine=innodb defatult charset utf8
create table boy(
id int not null auto_increment primary key,
name char(32)
)engine=innodb defatult charset utf8
create table b2g(
id int not null auto_increment primary key,
bid int,
gid int,
constraint fk1 foreign key(bid) reference boy(id),
constraint fk2 foreign key(gid) reference girl(id)
) engine=innodb defatult charset utf8
1. 列名(id name age gender)
2. 数据类型(int char)
数字:
整数:tinyint smaillint int bigint
小数:
float 不精准
double 双精准,比float 精准一些
decimal 精准,内部存储用字符串存储的,适用于财务数据
字符串:
char(20) : 定长,无论存储多少,空间都是20个字符串,当存储不够20个字符时,用空格填满。 长度最长 255
缺点:浪费空间
优点:性能比较高
varchar(20):不定长,最大长度20个字符。长度最长255
优点:节省空间
缺点:查找费时,麻烦,性能比较低
text mediumtext longtext
# 如果存储大文件夹的话,一般操作就是,文件存储在硬盘,然后数据库存储路径。
时间:
DATE:YYYY-MM-DD
TIME:HH:MM:SS
YEAR:YYYY
DATETIME:YYYY-MM-DD HH:MM:SS ****** 最常用
TIMESTAMP: YYYYMMMDD HHMMSS - 连表:
- left/right/inner join on #推荐大家用left join。只记一个left join 即可。 inner join 会把两个表查询结果,空值的删除掉
- select student.sid, student.sname,class.caption from student left join class on student.class_id = class.cid
- where 、in、Limit、Like
- Group by、 Order by 、Group having
- union 上下拼接 。上下拼接需要保证筛选的两张表列数相同
- union 会去重。
- union all 则不会去重
- left/right/inner join on #推荐大家用left join。只记一个left join 即可。 inner join 会把两个表查询结果,空值的删除掉
- 分组
- Group by XXX
- XXX下相同的会合并成一条。count(),max(),min(),sum(),avg() 聚合函数
- select class_id, count(sid) as '人数' from student Group by class_id
- Group by 后面不能加 where ,而应该加:having。如果非要加 where,只能在 group by 前面加
- select class_id, count(sid) as '人数' from student Group by class_id having count(sid) >=2
- XXX下相同的会合并成一条。count(),max(),min(),sum(),avg() 聚合函数
- Order by
- Group having
- Group by XXX
- 访问数据库:
- Memcached
- Reids