基本数据类型的测试
1.—tinyint类型
//创建表t1,类型为tinyint
//tinyint类型范围:有符号-128~127 无符号0~255
create table t1(num tinyint);
//测试tinyint
insert into t1 values(1);
insert into t1 values(128);
//将字段num类型改为无符号
alter table t1 modify num tinyint unsigned;
//再次测试
insert into t1 values(-1); //报错
insert into t1 values(255);
//查询t1表中所有字段的数据
select*from t1;
2.–bit[(M)] : 位字段类型。M表示每个值的位数,范围从1到64。如果M被忽略,默认为1。
//创建表t2,类型为bit(8)
create table t2(id int,num bit(8));
//测试bit(8)
insert into t2 (id,num)values(10,10);
insert into t2 (id,num)values(10,101);
//创建表t3,类型为bit(1)
//测试bit(1)
create table t3(gender bit(1));
insert into t3 values(0);
insert into t3 values(1);
insert into t3 values(2);//插入失败
alter table t3 modify gender bit(2);
insert into t3 values(3);
insert into t3 values(4);
3.–float[(m, d)] [unsigned] : M指定显示长度,d指定小数位数,占用空间4个字节
//创建表test1,类型为float
create table test1(age int,score float(4,2));
//测试float
insert into test1 values(12,88.92);
insert into test1 values(15,56.567);//显示结果是56.57,因为最后一位四舍五入了
//将字段float类型改为无符号
alter table test1 modify score float(4,2)unsigned;
insert into test1 values(12,-56.69);//插入失败
insert into test1 values(12,99.99);
4.–decimal(m, d) [unsigned] : 定点数m指定长度,d表示小数点的位数
create table test2(score decimal(6,3));
insert into test2 values(999.999);
insert into test2 values(999.9999);//插入失败
alter table test2 modify score decimal(65,30);
alter table test2 modify score decimal(66,30);//失败,decimal整数最大位数m为65
alter table test2 modify score decimal(65,31);//失败,支持小数最大位数d是30
5.–char(L): 固定长度字符串,L是可以存储的长度,单位为字符,最大长度值可以为255
create table test3(name char(5));
insert into test3 values('zhang');
insert into test3 values('hanmeimei');//插入失败,超过长度限制5
6.–varchar(L): 可变长度字符串,L表示字符长度,最大长度65535个字节
当表的编码是utf8时,varchar(n)的参数n最大值是65532/3=21844[因为utf中,一个字符占用3个字节],
如果编码是gbk,varchar(n)的参数n最大是65532/2=32766(因为gbk中,一个字符占用2字节)
create table test4(name varchar(5));
insert into test4 values('啦啦啦啦啦');
create table test5(name varchar(1));
insert into test5 values('陈威');//插入失败
alter table test5 modify name varchar(21845);//失败,最大值为21845
alter table test5 modify name varchar(21845)charset=gbk;
表的约束
表一:
goods | 商品表 | |||
---|---|---|---|---|
goods_id | goods_name | unitprice | category | provider |
商品编号 | 商品名称 | 单价 | 商品类别 | 供应商 |
1001 | 牛排 | 28.5 | 肉类 | |
2022 | 香蕉 | 3.2 | 水果 | |
9837 | 石榴 | 5.3 | 水果 |
表二:
customer | 客户表 | ||||
---|---|---|---|---|---|
customer_id | name | address | sex | card_id | |
客户编号 | 姓名 | 地址 | 邮箱 | 性别 | 身份证 |
1 | 唐三藏 | 东土大唐 | tangsanzhang@datang.com | 男 |
表三:
purchase | 订单表 | ||
---|---|---|---|
order_id | customer_id | goods_id | nums |
订单编号 | 客户编号 | 商品编号 | 购买数量 |
1 | 1 | 1001 | 5 |
//商品表
create table goods(
goods_id int primary key comment '商品编号',
goods_name varchar(32) comment '商品名称',
unitprice float(4,2)unsigned not null default 0.0 comment '单价',
category varchar(10) comment '商品类别',
provider varchar(30) not null comment '供应商'
);
//客户表
create table customer(
customer_i int primary key auto_increment comment '客户编号',
name varchar(15) not null comment '姓名',
address varchar(32) comment'地址',
email varchar(56) unique comment '邮箱',
sex varchar(2) comment'性别',
card_id char(18)unique comment '身份证'
);
//在原有的商品表good_id字段添加自增长属性(直接写要添加的属性就可以,不能在加上主键,因为它本省就是主键)
alter table goods modify goods_id int primary key auto_increment comment '商品编号';//失败,Multiple primary key defined,
alter table goods modify goods_id int auto_increment;//成功
//订单表
create table purchase(
order_id int primary key auto_increment comment '订单编号',
customer_id int comment '客户编号',
goods_id int comment '商品编号',
nums int default 0 comment'购买数量',
foreign key(customer_id)references customer(customer_i),
foreign key(goods_id)references goods(goods_id)
);
//往商品表中插入数据
insert into goods (goods_name,unitprice,category,provider)values
('牛排',28.5,'肉类','北京'),
('香蕉',3.2,'水果','上海'),
('石榴',5.3,'水果','深圳');
//往客户表中插入数据
insert into customer(name,address,email,sex,card_id) values
('唐三藏','东土大唐','tangsanzhang@datang.com','男','612732199811128977'),
('猪八戒','东土大唐','shubajian@datang.com','男','612732198211118991');
//往订单表中插入数据
insert into purchase (customer_id,goods_id,nums)values
(1,2,5),
(2,3,6);