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

先建一张表用来练习数据库

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


一、insert语句(插入语句)

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


二、update语句(更新语句)

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


三、delete语句(删除语句)

delete from class where salary>80复制代码


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

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


五、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表中bash

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

六、数据类型

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就是宽度,但在这里没有任何效果ui

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

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


这里能够看到snum这一个字段default默认是5个0,咱们加一些数据进去,看看效果3d

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


6.2 小数类型

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

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

接下来练习一下,创建一个新表blog

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 字符类型

类型 大小 用途
CHAR 0-255字节 定长字符串
VARCHAR 0-65535 字节 变长字符串
BLOB 0-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;复制代码
相关文章
相关标签/搜索