Basics mysql learning 03

Let's perform basic operations to build the table today:

First, we must understand to build the table column type, because the process of construction of the table is to declare column, the column is declared complete, the table also built.

mysql columns are divided into three categories:

A, numeric

Numeric integer and floating point is divided into two kinds.

First look Integer:

tinyint: footprint: 1 byte; Storage range: with the number of symbols: -2 ^ 7 (-128) ~ 2 ^ 7-1 (127), unsigned number: 0-255

smallint The: footprint: 2 bytes; Storage range: with the number of symbols: -2 ^ 15 (-32,768) ~ 2 ^ 15-1 (32,767), unsigned number: 0 to 65535

MEDIUMINT: footprint: 3 bytes; Storage range: with the number of symbols: -2 ^ 23 (-8388608) ~ 2 ^ 23-1 (8388607), unsigned number: 0 ~ 16777215

int: footprint: 4 bytes; Storage range: with the number of symbols: -2 ^ 31 (-2147483648) ~ 2 ^ 31-1 (2147483647), unsigned number: 0 to 4,294,967,295

BIGINT: Space occupying 8 bytes; Storage range: a signed: -2 ^ 63 (-9,223,372,036,854,775,808) ~ 2 ^ 63-1 (9,223,372,036,854,775,807), unsigned number: 0 ~ 18446744073709551615

When the series statement int parameters:

unsigned: unsigned int type in MySQL statement argument is unsigned, if you do not declare unsigned, the default is signed with.

(M) zerofill M zerofill mated together and only have the effect of use, i.e., the specified length of the data, then filled with enough 0

For example, we declare an id int (5) zerofill a id2 int (5):

We insert the same data to it:

We can see also 500, id 0 in front of two up, as we specify its data length is 5.

That if we insert data longer than 5 how to do?

We can see if we insert the data length exceeds a set value M, then according to the actual length of the insert.

Then we look at floating point type:

M is called precision, representing the total number of decimal places, D is called the scale, the representative number of decimal places.

float (M, D) single-precision floating-point, the space occupied: 4 bytes.

double float (M, D) double precision floating point type, the space occupied: 8 bytes.

When a floating point store are approximations exist, there is a fixed point decimal type, which is the integer and fractional parts stored separately.

Fixed point type:

decimal(M,D)

Let's look at the specific difference: we declare a num float (9,2) and a num2 decimal (9,2):

Then to insert num 1,234,567.23, to insert 1,234,567.25 num2, then we look at the results:

We found that, num bar following figures became 1,234,567.25. The reason is that when the above said float type of memory is stored approximate.

So if it comes to finance, accounts and so sensitive to the number of changes need to save time we use decimal decimal.

Second, the string

Char (M) is a fixed-length type e.g. Char (6) can store . 6 characters, the content is not enough . 6 characters with spaces filled in the tail, then when removing the right to delete all spaces.

Varchar (M) is of variable length e.g. varchar (20), can store 0 to 20 characters in length, but before the column contents are 1 to 2 bytes to the contents of the column length mark.

We declare two variables, namely the name name2, with a char, varchar with a

 

Inserting data, a trailing space Joe Smith, John Doe is also behind the spaces

Direct view, then do not come out to see a big difference, and we put it another character connected:

We can see the seating space after being deleted.

Text text type, usually used to store the contents of the article, news and other content. Disclaimer text not to default values when the column type.

Blob 二进制类型,一般用来存储图像音频等二进制信息。Blob在于防止字符集的问题导致信息丢失。比如:一张图片中有0xff字节,这个在ASCII字符集中认为非法,在入库的时候被过滤。使用blob就可以防止信息丢失。

三、日期时间型

Date 日期 存储年--日 存储范围是1000-01-01~9999-12-31

Time 时间 存储时:分:秒 存储范围是-8385959~8385959

Datetime 日期时间类型年--日 时:分:秒 范围是1000-01-01 000000~9999-12-31 235959

Year 年类型 YYYYYY(不推荐)范围是1901~2155 还能存一个特殊的0000

 

建表的语法不重要,重要的是要学会合理的选择列类型并为其声明参数。

 建表的语法:

Create table 表名 (

1 列类型 列参数,

2 列类型 列参数,

......

n 列类型 列参数

engine myisam charset utf8;

 最后一句是声明引擎和字符集的,那个我们会在后面讲到。

如果一张表建好了,我们需要增加列怎么办?

alter table 表名 add 列名 列类型 列参数

我们就给上面的test2表增加一列:

由于之前没有salary列,因此增加后原来的行在salary列的值为空。

这是默认增加在最后一列的,我们想在birth后面增加一列怎么办?

alter table 表名 add 列名 列类型 列参数 after birth;

我们想在把列增加最前面,该怎么办?

alter table 表名 add 列名 列类型 列参数 first;

删除列:

alter table 表名 drop 列名;

修改列类型:

alter table 表名 modify 列名 新列类型 列参数;

 

 我们看到这张表的结构:star是varchar(20)。

我们给它改成varchar(30):

 

修改列名:

 alter table 表名 change 旧列名 新列名 列类型 列参数;

 

Guess you like

Origin www.cnblogs.com/wanghaoyu666/p/11263241.html