1.sql类型
ddl 数据定义语言 create drop
dml 数据操作语言 select insert update delete 增删改查
dcl 数据控制语言 grant
count(1) 1表示第一个字段
create table ruoze(
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,
primary key(id) 主键 = 唯一约束+非空约束
) DEFAULT CHARSET=utf8;
1 jj 18 2020-04-26 21:25:16.0 2020-04-26 21:25:16.0
1 jj 33 2020-04-26 21:25:16.0 2020-04-26 21:27:03.0
insert into ruozedata.ruoze(name,age) values('jj',18);
insert into ruozedata.ruoze(name,age) values('rz',37);
insert into ruozedata.ruoze(name,age) values('huhu',19);
update ruozedata.ruoze set age=33 where name='jj';
delete from ruozedata.ruoze where id=3;
select * from ruozedata.ruoze;
注意点:
1.表名称 字段名称 不要写中文 不要汉语拼音
2.统一风格:
已经存在表结构设计,风格比如是create_user
那么新建的表也遵循。或者你不遵循设计,你的建表语句让你的leader check。
假如不存在,那么你应该建议设计规则
创建时间:
ctime
cretime
createtime
cre_time
create_time
订单号:
order
orderno
orderid
3.第一个字段必须是自增长字段 主键 无业务意义 架构设计的遵循
4.一张表只有一个主键 primary key(id)
业务数据 唯一约束
ALTER TABLE ruozedata.ruoze ADD CONSTRAINT
ruoze_un UNIQUE KEY (name);
Duplicate entry 'jj' for key 'ruoze_un'
5.后面四个字段 务必要加
6.业务字段 务必注释加上
comment 'xxxx'
3.增删改查
insert into ruozedata.ruoze(name,age) values('jj',18);
insert into ruozedata.ruoze(name,age) values('rz',37);
insert into ruozedata.ruoze(name,age) values('huhu',19);
Column count doesn't match value count at row 1
insert into ruozedata.ruoze
values(1,'jj',23,'j1','2020-12-12 00:00:00','j1','2020-12-12 00:00:00');
update ruozedata.ruoze set age=33 where name='jj';
update ruozedata.ruoze set age=33,name='小丽' where id=1;
delete from ruozedata.ruoze;
select * from ruozedata.ruoze;
update delete select语法慎重执行,仔细思考是否要加where过滤筛选
Incorrect string value: '\xE5\xB0\x8F\xE4\xB8\xBD' for column 'name' at row 1
select * from db.t;
select name,age from db.t;
* 代表所有字段
4.其他操作符
where语法
select * from ruozedata.ruoze where age <=18;
select * from ruozedata.ruoze where name ='jj';
select * from ruozedata.ruoze where age >=18 and name='jj';
select * from ruozedata.ruoze where age >=18 or name='jj';
select * from ruozedata.ruoze where name in ('jj','huhu');
select * from ruozedata.ruoze where name not in ('jj','huhu');
排序语法
select * from ruozedata.ruoze order by age; 默认是递增 asc
select * from ruozedata.ruoze order by age asc;
select * from ruozedata.ruoze order by age desc;
like语法
select * from ruozedata.ruoze where name like 'j%'; 字母开头为j的数据行
select * from ruozedata.ruoze where name like '%u'; 字母结尾为u的数据行
select * from ruozedata.ruoze where name like '%h%'; 含有h的数据行 不关心位置
select * from ruozedata.ruoze where name like '__h%'; 查第3个字母是h的数据
_ 占位符
合并表
create table a(i int,name varchar(255));
create table b(i int,name varchar(255));
insert into a values(1,'rz');
insert into b values(1,'rz');
insert into b values(2,'jj');
select * from a
union all
select * from b
select * from a
union
select * from b
null语法
select * from ruozedata.ruoze where age is null;
select * from ruozedata.ruoze where age is not null;
5.分组语法
分组/聚合函数:
sum
avg
max
min
count
select
sum(age) as age_sum,
avg(age),
max(age),
min(age),
count(1)
from ruozedata.ruoze ;
select
name,
ifnull(sum(age),0) as age_sum
from ruozedata.ruoze
group by name
having ifnull(sum(age),0) >20;
子查询
select
*
from
(
select
name,
ifnull(sum(age),0) as age_sum
from ruozedata.ruoze
group by name
) t where age_sum>20;
select
name,xxx,
ifnull(sum(age),0) as age_sum
from ruozedata.ruoze
group by name ,xxx
having ifnull(sum(age),0) >20;
分组字段必须select出现和group 出现是一致的