Mysql基础语句+数据类型实例

栏目: 数据库 · Mysql · 发布时间: 6年前

内容简介:先建一张表用来练习

先建一张表用来练习

create table class(
    id int primary key auto_increment,
    sname varchar(10) not null default '',
    gender char(1) not null default '',
    company varchar(20) not null default '',
    salary decimal(6,2) not null default 0.00,
    fanbu smallint not null default 0
)engine myisam charset utf8;复制代码

Mysql基础语句+数据类型实例

1、insert语句(插入语句)

# 插入中文之前需要输入 set names gbk;
insert into class
(sname,company, salary)
values
('刘备', '皇室成员', 15.28),
('孙策', '江东集团', 56.34),
('曹操', '宦官后裔', 88.56 );复制代码

Mysql基础语句+数据类型实例

2、update语句(更新语句)

update class
set fanbu  = 20000
where id=3;复制代码

Mysql基础语句+数据类型实例

3、delete语句(删除语句)

delete from class where salary>80复制代码

Mysql基础语句+数据类型实例

4、select语句(查询语句基础,后面学习复杂的查询语句)

select sname, salary from class;
select * from class where id>3;
复制代码

Mysql基础语句+数据类型实例

5、alter语句(增加列)

#增加一列
alter table class add score tinyint unsigned not null default 0复制代码

Mysql基础语句+数据类型实例

6、字符类型

6.1数字类型

类型 大小 范围(有符号) 范围(无符号) 用途
TINYINT 1 字节 (-128,127) (0,255) 小整数值
SMALLINT 2 字节 (-32 768,32 767) (0,65 535) 大整数值
MEDIUMINT 3 字节 (-8 388 608,8 388 607) (0,16 777 215) 大整数值
INT或INTEGER 4 字节 (-2 147 483 648,2 147 483 647) (0,4 294 967 295) 大整数值
BIGINT 8 字节 (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) (0,18 446 744 073 709 551 615) 极大整数值

提示,这里插入一则关于宽度的解释,例如

alter table class add age1 tinyint(1) not null default 0;复制代码

这里的tinyint(1),里面的1是什么意思,1就是宽度,但在这里没有任何效果

还有就是zerofill你知道什么意思吗,其实这个宽度必须跟zerofill组合起来才有作用

alter table class add snum smallint(5) zerofill not null default 0;复制代码

Mysql基础语句+数据类型实例

这里可以看到snum这一个字段default默认是5个0,我们加一些数据进去,看看效果

insert into class (sname, snum) values ('廖化', 15);复制代码

Mysql基础语句+数据类型实例

6.2 小数类型

Float(M,D) decimal(M,D)

M代表总位数,D代表小数右边的位数,比如decimal(4,2),代表总共4位,小数两位,整数就是4-2 =2位

接下来练习一下,建立一个新表

create table salary(
    sname varchar(20) not null default '',
    gongzi float(6,2)
)engine myisam charset utf8复制代码

Mysql基础语句+数据类型实例

插入两条数据

insert into salary 
values
('张三', -9999.99),
('李四', 9999.99)复制代码

Mysql基础语句+数据类型实例

我们看看浮点数能不能zerofill和unsigned

alter table salary add bonus float(5, 2) unsigned not null default 0.00;复制代码

Mysql基础语句+数据类型实例

插入数据

# (unsigned也能作用于小数,所以报错了)
insert into salary (sname, bonus) values ('王五', -888.88);
复制代码

Mysql基础语句+数据类型实例

MySQL数据类型 含义
float(m,d) 单精度浮点型 8位精度(4字节) m总个数,d小数位
double(m,d) 双精度浮点型 16位精度(8字节) m总个数,d小数位
Decimal(M,D) M+2 未打包的浮点数,用法类似于FLOAT和DOUBLE,如果在ASP中使用到Decimal数据类型,直接从数据库读出来的Decimal可能需要先转换成Float或Double类型后再进行运算。

6.3 比较float和decimal(decimal更精确)

比如js里,0.1+0.2 == 0.3 返回的是false,这就是float的缺陷

#建表
create table account (
    id int not null default 0,
    acc1 float(9,2) not null default 0.00,
    acc2 decimal(9,2) not null default 0.00
)engine myisam charset utf8;复制代码

Mysql基础语句+数据类型实例

# 建表之后,插入数据,来做比较
insert into account 
values
(1, 1234567.23, 1234567.23);
# 以下图片的结果,就出现了问题,float我们存的是1234567.23,但是取的时候变成了1234567.25
# decimal就不会出现这个问题复制代码

Mysql基础语句+数据类型实例

6.2 字符类型

类型 大小 用途
CHAR 0-255字节 定长字符串
VARCHAR 0-65535 字节 变长字符串
BLOB 0-65 535字节 二进制形式的长文本数据
0-65 535字节 长文本数据

这是最常见的4种字符类型

6.2.1比较一下char和varchar类型

# 建表
# char(N),不够N个长度,用空格在尾部补够N个长度,浪费了尾部,但是取出的时候,会删掉空格
# varchar(N),不用空格补齐,但列内容前,有1-2个字节来标志该列的内容长度
# 它们的N都是表示的字符,不是字节
create table test(
    ca char(6) not null default '',
    vca varchar(6) not null default ''
)engine myisam charset utf8;复制代码

Mysql基础语句+数据类型实例

# 接着插入数据,来做比较
insert into test values
('aa ', 'aa ')
select concat(ca, '!'), concat(vca, '!') from test;
# 注意下图,其中char和!之间没有空格,但是varchar和!之间有空格
# 原因在于,char存的时候,如果长度是6个字符,你只存了1个字符,那么剩下空的5个字符就用空白补齐
# 但是补齐之后,取的时候,会自动删掉后面的空格
# 所以我们存aa加一个空格的时候,后面那个空格在取的时候就被自动删掉了
# 但是varchar不会存在这个问题复制代码

Mysql基础语句+数据类型实例

6.2.2 text类型

# 注意text一般用来存文章、新闻内容,声明text列时,不必给默认值,给了会报错
create table test2(
    article text not null default ''
)复制代码

Mysql基础语句+数据类型实例

# 重新建test2这张表
create table test2(
    article text
);复制代码

Mysql基础语句+数据类型实例

6.2.3 blob的意义

# 在上面test2表的基础上,增加一个blob类型的列
# blob一般用来存图像,音频等二进制信息,用于防止字符集问题造成信息丢失
alter table test2 add img blob;复制代码

Mysql基础语句+数据类型实例

6.3 日期时间类型

MySQL数据类型 含义
date 日期 '2008-12-2'(3个字节)
time 时间 '12:25:36'(3个字节)
datetime 日期时间 '2008-12-2 22:06:44'
timestamp 自动存储记录修改时间
year 存储年份

6.3.1 date类型

# date 范围 1000-01-01 到 9999-12-31
# 建表
create table test3(
    star varchar(20) not null default '',
    birth date not null default '0000-00-00'
)engine myisam charset utf8;复制代码

Mysql基础语句+数据类型实例

# 插入数据
insert into test3
values
('小张', '1961-02-23');复制代码

Mysql基础语句+数据类型实例

6.3.2 time类型

# 建新列
# time范围是-838:59:59和'838:59:59'
alter table test3 add sign time not null default '00:00:00';复制代码

Mysql基础语句+数据类型实例

# 插入数据
insert into test3
(star, sign)
values
('rereww','25:10:15');复制代码

Mysql基础语句+数据类型实例

6.3.3 datetime类型

# 建表
# 实际上,一般我们用时间戳来存年月日+时分秒这个时间
create table test4(
    sname varchar(20) not null default '',
    logintime datetime not null default '0000-00-00 00:00:00'
)engine myisam charset utf8;复制代码

Mysql基础语句+数据类型实例

# 插入数据
insert into test4
values
('张三', '2001-10-12 15:33:19');复制代码

Mysql基础语句+数据类型实例

6.3.4 timesstamp类型

# 建表
create table test5(
    ts timestamp default CURRENT_TIMESTAMP,
    id int
)engine myisam charset utf8;复制代码

Mysql基础语句+数据类型实例

# 插入数据
insert into test5
(id)
values
(1),(2),(3);
# 注意timestamp自动填充当前时间复制代码

Mysql基础语句+数据类型实例

6.3.5 year类型

# 建表
# year 范围1911-2155(所以一般不用这个类型存年份,还不如用字符串呢)
create table test6 (
    thing varchar(20) not null default '',
    ya year not null default '0000'
)engine myisam charset utf8;复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Practical Algorithms for Programmers

Practical Algorithms for Programmers

Andrew Binstock、John Rex / Addison-Wesley Professional / 1995-06-29 / USD 39.99

Most algorithm books today are either academic textbooks or rehashes of the same tired set of algorithms. Practical Algorithms for Programmers is the first book to give complete code implementations o......一起来看看 《Practical Algorithms for Programmers》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码