mysql移动记录指针_Mysql之库、表、记录相关操作1

Mysql之库、表、记录相关操作4

创建表完整语法

#语法:

create table 表名(

字段名1 类型[(宽度) 约束条件],

字段名2 类型[(宽度) 约束条件],

字段名3 类型[(宽度) 约束条件]

)engine=innodb charset=utf8;

#注意:

1. 在同一张表中,字段名是不能相同

2. 宽度和约束条件可选

3. 字段名和类型是必须的

4. []可选参数

# create table db1.t1(name char(3) not null);

# 数据插入时,name不能为空(null), 且最长只能存放三个字符

# 总结: 宽度和约束条件为可选参数, 用来限制存放数据的规则

数据库的模式

# sql_mode :反应数据库的全局变量

# 数据库模式限制的是客户端对服务器操作数据的方式(是否严格)

# 两种模式

no_engine_substitution:非安全性,默认

strict_trans_tables:安全模式

# 查看当前数据库模式

show variables like "%sql_models%";# 匹配0~n个人以字符=>模糊查询

# 设置安全模式

set global sql_mode="strict_trans_tables";

# 重启客户端

quit

数据类型

mysql 数据库支持存放的数据类型:

整型|浮点型|字符型|时间类型|枚举类型|集合类型

一、整型

整型类型:tinyint|smallint|mediumint|int|bigint

作用:存储年龄、等级、id、号码等

_____________________________________________________________________________

tinyint(1字节)

tinyint[(m)] [unsigned] [zerofill]

小整数,用于保存一些范围的整数数值范围:

有符号:-128~127

无符号:0~128

PS:Mysql中无布尔值,使用tinyint(1)构造~~~?

—————————————————————————————————————————————————————————————————————————————

int(4字节)

int[(m)][unsigned][zerofill]

整数,用于保存一些范围的整数数值范围:

有符号:-2147483648~2147483647

无符号:0~4294967295

—————————————————————————————————————————————————————————————————————————————

bigint(8字节)

bigint[(m)][unsigned][zerofill]

大整数,数据类型用于保存一些范围的整数数值范围:

有符号:-9223372036854775808~9223372036854775807

无符号:0~18446744073709551615

'''

约束条件:

undigned:无符号

zerofill:0填充

'''

不同类型所占字节数不一样,决定所占空间及存放数据的大小限制

create table t8(x tinyint);

insert into t8 values(200); # 非安全模式存入,值只能到最大值127

select (x) from t8;

'''

宽度:

1、不能决定整型存档数据的宽度,超过宽度可以存放,最终由数据类型所占字节决定

2、如果没有超过宽度,且有zerofill限制,会用0填充前置的不足位

3、没有必要规定整型的宽度,默认宽度即为整型能存放数据最大宽度

'''

# eg:1

create table t9(x int(5));

insert into t9 values(123456);

select (x) from t9; # 结果: 123456

insert into t9 values(2147483648);

select (x) from t9; # 结果: 2147483647

insert into t9 values(10);

select (x) from t9; # 结果: 10

# eg:2

create table t10(x int(5) unsigned zerofill); # 区域0~4294967295

insert into t10 values(10);

select x from t10; # 结果: 00010

insert into t10 values(12345678900);

select x from t10; # 结果: 4294967295

# 练习

=========有符号和无符号tinyint==========

#tinyint默认为有符号

MariaDB [db1]> create table t1(x tinyint); #默认为有符号,即数字前有正负号

MariaDB [db1]> desc t1;

MariaDB [db1]> insert into t1 values

-> (-129),

-> (-128),

-> (127),

-> (128);

MariaDB [db1]> select * from t1;

+------+

| x |

+------+

| -128 | #-129存成了-128

| -128 | #有符号,最小值为-128

| 127 | #有符号,最大值127

| 127 | #128存成了127

+------+

#设置无符号tinyint

MariaDB [db1]> create table t2(x tinyint unsigned);

MariaDB [db1]> insert into t2 values

-> (-1),

-> (0),

-> (255),

-> (256);

MariaDB [db1]> select * from t2;

+------+

| x |

+------+

| 0 | -1存成了0

| 0 | #无符号,最小值为0

| 255 | #无符号,最大值为255

| 255 | #256存成了255

+------+

============有符号和无符号int=============

#int默认为有符号

MariaDB [db1]> create table t3(x int); #默认为有符号整数

MariaDB [db1]> insert into t3 values

-> (-2147483649),

-> (-2147483648),

-> (2147483647),

-> (2147483648);

MariaDB [db1]> select * from t3;

+-------------+

| x |

+-------------+

| -2147483648 | #-2147483649存成了-2147483648

| -2147483648 | #有符号,最小值为-2147483648

| 2147483647 | #有符号,最大值为2147483647

| 2147483647 | #2147483648存成了2147483647

+-------------+

#设置无符号int

MariaDB [db1]> create table t4(x int unsigned);

MariaDB [db1]> insert into t4 values

-> (-1),

-> (0),

-> (4294967295),

-> (4294967296);

MariaDB [db1]> select * from t4;

+------------+

| x |

+------------+

| 0 | #-1存成了0

| 0 | #无符号,最小值为0

| 4294967295 | #无符号,最大值为4294967295

| 4294967295 | #4294967296存成了4294967295

+------------+

==============有符号和无符号bigint=============

MariaDB [db1]> create table t6(x bigint);

MariaDB [db1]> insert into t5 values

-> (-9223372036854775809),

-> (-9223372036854775808),

-> (9223372036854775807),

-> (9223372036854775808);

MariaDB [db1]> select * from t5;

+----------------------+

| x |

+----------------------+

| -9223372036854775808 |

| -9223372036854775808 |

| 9223372036854775807 |

| 9223372036854775807 |

+----------------------+

MariaDB [db1]> create table t6(x bigint unsigned);

MariaDB [db1]> insert into t6 values

-> (-1),

-> (0),

-> (18446744073709551615),

-> (18446744073709551616);

MariaDB [db1]> select * from t6;

+----------------------+

| x |

+----------------------+

| 0 |

| 0 |

| 18446744073709551615 |

| 18446744073709551615 |

+----------------------+

======用zerofill测试整数类型的显示宽度=============

MariaDB [db1]> create table t7(x int(3) zerofill);

MariaDB [db1]> insert into t7 values

-> (1),

-> (11),

-> (111),

-> (1111);

MariaDB [db1]> select * from t7;

+------+

| x |

+------+

| 001 |

| 011 |

| 111 |

| 1111 | #超过宽度限制仍然可以存

+------+

# 注意:为该类型指定宽度时,仅仅只是指定查询结果的显示宽度,与存储范围无关,存储范围如下

761613f67fcf6b5bb7a810822f736cbb.png

'''

int的存储宽度是4个Bytes,即32个bit,即2**32

无符号最大值为:4294967296-1

有符号最大值:2147483648-1

有符号和无符号的最大数字需要的显示宽度均为10,而针对有符号的最小值则需要11位才能显示完全,所以int类型默认的显示宽度为11是非常合理的

最后:整形类型,其实没有必要指定显示宽度,使用默认的就ok

'''

二、浮点型

浮点类型:float|double

作用:存储薪资、身高、体重、参数等

======================================

#FLOAT[(M,D)] [UNSIGNED] [ZEROFILL]

定义:

单精度浮点数(非准确小数值),m是数字总个数,d是小数点后个数。m最大值为255,d最大值为30

有符号:

-3.402823466E+38 to -1.175494351E-38,

1.175494351E-38 to 3.402823466E+38

无符号:

1.175494351E-38 to 3.402823466E+38

精确度:

**** 随着小数的增多,精度变得不准确 ****

=====

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值