一、数据库分类补充
1. 关系型数据库
MySQL,Oracle,SQLserver,postgresql, DB2, access, MariaDB等
2. 非关系型数据库
Redis, memcache, mongoDB等
特征:
1. 关系型数据库是可以建立表关系的,拥有固定的表结构
2. 非关系型数据库它是以K: V键值对的形式存在
二、针对数据库的SQL语句
例如:
show databases; 查看数据库
shou create databases db1; 查看数据库db1
create databases db1; 创建数据库
alter database db1 charset= 'gbk' ; 修改数据库db1的字符集为gbk
drop database db1; 删除数据库
use mysql; 使用数据库
三、针对数据库的表的SQL语句
- - 什么是表?
- - 表相当于文件,表的一条记录就相当于文件的一行内容,不同的是,
- - 表中的一条记录有对应的标题,称为表的字段
- - 查看当前所在的库
select database( ) ;
- - 使用库
use db1;
- - 查看表
show tabels;
- - 查看表的结构
show create table t1;
- - 格式化之后的表结构
describe t1;
desc t1;
- - 创建表
create table t2( id int ( 11 ) , name varchar( 20 ) , age int ) ;
- - 修改表
alter table t1 rename tt1;
- - 删除表
drop table t1;
四、针对记录的SQL语句
- - 1. 查看表中的数据
select * from t1;
select id from t1;
select id , name from t1;
- - 2. 增加数据
- - 增加单条数据
insert into t1 values( 1 , 'kevin' , 18 ) ;
- - 增加多条数据
insert into t1 values( 2 , 'tank' , 19 ) , ( 3 , 'zs' , 21 ) ;
- - 根据选择的字段增加数据
insert into t1( id , name) values( 4 , 'json' ) , ( 5 , 'python' ) ;
- - 3. 修改数据
update t1 set age= 25 where id = 4 ;
update t1 set age= 26 where id = 5 ;
- - 4. 删除数据
delete from t1 where id = 5 ;
五、存储引擎(存储数据的方式)
- - 什么事存储引擎
- - 就是存储数据的方式
- - MySQL中有哪些存储数据的方式
show engines;
- - 总共支持9 中存储引擎,我们需要掌握2 种:
- - MyISAM:
- - 它是MySQL5. 5 及之前的版本默认的存储引擎,它的存取速度更快,但是数据相对InnoDB不够安全
- - InnoDB
- - 它是MySQL5. 6 及之前的版本默认的存储引擎,它的存取速度相对MyISAM更慢了,但是数据相对MyISAM更加安全
- - MEMORY
- - 它的数据是在内存中存着的,内存是基于电工作的,所以断电数据丢失,重启服务端也会丢失数据
- - 验证以上三者存储引擎所产生的文件
create table t2( id int ) engine= MyISAM;
create table t3( id int ) engine= InnoDB;
create table t4( id int ) engine= MEMORY;
- - MyISAM:
- - 产生这三个文件:
- - . frm:这个文件存表结构
- - . MYD:这个文件存数据 data
- - . MYI:这个文件存数据的索引 index >> > 类似于是书的目录 >> > 加快查询速度的
- - InnoDB:
- - 产生这个两个文件:
- - . frm: 这个文件存表结构
- - . idb: 这个文件存数据的索引和数据 index >> > 类似于是书的目录 >> > 加快查询速度的
- - MEMORY
- - MEMORY:
- - . frm: 这个文件存表结构
六、数据库的数据类型(重要)
1. 整型
tinyint smallint mediumint int bigint
不同的类型存储的范围不一样
存储范围比较:
tinyint:1 个字节- - - - - - > 8 位- - - - - - > 2 ** 8 - - - - > 256 - - - - - > 0 - 255 - - - - - > - 128 - 127
smallint: 2 个字节存储- - - - - > 16 位- - - - > 2 ** 16 - - - - > 65536 - - - - - > 0 - 65535 - - - - > - 32768 - 32767
int : 4 个字节- - - - - - > 32 位- - - - - > 2 ** 32 - - - - > 21. . . - - - - >
bigint:8 个字节- - - - - > 64 为- - - - - > 2 ** 64 - - - - >
默认情况整型是否带符号
create table t5 ( id tinyint) ;
create table t5 ( id smallint) ;
create table t5 ( id int ) ;
create table t5 ( id bigint) ;
insert into t5 values( 999 ) ;
insert into t5 values( - 129 ) ;
"""得出结论:默认情况整型是带符号的"""
2. 浮点型
小数
float double decimal
"""语法格式"""
float ( 255 , 30 ) ; 255 表示的是存储的位数,30 代表的是小数位数
double( 255 , 30 ) ; 255 表示的是存储的位数,30 代表的是小数位数
decimal( 65 , 30 ) ; 65 表示的是存储的位数,30 代表的是小数位数
decimal( 8 , 2 )
这三个有什么区别
create table t6( id float ( 255 , 30 ) ) ;
create table t7( id double( 255 , 30 ) ) ;
create table t8( id decimal( 65 , 30 ) ) ;
插入数据
insert into t6 values( 1.111111111111111111111111 ) ;
insert into t7 values( 1.111111111111111111111111 ) ;
insert into t8 values( 1.111111111111111111111111 ) ;
"""得出结论:精确度不一样"""
decimal >> > double >> > float
以后就是使用decimal
3. 字符串
char( 4 ) varchar( 4 )
这两个都是用来存储字符串使用的
char( 4 ) : 它是定长,那么他就存4 位,如果没有超出4 位,空格填充到4 位,超出4 位,报错或者,最大存4 位
varchar( 4 ) :可变长的,不超出4 位,有几位存几位,超出4 位,报错或者,最大存4 位
验证
create table t9( id int , name char( 4 ) ) ;
create table t10( id int , name varchar( 4 ) ) ;
insert into t9 values( 1 , 'kevin' ) ;
insert into t10 values( 1 , 'kevin' ) ;
如果你想让报错,需要设置严格模式
如何查看严格模式
1. select @@sql_mode;
2. show variables like '%mode%'
设置严格模式
1. 永久修改:需要改配置文件
2. 临时修改:
set global sql_mode= 'STRICT_TRANS_TABLES,PAD_CHAR_TO_FULL_LENGTH' ;
研究定长和可变长
insert into t9 values( 2 , 'k' ) ;
insert into t10 values( 2 , 'k' ) ;
验证存储的长度
select char_length( name) from t9;
select char_length( name) from t10;
以后到底使用定长还是不定长呢,
大多数情况使用的都是varchar,如果你的存储数据就是固定的,就是有char
4. 日期类型
datetime date time year
年月日 时分秒 年月日 时分秒 年份
create table t14 ( id int ,
reg_time datetime,
update_time date,
delete_time time,
birth year
) ;
insert into t14 values( 1 , '2023-10-01 11:11:11' , '2023-07-10' , '11:11:11' , '2023' ) ;
5. 枚举类型
enum: 多选一
create table t15 ( id int ,
hobby enum( 'tangtou' , 'hejiu' , 'xijio' , 'chouyan' )
) ;
insert into t15 values( 1 , 'tangtou' ) ;
set :
多选多
create table t16 ( id int ,
hobby set ( 'tangtou' , 'hejiu' , 'xijio' , 'chouyan' )
) ;
七、整型中的数字代表的什么意思
char( 4 )
varchar( 4 ) 括号里的数字代表的就是存储的长度
int
tinyiny
smallint
整型的存储范围跟括号中的数字有没有关系:没有任何关系
整型的存储范围完全是跟关键字相关,int , smallint
create table t11 ( id int ( 3 ) ) ;
insert into t11 values( 99999 ) ;
整型中括号中的数字代表的是数据展示的位数
create table t12 ( id int ( 3 ) zerofill) ;
insert into t12 values( 9 ) ;
create table t13 ( id int ( 11 ) zerofill) ;
insert into t13 values( 9 ) ;
八、创建表的完整语法结构
create table 表名(
字段名1 数据类型 约束条件 约束条件 约束条件,
字段名2 数据类型 约束条件 约束条件 约束条件,
字段名3 数据类型 约束条件 约束条件 约束条件,
字段名4 数据类型 约束条件 约束条件 约束条件,
字段名5 数据类型 约束条件 约束条件 约束条件,
) ;
create table t17 (
id int ,
name varchar( 16 ) ,
age int
) ;
注意事项:
1. 字段名和数据类型是必须要写的
2. 约束条件是可选的,有就写,没有不写
3. 最后一个字段的末尾不能加逗