0%

数据库

数据库

数据库基础

基础

1.数据库

数据库就是用来存储各种数据的容器

2.数据库管理系统

专门用于创建和管理数据库的统一软件,介于应用和操作系统之间。比如MYSQL、SQL Server、Oracle等。他们不仅有基础的数据管理功能,还能保证数据的完整性、安全性和可靠性

3.数据库应用程序

通过数据库管理系统提供的接口与之通信、访问和管理数据的应用程序

4.SQL语言

它是一种数据库查询语言和程序设计语言,主要用于管理数据库中的数据,如存储数据、查询数据、更新数据

MYSQL

MYSQL下载链接

历史版本下载链接,选择MySQL Community Server

下载使用5.0的最高版本5.0.96

使用

启动服务:

1
net start mysql

关闭服务:

1
net stop mysql

登录:

1
mysql -h localhost -u root -p

登录也可以直接打开MySQL Command Line Client

image-20240719221312406

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
mysql> help

For information about MySQL products and services, visit:
http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
https://shop.mysql.com/

List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for `help'.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
notee (\t) Don't write into outfile.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'

查询当前有哪些数据库:

1
show databases;

image-20241217170658744

数据库和表操作

通过 show databases; 可以查看到所有有的数据库,一个数据库对应的就是一个文件夹

image-20241217171020105

选择一个数据库:

1
use mysql;

数据库操作

1.查看全部数据库

1
show databases;

2.创建数据库

1
create database 数据库名

3.查询创建好的数据库

1
show create database 数据库名

4.修改数据库

1
alter database 数据库名 default character set 编码方式 collate 编码方式_bin

如:

1
alter database abcabc default character set gbk collate gbk_bin

5.删除数据库

1
drop database 数据库名

数据类型

数字类型

数据类型 字节数
TINYINT 1
SMALLINT 2
MEDIUMINT 3
INT 4
BIGINT 8
FLOAT 4
DOUBLE 8
DECIMAL(M, D) M+2

日期类型

数据类型 字节数 日期格式 取值范围
YEAR 1 YYYY 1901~2155
DATE 4 YYYY-MM-DD 1000-01-01999912-03
TIME 3 HH:MM:SS -838:59:59~838:59:59
DATETIME 8 YYYY-MM-DD HH:MM:SS 1000-01-01 00:00:00 ~ 9999-12-31 23:59:59
TIMESTAMP 4 YYYY-MM-DD HH:MM:SS 1970-01-01 00:00:01 ~ 2038-01-19 03:14:07

字符串和文本类型

数据类型 字节数
CHAR 用于表示固定长度的字符串,char(4),一直占用4个字节
VARCHAR 用于表示可变长度的字符串,比如"a"占1+1=2个字节,"aa"占用2+1=3个字节
BINARY 用于表示固定长度的二进制长度
VARBINARY 用于表示可变长度的二进制长度
TINYTEXT 0~255字节
TEXT 0~65535
MEDIUMTEXT 0~16777215字节
LONGTEXT 0~4294967295字节

数据库表的操作

1.创建数据表

1
create table 表名;

2.查询当前数据库所有表

1
show tables;

3.查看数据表

1
show create table 表名;
1
desc 表名;

image-20241217173806616

4.修改表

1
2
// 修改表名
alter table 旧表名 rename 新表名;

image-20241217174209933

1
2
// 修改字段名的属性
alter table 表名 modify 字段名 数据类型;

image-20241217174408895

1
2
// 修改字段名
alter table userinfo change id userid int(6);

image-20241217174607451

1
2
// 新增字段
alter table userinfo add newinfo int(8);

image-20241217174731199

1
2
// 删除字段
alter table userinfo drop newinfo;

image-20241217174823026

1
2
// 删除表
drop table 表名;

添加、更新和删除数据

对于int类型数据,数据类型有zerofill属性和没有zerofill属性的区别:

image-20241217195621319

查询

查看表中的数据:

1
select * from 表名

添加

向表中添加数据(使用字段名):

1
insert into 表名 (id, name, StudentNumber) values(1, "Gianluca Zeong", 20231084);

如上面这一条插入语句,如果name的字符数超过了定义的属性varchar(8),那么就会截断8个字符。

可以再使用alter把type改的大一点

向表中添加数据(使用不全字段名):

1
insert into 表名 (id, StudentNumber) values(1, 20233033);

image-20241217200408971

没有给值的name字段因为本来是varchar类型,所以会显示NULL

向表中添加数据(不使用字段):

只写values的时候需要与字段顺序和类型完全对应:

1
insert into userinfo values(3, "goblin", 20231111);

添加多行数据:

1
insert into userinfo values(4, "444", 44444444), (5, "555", 55555555);

image-20241217203148102

前面在id=1和3之间删了一条2,现在插入多条时,第一条会插入到之前删掉的那一条的位置

更新

1
update 表名 set 字段名 = 值

上面这个语句会将表中所有的 字段名 改成

1
update 表名 set 字段名 = 值 where 条件

上面这个语句会将所有匹配where条件的数据的 字段名 改为

image-20241217201512953

删除数据

1
delete from 表名 where 条件;

image-20241217202748820

单表查询

查看表的数据:

1
select * from 表名;

条件查询:

1
select * from 表名 where 条件表达式

where联合

image-20241217204306484

聚合查询:

Count() sun() avg()