MySQL_02

来源:互联网 发布:软件开发工作流程图 编辑:程序博客网 时间:2024/06/08 13:36

12,增加一条数据到表msg

      insert into msg(id,title,name,content)values

(1,'初来咋到','张三','刚来不能当老大?');(按照例子多增加几条记录。)

13,设置接收字符集:set names gbk;

14,更新一条记录:update msg set id=3,content='偏要当老大' where id=1;

15,删除一条记录:delete from msg where id=3;where子句中可以是其他属性)

16,查询一条记录:select name from msg where id>1;

 

数据类型:

整形:tinyint<smallint<mediumint<int<bigint

(字节:1           2            3      4    8 

小数型:float(M,D)decimal(M,D) (M总位数,D小数位)

字符串型:char(M)varchar(M)text文本类型M表示可容纳的最大字符数

日期类型:date日期,time时间,datetime日期时间,year

除整形、小数型,其他类型在赋值时一般加单引号 

 

:

create table class(

 id int primary key auto_increment,

 name varchar(10),

 age tinyint                 

 )charset utf8;------

------------------------------------------------

insert into class

(name,age) values

('张三',25);

insert into class

(name,age) values

('李四',127);

------------------------------------------------

tinyint,默认有符号,范围-128 ~ 127

-----------------------------------------------

alter table class add age2 tinyint unsigned;

insert into class (name,age,age2) values

('wangwu',25,255);

-----------------------------------------------

tinyint(M) zerofill

zerofill0填充,M为总位数(包含数据),此属性自动指定unsigned

alter table class add age4 tinyint(5) zerofill;

insert into class (name,age4) values ('zhaoliu',25);

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

| name  | age4 |

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

| zhaoliu | 00025 |

------------------------------------------------

声明默认值:default 非空:not nulll

alter table class add age5 tinyint not null default 0;

--------------------------------------------

小数表示:

float(M,D) (M表示总位数,D表示小数位)(五省六入)

create table goods(

name varchar(10) not null default '',

price float(6,2)

);

insert into goods values ('第一重',22.3);

insert into goods (name,price)

 values('跑步机',688.896);

|第一重 | 22.30 |

|跑步机 | 688.90 |

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

decimal(M,D),精度更高

alter table goods add bigprice float(9,2) not null default 0.0,

alter table goods add deciprice decimal(9,2) not null default 0.0;

insert into goods (name,bigprice,deciprice)

values ('自行车',1234567.23,1234567.23);

|自行车 | NULL | 1234567.25 | 1234567.23 |

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

create table stu(

name char(8) not null default '',

waihao varchar(10) not null default ''

)charset utf8;

insert into stu

(name,waihao)

values

('zhangsan','saner11111');

insert into stu

(name,waihao)

values

('可以存放八个字符','是字符数,不是字节数');

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

| name            | waihao               |

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

| zhangsan        | saner11111           |

|可以存放八个字符 |是字符数,不是字节数 |

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

UTF-8字符集时可以存放8个汉字,gbk时可以存放4个汉字

char(M):可存字符数M,实占空间M0<=M<=255,不够M字符时内部用空格补全

             存储数据尾部有空格时,取出时会被清除

varchar(M):可存字符数M,实占空间i + (1~2)字符,0<=M<=(ascii 65535,utf8 22000)

charvarchar选取的原则:

char存取速度较varchar

1,空间利用效率2,速度要求

text:不能加默认值,不能全文索引,速度稍慢,尽量使用charvarchar替代

-----------------------------------------------------------

日期时间类型

   year  1字节 1901~2155年,【0000,表示错误时选择】

-- date

09:00:00   time

-- hh:mm:ss  datetime

-------------------------------------

create table y(

ya year(4)

);

insert into y values ('1901');

insert into y values ('97');

insert into y values ('12');

+------+

| ya  |

+------+

| 1901 |

| 1997 |

| 2012 |

+------+

如果只输入两位,‘00-69’表示2000-206970-99’表示1970-1999

----------------------------------------------------------

日期类型:date典型格式:1992-08-12范围:1000-01-01 ~ 9999-12-31

create table d (

title varchar(30),

dt date

)charset utf8;

insert into d values ('开过大典','1949-10-01');

--------------------------------------------

时间类型:time典型格式:hh:mm:ss  范围:-838.:59:59 ~ 838:59:59

create table t(tm time);

insert into t values ('13:34:56');

------------------------------------------------

日期时间类型:datetime典型格式:1989-05-06 14:33:21

范围:1000-01-01 00:00:00 ~ 9999-12-31 23:59:59

create table user(

name varchar(20) not null default '',

regtime datetime not null default '1000-01-01 00:00:00'

)charset utf8;

insert into user values ('李四','2012-03-22 14:28:36');

开发中很少用日期时间类型来一个精确到秒的列:

虽然日期时间类型能精确到秒,并且方便查看,不方便日期时间的计算

用时间戳来表示:是1970-01-01 00::00:00到当前的秒数(用int存储)(调用具体编程语言的函数处理)

---------------------------------------------

枚举类型:enum

create table t2(

gender enum('','')

)charset utf8;

insert into t2 values ('');

 

 

原创粉丝点击