MySQL各种类型实验

实验一:整数

-- 测试一 
create database test;-- 新建数据库,如果已经有了就不需要再创建了
USE test;-- 打开数据库
drop table if exists test1;-- 如果存在表test1则删除
create table TEST1(-- 新建表test1
a INT-- 表test1只有一个a列
);
insert into test1 values(123456);-- 向表test1中插入元素
select * from test1;-- 查询表中全部内容

注: -- 行注释减减加空格表示行注释

-- 测试二
drop table if exists test2;
create table TEST2(
a INT(11)
);
insert into test2 values(123456);
select * from test2;
-- 测试三
USE test;
drop table if exists test3;
create table TEST3(
a INT(11) zerofill
);
insert into test3 values(123456);
select * from test3;

INT后面括号的数表示显示宽度,不是占多少字节;
zerofill填充0,在Navicat中显示不出零,在控制台能正常显示并看到填充的0

2.实验二:实数

use test;
drop table if exists test4;
CREATE TABLE test4(
c1 float(10,2),-- 长度是10小数占两位小数点不占位
c2 decimal(10,2)
);

insert into test4 VALUES
(131072.32,131072.32);
insert into test4 VALUES
(131072.5678,131072.1234);

select * from test4;

decimal比较精确,当涉及到钱的时候通常用这种类型
3.时间和时间戳

use test;

drop table if exists test5;
CREATE TABLE test5(
c1 datetime,
c2 timestamp,
c3 char(10)
);

insert into test5 VALUES (now(),CURRENT_TIMESTAMP,'布朗');
-- now()计算机系统的当前时间
-- current_timestamp当前时间戳
insert into test5 VALUES ('1998-10-05 10:15:25',CURRENT_TIMESTAMP,'小布朗');

update test5 set c3='马丁·路德'where c3 = '小布朗';-- 修改命令

select * from test5;

若定义一个字段为timestamp类型,这个字段的时间数据会随着其他字段修改的时候自动刷新,所以这个数据类型的字段可以存放这条记录最后被修改的时间
4.字符串类型

-- 字符串类型测试一
use test;

drop table if exists test6;
CREATE TABLE test6(
c1 char(4),
c2 varchar(4)
)ENGINE=myisam;
-- ENGINE=myisam存储引擎

insert into test6 VALUES ('','');
insert into test6 VALUES ('ab','ab');
insert into test6 VALUES ('abcd','abcd');

select * from test6;
-- 字符串类型测试二
use test;

drop table if exists test7;
CREATE TABLE test7(
v VARCHAR(4),
c CHAR(6)
);

insert into test7 VALUES ('ab  ','ab  ');

select CONCAT(v,'+'),CONCAT(c,'+') from test7;
-- concat(,)字符串连接函数

5.复合类型
enum多选一
set多选

-- 自增长列
drop table if exists test9;
CREATE TABLE test9(
c1 int(11) AUTO_INCREMENT PRIMARY KEY,
c2 char(10)
)ENGINE = myisam,charset=gbk,AUTO_INCREMENT=10000;
insert into test9 value(null,'A');
select * from test9;

代理键
自然键

-- 布尔型
drop table if exists test10;
CREATE TABLE test10(
c1 bool,#实际等同于tinyint(1)
c2 char(10)
)ENGINE = myisam,charset=gbk;
insert into test10 value(1,'A');
select * from test10 where c1 = true;
use test;
-- 存储文件的目录?转义字符
drop table if exists test11;
CREATE TABLE test11(path VARCHAR (99));
insert into test11 values('C:\\Program Files\\MySQL Server');
select * from test11;
posted @ 2019-05-15 16:20  瑶啊摇✨  阅读(311)  评论(0编辑  收藏  举报