MySQL建表过程/数据类型

须知

A.主要学习列类型的存储范围与占据的字节关系

B.存储同样的数据不同列类型所占据的空间和效率是不一样的

C.一个字节八个位

D.参考

MySQL三大字符类型

1、 数值型
b.整形:
Tinyint(占1个字节-128~127 or 0~255)、Smallint(2个字节)、Mediuint(3个字节)、int(4个字节)、Bigint(8个字节)
<1>学习Tinyint的参数并验证字节与范围的关系
(m) unsigned zerofill —-位数不够用0补充到m位,无符号

alter table class add snum smallint(5) zerofill not null default 0;#

<2>unsinged表示无符号,可以影响存储范围

a.浮点型:
<1>Float(M,D),整数位和小数位一起存储
<2>decimal(M,D),整数位和小数位分开存储,精度更高
M代表总位数,D代表小数点右边的位数。

 alter table salary add bonus float(5,2) unsigned not null default 0.0;

2、字符型
<1>char(M)定常类型,不够M个长度就在尾部补上空格,凑齐M个长度,利用率<=100%
<2>varchar(M)变长类型,不用空格补全,利用率<100%
<3>text文本类型,不用加默认值,存储文章内容,新闻内容等

create table test(article text); #不用加not default

<4>blob二进制类型,用来存储图像/音频等二进制信息
3、日期时间型
<1>日期型date,3个字节,存储范围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;

<2>时间型time,3个字节

alter table test3 add sign time not null default '00:00:00';
insert into test3 (star,sign) values ('李旭','19:12:22');

<3>日期时间型datetime,8个字节

create table test4 (
sname varchar(20) not null default '',
logintime datetime not null default '0000-00-00 00:00:00'
)engine myisam charset utf8;
desc test4;

<4>年份类型year,1个字节
<5>timestamp,4个字节

语法训练

#建数据库
create database test;
use test;

#创建表
#所谓的建表就是声明列的过程,所以要首先分析列
create table member(
id int unsigned auto_increment primary key,
username char(20) not null default '',
gender char(1) not null default '',
weight tinyint unsigned not null default 0,
birth date not null default '0000-00-00',
salary decimal(8,2) not null default 0.00,
lastlogin int unsigned not null default 0
)engine myisam charset utf8;

#修改表的语法
create table m1(
id int unsigned auto_increment primary key
)engine myisam charset utf8;#建表
desc m1;#查看
#增加列
alter table m1 add username char(20) not null default '';#在最后一列,增加一列
desc m1;#查看
alter table m1 add birth date not null default '0000-00-00';#在最后一列,增加生日列
desc m1;#查看
alter table m1 add gender char(1) not null default '' after username;#增加性别列,加在username后
desc m1;#查看
alter table m1 add pid int not null default 0 first;#在第一列增加一列
desc m1;#查看
#删除列
alter table m1 drop pid;#删除pid列
desc m1;#查看
#修改列类型
alter table m1 modify gender char(4) not null default '';#吧char(1)改为char(4)
desc m1;#查看
#修改列名和类型
alter table m1 change id uid int unsigned ;#注意查看表的变化,自增长丢失了,但是主键还在
desc m1;#查看

猜你喜欢

转载自blog.csdn.net/qq_40006058/article/details/79965008