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

1,407 阅读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;


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;