【MySQL】数据库基础_frank_fuckppt

数据库的本质:放数据的仓库

数据存放

萌芽阶段

  • 放在内存里——瞬时:程序结束,上次的数据就没啦
  • 放在文件里——持久:进步了!能一直保存了(比如放在.txt文件里),但还是存在问题
    • 不安全
    • 不方便在中间插入、删除数据
    • 但也不是一无是处,可以应用于配置文件:.xml文件、.ini文件等等

数据库的产生

核心:CRUD(最难的是“查找”)

层次模型

  • 国家-地区-学校-专业-班级,层次分明
  • 优点:层次清晰 缺点:(假如一张“专业”表有你,“学校”表也有你)
    • 查询效率低下(不知道查哪张)- 数据不完整(这张表改了那张表没改)
      致命:重复就GG了

网状模型

  • 优点:解决复杂问题,数据也完整了,重复的话也只操作一个文件
  • 缺点:重复文件没法解决特殊性问题(假设计算机一班的人和上mysql课的有一部分人都是)
  • 计算机一班的每人收100元- 上mysql课的每人收233元
    致命:并未解决导航问题

关系型

  • 解决了导航问题:比如每张表都有个学号(公共的编号)- 分批管理,各管各的- 数据完整,层次清晰- 说白了,关系就是共性,为了让每张表都能找到爹
  • 和谁都有关系,但是又互不影响

关于MySQL数据库

安装

版本(看企业,贴合业务)

在cmd下进入(u是用户名,p是密码)

# 不推荐直接在-p后输入密码
	mysql -u root -padmin

# 建议这样
	mysql -u root -p
	Enter Password:***

root用户可以为所欲为!!!

MySQL是个啥

  • 用C++开发(bin中的.exe、include中的.h可以看出来…)
  • 典型C/S架构
    • server服务端:MySQL Sever mysql服务端(在本地,客户端要访问,需要开启服务)- client客户端:通过终端cmd去访问/操作数据库

关闭服务

# 关闭服务(大小写不敏感)
net stop mysql
# 开启服务
net start mysql

断开连接

quit- exit- \q- 直接关闭CMD
windows系统的情况屏幕命令:`cls`

在MySQL Server 的目录下创建data文件夹

mysqld --initialize-insecure --user=root

操作数据库

库的基本操作

显示所有仓库

# 输入内容
show databases;
# 输出内容
+----------------------+
| Database         |
+----------------------+
| information_schema  |
| 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)
  • 默认latin1- win10下学习用gbk,实际开发用utf8- 修改字符集
drop database if exists 'student';  -- 删库
alter database student charset=gbk;  -- 字符集设置
use student; -- 使用数据库

表的基本操作

  • 本质:将仓库里的东西归类

查看表

  • 首先要use用来指定说我就要你这个库了,指定仓库发货!才能show
show tables;

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)

创建表

【规范创建表】
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 keyprimary key:主键,唯一不重复,靠它来区分此表
    comment:注释
    not null:该字段不为空
    default:默认值
    engine=innodb:使用的是innodb引擎

删表

 # 标准做法
drop table if exists 'stu';
 # 多张表可以逗号隔开
drop table if exists ooo,jjj,kkk;

修改表

给你一堆属性名字,然后你可以为这些属性添加,或者修改类型,或者删除,抑或是直接把人家的表名字修改了。

# 指定位置添加字段
alter table student add phone varchar(20);
alter table student add phone varchar(2) after name;
alter table student add gender 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);
# 改名字rename(表名不能是复数,这边是不规范的)
alter table student rename to newstudent;

数据的基本操作

数据是一行一行的,一条一条的

添加数据行

# 插入单条数据
# 顺序没要求,但属性一定要一一对应;
# 如果不写第一个"()"的内容,就要按顺序了,且id处填null
insert into teacher (name, phone, address) values ('willorn','120120120','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;

清空表(自增id会从1开始,而`delete`不会)
delete from teacher;
truncate table student;

更新数据行

where指定的字段要尽可能的唯一,防止SQL注入

update teacher set name='Jack' where id=1;

查找数据

# 查整张表(*表示找出全部,方便但是性能低)
select * from teacher;

select name as 'teacherName' from teacher where id=1;

SQL语句区分

  • DDL:数据定义语言 》 Data Definition Language
    • 针对【数据库】- 数据库定义语言 create alter drop show
  • DML:数据操作语言 》 data manipulation language
    • 针对【数据】- 数据库操纵语言 insert update delete select
  • DCL:分配权限的语言

查看字符集情况

[1]查找所有字符集设置
show variables like 'character_set_%';
[2]设置客户端字符集
set character_set_client = utf8;

数据类型

设计数据库的数据类型很复杂,需要按照真实的项目需求来设计。

数值类型

unsigned不可能为负数

年龄一般就是UTINYINT:无符号的,年龄不能为负数,难道你还能预约吗?

类型大小范围(有符号)范围(无符号unsigned)用途
TINYINT1 byte(-128,127)(0,255)小整数值
SMALLINT2 bytes(-32 768,32 767)(0,65 535)大整数值
MEDIUMINT3 bytes(-8 388 608,8 388 607)(0,16 777 215)大整数值
INT or INTEGER4 bytes正负21个亿0~42亿大整数值
BIGINT8 bytes(-9,223,372,036,854,775,808,9 223 372 036 854 775 807)(0,18 446 744 073 709 551 615)极大整数值
FLOAT4 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)单精度 浮点数值
DOUBLE8 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的值小数值

int实际操作

create table emp(	
	id smallint unsigned auto_increment primary key comment '员工id',
	age tinyint unsigned,
	kkk int(6)
);

【注意】int(num)里的代表数据的宽度,但是超过了宽度也没有问题,但是不能超过数据的默认范围。
【报错】out of range value for column

【Tips】tinyint默认是3(255),smallint默认是5(65535)

浮点数

浮点数应用(**千万不能用浮点型!!!!!**存在精度丢失问题!!!)

create table t_1(
	num1 float(3,1),
	num2 double(5,2)
);

(总宽度,小数部分宽度)

超过数据的保留小数

基础操纵

alter table student add gender unsigned tinyint;
alter table student drop gender;
alter table student change gender varchar();

create database 'student' if not exists 'student';
show create database 'student';
drop database if exists 'student';
alter database student charset = gbk;
show database;

create table student (idx int primary key auto_increment, 
                      gender unsigned tinyint default 1, 
                      name varchar(8) not null)engine = InnoDB;
create index pk_student on student ()

drop table student;
rename table student to teacher;

insert into student () values();
delete from student where xx = xx;
update student set isValid = 1 where age > 18;
select * from student 
	where xx = xx and 
	union = and
	group by and
	having 
	order by age;
select * from student
	where idcard regexp '\\[[:digit:]]{4}\\'
	order by id;

create view convinence as select * from student;





calculate

select name, age from student 
	where age between 12 and 18;
Mod()
Exp()
Rand()

聚集函数:
AVG COUNT MAX MIN SUM
select max(score)  maxScore,
	min(score) minScore,
	COUNT(score) '总数'
	from Scores;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

willorn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值