本文共 7612 字,大约阅读时间需要 25 分钟。
本质:就是个放数据的仓库
核心:CRUD(最难的是“查找”)
层次模型
优点:层次清晰
缺点:(假如一张“专业”表有你,“学校”表也有你)
致命:重复就GG了
网状模型
优点:解决复杂问题,数据也完整了,重复的话也只操作一个文件
缺点:重复文件没法解决特殊性问题(假设计算机一班的人和上mysql课的有一部分人都是)
致命:并未解决导航问题
关系型
版本(看企业,贴合业务)
下载
点MySQL Community Download
点MySQL Community Server、
选MSI Installer(几百M的那个)
选5.7.29
太慢?在下载内容里复制链接到迅雷就行啦
安装
添加到环境变量(比如:C:\Program Files (x86)\MySQL\MySQL Server 5.5\bin)
在cmd下进入(u是用户名,p是密码)
# 不推荐直接在-p后输入密码mysql -u root -padmin# 建议这样mysql -u root -p>Enter Password:*****
root
用户可以为所欲为!!!
用C++开发(bin中的.exe、include中的.h可以看出来…)
典型C/S架构
# 关闭服务(大小写不敏感)net stop mysql# 开启服务net start mysql
断开连接
windows系统的情况屏幕命令:cls
在MySQL Server 的目录下创建data文件夹
mysqld --initialize-insecure --user=root
# 输入内容show databases;# 输出内容+--------------------+| Database |+--------------------+| information_schema || how2java || mybatis || mysql || performance_schema || test |+--------------------+6 rows in set (0.00 sec)
information_schema:服务器管理数据库的信息
mysql:用户信息(比如root用户)
performance_schema:存储服务器性能的东西(5.5之后才有的)
test:自带的测试样例
create database student;
tips:别用关键字,比如:database
create database if not exists `student` charset=utf8;
# 输入内容mysql>show create database student;# 输出内容+----------+--------------------------------------------------------------------+| Database | Create Database |+----------+--------------------------------------------------------------------+| student | CREATE DATABASE `student` /*!40100 DEFAULT CHARACTER SET utf8 */ |+----------+--------------------------------------------------------------------+1 row in set (0.00 sec)
drop database student;
drop database if exists 'student';
alter database student charset=gbk;
use student;
use
,才能show
show tables;
# 输入内容mysql> use school;# 输出内容Database changed# 输入内容mysql> create table student( -> id int, -> name varchar(30), -> age int -> );# 输出内容Query OK, 0 rows affected (0.01 sec)# 输入内容mysql> show tables;# 输出内容+------------------+| Tables_in_school |+------------------+| student |+------------------+1 row in set (0.00 sec)
create table if not exists teacher(id int auto_increment primary key comment '主键id',name varchar(30) not null comment '老师的名字',phone varchar(20) comment '电话号码',address varchar(100) default '暂时未知' comment '住址')engine=innodb;
tips:
id name age:字段(field)
auto_increment:自动增长(必须是primary key)
primary key:主键,唯一不重复,靠它来区分此表
comment:注释
not null:该字段不为空
default:默认值
engine=innodb:暂不解释
mysql> desc teacher;+---------+--------------+------+-----+----------+----------------+| Field | Type | Null | Key | Default | Extra |+---------+--------------+------+-----+----------+----------------+| id | int(11) | NO | PRI | NULL | auto_increment || name | varchar(30) | NO | | NULL | || phone | varchar(20) | YES | | NULL | || address | varchar(100) | YES | | 暂时未知 | |+---------+--------------+------+-----+----------+----------------+4 rows in set (0.00 sec)
drop table if exists 'stu';
drop table if exists ooo,jjj,kkk;
alter table student add phone varchar(20);
alter table student add gender varchar(2) after name;
alter table student add sex varchar(1) first;
alter table student drop phone;
change
可以改名字,也可以改类型;modify
只能改类型)alter table student change phone tel_phone int(11);alter table student modify tel_phone varchar(13);
alter table student rename to students;
insert into teacher (name,phone,address) values('王帅真','18359730121','嘉园路');
顺序没要求,但属性一定要一一对应;
如果不写第一个"()"的内容,就要按顺序了,且id处填null
insert into teacher values(NULL,'Tom',NULL,default),(NULL,'Jack',NULL,default);
delete from teacher where id=2;delete from teacher where name="Tom";delete from teacher where age>18;
delete
不会)truncate table student;
where
尽可能的唯一,防止SQL注入
update teacher set name='Jack' where id=1;
mysql> select * from teacher;+----+--------+-------------+---------+| id | name | phone | address |+----+--------+-------------+---------+| 1 | 王帅真 | 18359730121 | 嘉园路 |+----+--------+-------------+---------+1 row in set (0.00 sec)
mysql> select name from teacher where id=1;+--------+| name |+--------+| 王帅真 |+--------+1 row in set (0.00 sec)
create
alter
drop
show
insert
update
delete
select
show variables like 'character_set_%';
set character_set_client=utf8;
类型 | 大小 | 范围(有符号) | 范围(无符号) | 用途 |
---|---|---|---|---|
TINYINT | 1 byte | (-128,127) | (0,255) | 小整数值 |
SMALLINT | 2 bytes | (-32 768,32 767) | (0,65 535) | 大整数值 |
MEDIUMINT | 3 bytes | (-8 388 608,8 388 607) | (0,16 777 215) | 大整数值 |
INT或INTEGER | 4 bytes | (-2 147 483 648,2 147 483 647) | (0,4 294 967 295) | 大整数值 |
BIGINT | 8 bytes | (-9,223,372,036,854,775,808,9 223 372 036 854 775 807) | (0,18 446 744 073 709 551 615) | 极大整数值 |
FLOAT | 4 bytes | (-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) | 0,(1.175 494 351 E-38,3.402 823 466 E+38) | 单精度 浮点数值 |
DOUBLE | 8 bytes | (-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) | 0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) | 双精度 浮点数值 |
DECIMAL | 对DECIMAL(M,D) ,如果M>D,为M+2否则为D+2 | 依赖于M和D的值 | 依赖于M和D的值 | 小数值 |
create table user(id smallint unsigned auto_increment primary key comment 'id',age tinyint unsigned not null default 0,kkk int(6));
int()里的代表数据的宽度,即十进制位数
tinyint默认是3(255),smallint默认是5(65535)
create table t_1(num1 float(3,1),num2 double(5,2),)
()里的前一个是总宽度,后一个是小数部分宽度
如果我故意插入(2.99,2.777777777777)
最后会出来的是(3.0,2.78),也就是会被约
一部分空间存整数,一部分空间存小数
类型 | 大小 | 用途 |
---|---|---|
CHAR | 0-255 bytes | 定长字符串 |
VARCHAR | 0-65535 bytes | 变长字符串(多余的空间会回收,但效率偏低) |
TINYBLOB | 0-255 bytes | 不超过 255 个字符的二进制字符串 |
TINYTEXT | 0-255 bytes | 短文本字符串 |
BLOB | 0-65 535 bytes | 二进制形式的长文本数据 |
TEXT | 0-65 535 bytes | 长文本数据(比如:存博客文章) |
MEDIUMBLOB | 0-16 777 215 bytes | 二进制形式的中等长度文本数据 |
MEDIUMTEXT | 0-16 777 215 bytes | 中等长度文本数据 |
LONGBLOB | 0-4 294 967 295 bytes | 二进制形式的极大文本数据 |
LONGTEXT | 0-4 294 967 295 bytes | 极大文本数据 |
业内规定,表内都要有!!!!!!
类型 | 大小 ( bytes) | 范围 | 格式 | 用途 |
---|---|---|---|---|
DATE | 3 | 1000-01-01/9999-12-31 | YYYY-MM-DD | 日期值 |
TIME | 3 | ‘-838:59:59’/‘838:59:59’ | HH:MM:SS | 时间值或持续时间 |
YEAR | 1 | 1901/2155 | YYYY | 年份值 |
DATETIME | 8 | 1000-01-01 00:00:00/9999-12-31 23:59:59 | YYYY-MM-DD HH:MM:SS | 混合日期和时间值 |
TIMESTAMP | 4 | 1970-01-01 00:00:00/2038结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07 | YYYYMMDD HHMMSS | 混合日期和时间值,时间戳 |
create table t_5(sex enum('man','woman','?','none','it'));
枚举类型有的才能取
第一个类型是1(‘man’)
第二个类型是2(‘woman’)
…
以此类推
实际存的就是整数数据
优点:
速度快
节省空间,限制数据(2byte)
create table t_6(hobby set('哲学','经济学','IT','人文社科'));
插入的话只能插入一个字段
insert into t_6 values('IT,经济学');
哲学——2^0
经济学——2^1
IT——2^2
人文社科——2^3
比如:我插入1,就是'哲学'
;我插入5,就是'哲学','IT'
总之就是“位操作”
转载地址:http://cobv.baihongyu.com/