MySQL语法

database(DB):表、视图、索引、存储过程、触发器、events
在大数据学习使用过程中,主要掌握前三个即可

1.字段类型——用来规范数据

  • 数值型
    int 整数
    long 长整数
    float 单精度
    double 双精度
    decimal 小数值 跟钱挂钩的
  • 字符串类型 ‘abc’
    char 定长字符串 0-255字节 abc–》abc255 自动补全,造成资源浪费
    varchar 变长字符串 0-65535字节 abc–》abc
    text
  • 日期和时间
    date YYYY-MM-DD 2019-09-10
    time HH:MM:SS 10:10:10
    datetime 2019-09-10 10:10:10
    timestamp 2019-09-10 10:10:10

2.SQL分类:

  • DDL: 数据定义语言 create drop(删除表) alter(修改字段)
  • DML: 数据操作语言 insert update delete select
  • DCL: 数据控制语言 grant (赋权限)

3.创建表

create table rzdata(
id int ,
name varchar(200),
age int 
)

建表规则:

  • 开头:第一个字段一定要是自增长的、无意义的、非业务的字段
    保证数据行是按顺序写入,对于根据主键做关联操作的性能也会更好
  • 结尾:创建时间、创建人、修改时间、修改人
  • 规范:统一风格
  • 千万不要用汉语拼音或首字母做字段名称
  • 千万不要用中文做字段名称

4.增删改查

#在表rzdata中插入一条数据:
insert into rzdata(id,name,age) values(1,'rz',18)
#更新数据:
update rzdata set age=22 where id=1;
#删除数据;
delete from rzdata where id=1;
#查询:
select * from rzdata;

案例:创建一张学生表

create table studentinfo(
id int auto_increment primary key,

num int ,
name varchar(200),
age int ,

create_time timestamp default current_timestamp,
create_user varchar(100),
update_time timestamp default current_timestamp on update current_timestamp,
update_user varchar(100)
);

主键:primary key

  • 一张表只能有1个主键
  • 主键=非空约束(not null default xxx)+唯一约束
  • 主键是唯一约束的表态升级
  • 主键或唯一约束,可以根据数据特性,进行多个字段设置

insert into ruozedata.studentinfo(num,name,age)
values(1,'jepson',16);
update ruozedata.studentinfo set age=28 where num=1;
select * from ruozedata.studentinfo;

插入、更新后得到结果分别如下:
id num name age create_time cretae_user update_time update_user
1 1 jepson 16 2019-06-26 22:14:19 2019-06-26 22:14:19
1 1 jepson 28 2019-06-26 22:14:19 2019-06-26 22:17:58

insert into ruozedata.studentinfo
values(3,3,'huhu',16,'2019-06-26 22:20:10','r','2019-06-26 22:20:10','r');

注意:插入时如果没有用括号指定要插入的列的名称,那么就默认插入的values值要包含所有的字段,包括主键id;插入时最好将列名称写上,与字段名称一一对应,不会出错。

生产上,update,切记要确认是否要加where
update ruozedata.studentinfo set age=19;
也可以一次更新多列:
update ruozedata.studentinfo set name=‘huhu2’,age=19 where id=3;

生产上,delete,切记要确认是否要加where
delete from ruozedata.studentinfo where id=3;

假设有两个系统,都能创建订单号,分别为系统一:system1,订单号001;系统二:system2,订单号001

  • MySQL分库
    wmsb01.t (id,orderno) id自增长主键(加非空),非业务性
    1 100
    wmsb02.t (id,orderno)
    1 100
  • 大数据平台
    wms.t(id,orderno) 主键 : id
    1 100
    由于数据是在MySQL数据库产生而不是在大数据平台产生的,此时,如果主键只设id一个字段的话,那么第二条数据一定进不来,故再加另一个字段
    wms.t(warehouse_no,id,orderno) 联合主键 :id,warehouse_no
    b01 1 100
    b02 1 100

5.查询

  • 按条件查询:> < = !=
select * from ruozedata.studentinfo where id>1;
  • 或 or ,且、和 and
select * from ruozedata.studentinfo where age=26 or name='ruoze1';
select * from ruozedata.studentinfo where age=26 and name='ruoze1';
select * from ruozedata.studentinfo where age=26 and (name='ruoze1' or name='jepson');
  • 模糊查询:
    Linux:find /-name ‘abc
    MySQL:
select * from ruozedata.studentinfo where name like '%r%';
#首字母为ruo:
select * from ruozedata.studentinfo where name like 'ruo%';
#最后字母确定为1:
select * from ruozedata.studentinfo where name like '%1';
#第三个字母为o:(占位符)
select * from ruozedata.studentinfo where name like '__o%';
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值