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

news/2024/8/27 8:45:08

先建一张表用来练习

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;复制代码


1、insert语句(插入语句)

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


2、update语句(更新语句)

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


3、delete语句(删除语句)

delete from class where salary>80复制代码


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

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


5、alter语句(增加列)

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


# 在指定的地方增加一列,比如我们在上图的基础上,指定在id列后面增加一列
alter table class add zengjia char(1) not null default '' after id;复制代码


# 把新列加在最前面,可以用first 例如
alter table class add fir char(1) not null default '' first;
复制代码


# 删除列 语法用drop,例如删除fir列
alter table class drop fir;复制代码


# 修改列类型 语法用modify 用法如下
alter table class modify zengjia char(4) not null default '';复制代码


# 修改列名及列类型 语法用change 例如
alter table class change zengjia hahha char(3) not null default '';复制代码


insert语句插入检索的数据,需要注意不用写values比如,将custnew表查询的数据插入customers表中

insert into customers(
    cust_id,
    cust_email
) select (
    cust_id,
    cyst_email
)from cystnew复制代码

6、数据类型

6.1数字类型

类型大小范围(有符号)范围(无符号)用途
TINYINT1 字节(-128,127)(0,255)小整数值
SMALLINT2 字节(-32 768,32 767)(0,65 535)大整数值
MEDIUMINT3 字节(-8 388 608,8 388 607)(0,16 777 215)大整数值
INT或INTEGER4 字节(-2 147 483 648,2 147 483 647)(0,4 294 967 295)大整数值
BIGINT8 字节(-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;复制代码


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

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


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复制代码


插入两条数据

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


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

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


插入数据

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


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;复制代码


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



6.2 字符类型

类型大小用途
CHAR0-255字节定长字符串
VARCHAR0-65535 字节变长字符串
BLOB0-65 535字节二进制形式的长文本数据
TEXT
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;复制代码


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


6.2.2 text类型

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


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


6.2.3 blob的意义

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


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;复制代码


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


6.3.2 time类型

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


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


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;复制代码


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


6.3.4 timesstamp类型

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


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


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;复制代码



http://www.niftyadmin.cn/n/3166392.html

相关文章

java xml判断节点_java 判断xml中是否含有一个指定的节点

RT,使用jdom4进行解析。/**** 判断xml中是否含有一个指定的节点* param str xml主题内容* param nodeString 指定的节点* return*/SuppressWarnings("rawtypes")public static boolean isExistNote(String str,String nodeString ) {if (StringUtils.isBl…

async/await的简单使用

async/await的简单使用 使用场景: async/await是基于Promise对象的使用,使用在异步返回消息执行顺序的处理逻辑 问题描述: 先看一下这段代码,这段代码主要实现的功能是,获取定位地址并赋值在input框中,但…

kali命令连接wi-fi_如何从Windows 8中删除打开或不安全的Wi-Fi热点:带源的Wifi.exe命令行实用工具

kali命令连接wi-fiFor the most part Im happy with Windows 8 but one feature was removed that makes no sense to me - the wireless networks dialog. 在大多数情况下,我对Windows 8感到满意,但是删除了一项对我没有意义的功能-无线网络对话框。 Su…

java 分布式 定时任务_Java中实现分布式定时任务的方法

定时器Scheduler在平时使用比较频繁,在springboot中,配置好Scheduled和EnableScheduling之后,定时器就能正常执行,实现定时任务的功能。但是在这样的情况下:如果开发的服务需要水平部署实现负载均衡,那么定…

阿里云表格存储TableStore全新升级 打造统一在线数据存储平台 ...

3月6日,阿里云表格存储 TableStore 全新升级,突破原生 NoSQL 数据库边界。提供多元索引、二级索引能力,支持数据便捷查询,与计算生态无缝集成,打通数据实时消费通道,打造统一在线数据存储平台,深…

java jceks 密钥,JCEKS keyStore在JavaTM加密扩展中使用的加密?

In JCEKS key Store which algorithm used and what is the size of key .i find something that its use Triple DESbut what is the key size..?Thanks解决方案Currently, each PrivateKey and SecretKey entry in a JCEKS key store is encrypted with 3-key triple DES in…

Fastify 2.0.1 和 1.14.3 发布,极速 Node.js Web 框架

Fastify 2.0.1 和 1.14.3 发布了。Fastify 是一个高度专注于以最少开销和强大的插件架构为开发者提供最佳体验的 Web 框架,号称是目前最快的 Node.js 应用框架之一。 它受 Hapi 和 Express 的启发。 2.0.1 更新内容主要有: Fixes: 修复:seria…

java smooks_Smooks 1.2框架:处理XML与非XML的Java框架

Smooks 1.2框架能够用于各种数据格式的转换:XML to XML,CSV to XML,EDI to XML,XML to EDI,XML to CSV,Java to XML,Java to EDI,Java to CSV,Java to Java,X…