mysql、数据类型、整数型、小数型、字符串型、时间型、json型、bit型、enum set复合型、binary字节型、char和varchar的区别、将图片字节化存入数据库、读取数据库图片等

目录

数字和字符串

时间型

bit布尔型

enum枚举 set集合复合型

json型

binary字节型与将图片字节化存入数据库、读取数据库图片共同演示

将图片字节化存入数据库、读取数据库图片与binary字节型共同演示

char和varchar的区别


数字和字符串

整数型tinyint、smallint、mediumint、int、bigint;小数型float、double 、decimal(p,s)、numeric(p,s)。字符串:char、varchar、text、tinytext、mediumtext、longtext。

use db;

--数据类型 数字
--整型数字 TINYINT SMALLINT MEDIUMINT INT BIGINT
--小数数字 float double decimal(p,s) numeric(p,s)
--建立表
create table t1(
    c1 TINYINT,//微小型
	c2 SMALLINT,//小型
	c3 MEDIUMINT,//中型
	c4 BIGINT,//大型
	f1 float,
	f2 float(6,2),
	d1 double(2,1),
	d2 double(6,2),
	d3 decimal(7,3)
);
insert t1 value(1,2.5,3,4,1,2,3,4,5);//添加数据
insert t1 value(1,2.5,3.5,4.1,1.3,2.2,3,4.9,5.8);
select * from t1;
show create table t1;//显示表的配置

create table t2(
/* UNSIGNED表示无符号,ZEROFILL表示补零 */
    c1 tinyint,/*-128 - 127*/
	c2 TINYINT UNSIGNED,/*0 - 255*/
	C3 int(5) UNSIGNED ZEROFILL,
	D1 DECIMAL(15,3) UNSIGNED,
	D2 DECIMAL(15,3) UNSIGNED ZEROFILL
);
insert into t2 value(127,200,2,.12345678,18); 
insert into t2 value(127,200,2,.12345678,18.7); 

select * from t2;

create table student(
id int unsigned auto_increment,
name varchar(30),
age tinyint,
money decimal(10,2),
primary key(id)
);
insert into student values(null,'java',18,200);
select * from student;



--数字类型 字符型(串)
show tables;//查看表
select user(), database(),now(),3*3,rand(),version(),sysdate();//以表格的形式显示数据库用户、当前数据库名、时间、结果、随机数、mysql版本、时间
select CURRENT_DATE,curdate();//时间

--删除几个指定的表
drop table if exit t1,t2,t3;
/*多行注释*/
--单行注释
--字符串的值需要添加单引号,数字类型一般不加单引号

--string char(n) varchar(n) Tinytext text mediumtext longtext
create table t3(
s1 char(6),/*定长字符串,功能是,如果设置6,则插入数据不到被空格也要占用6字节*/
s2 char(200),
s3 char(255),
s4 varchar(10000),//0 - 16383/*变长字符串,如果设置10000,最多插10000个字符,如果插入2个字,则占用2个字符的字节数*/
t1 text,
t2 tinytext,
t3 mediumtext,
t4 longtext
);
drop table t3;
select * from t3;
insert into t3 values(100,'你好','hello world','变长字符串,如果设置10000,最多插10000个字符,如果插入2个字,则占用2个字符的字节数','jghshhgfjush','hjgiuhghiuhgru','hgidhru','ghuriunrh');

时间型

date、time、datetime、year、timestamp

/**时间类型:date(yyyy-MM-dd)、time(HH:mm:ss)、datetime(yyyy-MM-dd HH:mm:ss)、timestamp(yyyy-MM-dd HH:mm:ss)*/
create table if not exists t4(
    t1 date,
    t2 time,
    t3 datetime,
    t4 timestamp/*时间戳*/
);
select * from t4;
drop table t4;
insert into t4 values(now(),now(),now(),now()),(current_date,current_time,now(),current_timestamp);
/*datatime和timestamp的区别,tiemstamp显示的是最后一次修改时间*/
create table if not exists t5(
    name varchar(20),
    create_time datetime,
    update_time timestamp default current_timestamp on update current_timestamp
);
select * from t5;
drop table t5;
insert t5 value('wzr',now(),current_timestamp);
update t5 set name='wzt';

结果

bit布尔型

/*bit布尔型*/
create table if not exists t7(
    id int unsigned not null auto_increment,
    name varchar(15),
    tf bit,/*true或1 false或0*/
    stf tinyint unsigned default 0,
    primary key (id)
)engine=innodb default charset = utf8mb4;
drop table if exists t7;
select * from t7;
insert into t7 value(null,'wzt1',true,1);
insert into t7 value(null,'wzt2',1,1);
insert into t7 value(null,'wzt3',false,0);
insert into t7 value(null,'wzt4',0,0);

结果

enum枚举 set集合复合型

/*enum枚举、set集合*/
create table if not exists t6(
    id int unsigned auto_increment not null,
    name varchar(20),
    genter enum('男','女','妖') default '妖',/*必须是enum其中的一个*/
    hobby set('看小说','打游戏','乒乓球','旅游'),/*必须是集合中的,可以是多个*/
    primary key (id)
);
select * from t6;
insert t6 value(1,'wzt','男','看小说,打游戏');
insert t6 value(null,'wzt',null,'看小说,打游戏');
insert into t6 (name,hobby) value ('yaozeng','旅游');

结果

json型

/*json类型*/
select json_type('[]');/*array*/
select json_array(1,2,true,4);
select json_type('[1,2,3,4]');
select json_type('[1,"数组",true,"mysql"]');

select json_type('{}');/*object*/
select json_object('id',1,'name','wzt','tf',true);
select json_type('{"key":"value"}');

select json_type('"wzt"');/*string*/

select json_type('10');/*Integer*/

select json_type('true');/*boolean*/
/*建表,使用json*/
create table if not exists t8(
    stu json,
    dept varchar(50),
    xueyuan varchar(20)
)engine=innodb default charset=utf8mb4;
select * from t8;
drop table if exists t8;
insert into t8 value('{"stuno":3118010299,"name":"李四","age":24}','计算机科学与技术','信息与电子工程学院');
insert into t8 value('{"stuno":3118010288,"name":"王二","age":66}','软件工程','信息与电子工程学院');
insert into t8 value('{"stuno":3118010277,"name":"张三","age":18}','网络与安全','信息与电子工程学院');

/*查询*/
select json_extract(stu,'$.name') from t8;/*查询学生名字*/
select stu->>'$.name' from t8;/*去除姓名的双引号*/
select JSON_UNQUOTE(json_extract(stu,'$.name')) from t8;/*去除姓名的双引号*/
select dept,stu->>'$.name' from t8;/*查询专业和姓名*/
select dept,stu->'$.name' from t8;
select * from t8 where stu->'$.name'='李四';/*查看李四的信息*/
select * from t8 where stu->'$.age'>24;/*查询年龄大于二十四的学生信息*/
select * from t8 where stu->'$.name' like '%二%';/*查询名字里有二的学生信息*/
select * from t8 order by stu->>'$.age' asc ;/*升序*/

/*添加、删除和修改json里的数据*/
insert into t8 value('{"stuno":3118010266,"name":"麻子","age":9}','会计','信息与电子工程学院');/*添加*/
delete from t8 where stu->'$.age'=9;/*删除年龄为9的学生信息*/
update t8 set stu=json_set(stu,'$.age',15) where stu->'$.name'='麻子';/*将麻子的年龄 改为15岁*/
update t8 set stu=json_remove(stu,'$.stuno') where stu->'$.name'='麻子';/*删除json中的stuno列*/

结果图

binary字节型与将图片字节化存入数据库、读取数据库图片共同演示

/*binary二进制*/
create table if not exists t9(
    id int unsigned not null auto_increment,
    address varchar(100),
    tp longblob,/*二进制中binary较小,所以多用blob。其中tinyblob微小型 medumblob中型 longblob长型*/
    primary key (id)
);
select * from t9;


将图片字节化存入数据库、读取数据库图片与binary字节型共同演示

@Test
    @DisplayName("blob、将图片字节刘存入数据库中")
    public void Blobtest() throws ClassNotFoundException, SQLException, FileNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3305/db?useUnicode=true&characterEncoding=utf-8","root","");
        String sql = "insert into t9 value(null,?,?)";
        String s = "D:\\桌面\\bg.jpg";
        File file = new File(s);
        String name = file.getName();
        FileInputStream fis = new FileInputStream(file);
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,name);
        ps.setBlob(2,fis);
        int i = ps.executeUpdate();
        if(i>0) System.out.println("插入成功");
    }
    @Test
    @DisplayName("blob、将图片从数据库中读出")
    public void Blobtest2() throws ClassNotFoundException, SQLException, IOException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3305/db?useUnicode=true&characterEncoding=utf-8","root","");
        String sql = "select * from t9 where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1,1);
        ResultSet rs = ps.executeQuery();
        if(rs.isBeforeFirst()) {
            rs.next();
            rs.getBlob(3).getBinaryStream().transferTo(new FileOutputStream(rs.getString(2)));
            System.out.println("读取成功");
        }

    }

结果

char和varchar的区别

char定长字符串,功能是,如果设置6,则插入数据不到被空格也要占用6字节

varchar变长字符串,如果设置10000,最多插10000个字符,如果插入2个字,则占用2个字符的字节数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值