mysql笔记 二 字段类型

来源:互联网 发布:vector与数组的区别 编辑:程序博客网 时间:2024/05/11 12:50

类型

整型

   tinyint        0 -- 255  -128---127

   smallint       

   mediumint

   int

   bigint   

小数型

   float

   decimal

字符串类型

   varchar

   char 

   text

日期型

    date

datetime 

time

year

类型

字节

占几位

无符号

有符号

Tinyint

8

0->2^8-1    255  3

-2^7->2^7-1    -128--127

Smallint

2

16

0->2^16-1    65535 5

-2^15->2^15-1  -32768 -32767

Mediumint

3

24

0->2^24-1  16777215  8

-2^23->2^23-1  -8388608-8388607

Int

4

32

0->232-1  4294967295  十位

-2^31->2^31 -1  ~2147483648  十忆

Bigint

8

64

184467440 ~20

9223372036  ~ 19

Tinyint  创建之后 默认是有符号的 -128-127

整形可选属性

   M:宽度  zerofill填充0才有意义

   Unsigned 无符号类型

   Zerofill   0填充 默认无符号

浮点型

Float(M,D)          浮点型

Decimal(M,D)   9999.99 ---    -9999.99   定点型  更精确

固定占 4个字节 要么是8个字节

  

M  精度 不包含小数点的总长度

D  标度 小数点后面的长度

字符型

Char  定长 (M) 代表可容纳的字符数

Varchar  变长 (M) 代表可容纳的字符数

定长 如果小于M个字符 实占M个字符

变长  如果小于M  那就存N个字节 N<=M 实占N个字节加上1~2个字节 2个字节标志 可以写到 65535 (以 ascii 字符可以65535  如果gbk utf8的话22000左右,如果是5.0.3以下版本 只能存255个字节)

Char  定长类型 如果少于长度 用空格补齐  如果尾部是空格 那么就丢失了

Text  能存很大的数据  但是搜索很慢  而且 text无法加默认值

日期和时间

年  year  1个字节表示  1901  -2155  还少了一个字节 预留的 000000

年月日  date

09:00:00   time 

年月日 时分秒  datetime

Year  如果输入2位的话 ‘00-69’ 那么表示 2000-2069

如果2位是‘70-99’ 表示 1970--1999

Date 类型  1908-01-23   1000-01-01  ~~~ 9999-12-31  

Time  时间类型  hh:mm:ss  -838:59:59  ~~~  838:59:59 还可以用来表示2个事件的时间间隔

Datetime 日期时间类型  1000-01-01 00:00:00  ~~~   9999-12-31 23:59:59 

时间戳 

1970-01-01 0000:00 到当前秒数

一般发布商品 用时间戳 不用datetime 因为datetime虽然直观但是计算不便 

建表语句

Create table  表名 (

1  列属性 默认值,

2  列属性 默认值,

3  列属性 默认值

Engine  =  存储引擎

Charrset  =  编码

create  table php (

id int primary key auto_increment,

name char(3) not null default '',

age tinyint  unsigned  not null default 0,

email varchar(30) not null default '',

tel char(11) not null default '',

salary decimal(7,2) not null default '1800.68',

riqi  date not null default '2012-03-01'

)charset utf8;

原创粉丝点击