1.字段类型
数值类型:
int 整数
long 长整型
float 单精度
double 双精度
decimal 钱
字符串:
char 字节 定长0-255长度 jepsonxxxxxx 自动补齐
varchar 字符串 变长0-65535字节 jepson
日期:
datetime和timestamp的区别:http://baijiahao.baidu.com/s?id=1660748223312093233&wfr=spider&for=pc
date 日期 YYYY-MM-DD
time 时间 HH:MM:SS
datetime 年月日时分秒
timestamp 年月日时分秒
2.SQL类型
ddl:数据定义语言 对应语句:create drop
dml:数据操作语言 对应语句:select insert update delete 增删改查
dcl:数据控制语言 对应语句:grant
3.建表规范
1.建表语句例子
create table ruozedata.rz(
id int(11) not null auto_increment, #第一列必须是自增长id
name varchar(255),
age int(3),
create_user varchar(255), #创建人
create_time timestamp not null default current_timestamp, #创建时间
update_user varchar(255), #更新人员
update_time timestamp not null default current_timestamp on update current_timestamp, #on update current_timestamp 此意思是只要表中任何一个字段的值发生变化,都会在变化对应的行的这一列会更新更新当前时间
primary key(id) 主键= 唯一约束 +not null非空约束
);
2.规范
1.建表的第一个字段必须是自增长字段 主键,且 无业务意义 架构设计默认规则
2.表名称 字段名称 不要写中文 尽量不要汉语拼音
3.统一风格:已经存在表结构设计,风格比如是create_user。
4.一张表只有一个主键 id,业务字段需要唯一的话,就使用唯一约束来保证。
ALTER TABLE ruozedata.rz
ADD CONSTRAINT rz_un UNIQUE KEY (name);
5.如上建表语句例子中最后四个字段注释务必加上
comment 'xxx'
4.增删改查
insert into ruozedata.rz(name,age) values('ruoze',18);
update ruozedata.rz set age=38 where name='ruoze';
select * from ruozedata.rz;
delete from ruozedata.rz where id=1;
5.其他语法
drop table ruozedata.rz;
create table ruozedata.rz(
id int(11) not null auto_increment,
name varchar(255) comment '姓名',
age int(3) comment '年龄',
create_user varchar(255),
create_time timestamp not null default current_timestamp,
update_user varchar(255),
update_time timestamp not null default current_timestamp on update current_timestamp,
primary key(id)
);
insert into ruozedata.rz(name,age) values('ruoze',18);
insert into ruozedata.rz(name,age) values('jepson',16);
update ruozedata.rz set age=38 where name='ruoze';
insert into ruozedata.rz(name,age,create_user,update_user ) values('ruoze',18,'xingxing','xingxing');
insert into ruozedata.rz(name,age) values('jepson',16);
insert into ruozedata.rz(name,age) values('xingxing',14);
insert into ruozedata.rz(name,age) values('xiao17',12);
insert into ruozedata.rz(name,age) values('jepson',22);
insert into ruozedata.rz(name,age) values('jfpson',22);
1.过滤 where
select * from ruozedata.rz where name='ruoze';
select * from ruozedata.rz where name='jepson' and age=22;
select * from ruozedata.rz where age=16 or age=12;
select * from ruozedata.rz where age in (16,12);
select * from ruozedata.rz a where exists (
select b.id from ruozedata.rz b
where (age=16 or age=12 ) and a.id=b.id
);
2.排序语法 order by
select * from ruozedata.rz order by age;
select * from ruozedata.rz order by age asc;
select * from ruozedata.rz order by age desc , name desc ;
3.模糊匹配 like
select * from ruozedata.rz where name like 'j%'; 字母j开头
select * from ruozedata.rz where name like '%n'; 字母n开头
select * from ruozedata.rz where name like '%o%'; 含有o
select * from ruozedata.rz where name like '__p%'; 第三位p #_ 占位符
4.合并表
create table t1(id int,name varchar(255));
create table t2(id int,name varchar(255));
insert into t1 values(1,'ruoze');
insert into t2 values(1,'ruoze');
insert into t2 values(2,'jepson');
过滤重复
select * from t1
union
select * from t2;
不过滤重复
select * from t1
union all
select * from t2;
5.分组语法 group by …having …
分组/聚合函数有:sum avg max min count
1.不带分组字段的查询:
select
sum(age),avg(age),max(age),min(age),count(*)
from ruozedata.rz ;
2.带分组字段的查询
select
name,
sum(age),count(1)
from ruozedata.rz
group by name
having sum(age)>18;
3.等价于子查询进行筛选
select * from
(
select
name,
sum(age) as sum_age,count(1) as c
from ruozedata.rz
group by name
) as t where t.sum_age>18;