database --》数据库 (相当于电脑中的文件夹)
table --》 表 (相当于电脑中的excel表格)
table
1.列(column )2.列的类型(type)
字段类型
(1)数值类型
1.int 整型 2.long 长整型 3.float 单精度 4.double 双精度 5.decimal 小数值 (与钱有关的)
(2)字符串类型
1.char 字节 (长度 0-255 )2.varchar 字符串 ( 范围变大 0-65535)3.text
(3)日期
1.date 日期(示例 YYYY-MM-DD) 2.time 时间(示例 HH:MM:SS )
3.datetime 年月日时分秒 (示例 YYYY-MM-DD HH:MM:SS)
4.timestamp 年月日时分秒(示例 YYYY-MM-DD HH:MM:SS)
sql类型
1.ddl 数据定义语言:create drop alter
2.dml 数据操作语言: select(增加) insert(删除) update(查询) delete(修改)
3.dcl 数据控制语言: grant 【不需要掌握】
sql基本语法
1.创建数据库
create database xxxx(数据库名称自定义);
Mysql语法:
database --》数据库 (相当于电脑中的文件夹)
table --》 表 (相当于电脑中的excel表格)
table
1.列(column )2.列的类型(type)
字段类型
(1)数值类型
1.int 整型 2.long 长整型 3.float 单精度 4.double 双精度 5.decimal 小数值 (与钱有关的)
(2)字符串类型
1.char 字节 (长度 0-255 )2.varchar 字符串 ( 范围变大 0-65535)3.text
(3)日期
1.date 日期(示例 YYYY-MM-DD) 2.time 时间(示例 HH:MM:SS )
3.datetime 年月日时分秒 (示例 YYYY-MM-DD HH:MM:SS)
4.timestamp 年月日时分秒(示例 YYYY-MM-DD HH:MM:SS)
sql类型
1.ddl 数据定义语言:create drop alter
2.dml 数据操作语言: select(增加) insert(删除) update(查询) delete(修改)
3.dcl 数据控制语言: grant 【不需要掌握】
sql基本语法
1.创建数据库
create database xxxx(数据库名称自定义);
Mysql语法:
database --》数据库 (相当于电脑中的文件夹)
table --》 表 (相当于电脑中的excel表格)
table
1.列(column )2.列的类型(type)
字段类型
(1)数值类型
1.int 整型 2.long 长整型 3.float 单精度 4.double 双精度 5.decimal 小数值 (与钱有关的)
(2)字符串类型
1.char 字节 (长度 0-255 )2.varchar 字符串 ( 范围变大 0-65535)3.text
(3)日期
1.date 日期(示例 YYYY-MM-DD) 2.time 时间(示例 HH:MM:SS )
3.datetime 年月日时分秒 (示例 YYYY-MM-DD HH:MM:SS)
4.timestamp 年月日时分秒(示例 YYYY-MM-DD HH:MM:SS)
sql类型
1.ddl 数据定义语言:create drop alter
2.dml 数据操作语言: select(增加) insert(删除) update(查询) delete(修改)
3.dcl 数据控制语言: grant 【不需要掌握】
sql基本语法
1.创建数据库
create database xxxx(数据库名称自定义);
2.查看数据库
show databases;
3.切换数据库
use XXXX(数据库名称);
4.创建表
create table xxxxx(表名,自定义)(参数名称1 参数类型1,........)
create table student(id int(11) NOT NULL AUTO_INCREMENT,
name varchar(20) COMMENT '姓名',
age int(3),
create_user varchar(20), 操作者姓名
create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP(默认值),
创建时间 的时间戳
update_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP(默认值,
修改时间的时间戳
PRIMARY KEY(id) );
2.查看数据库
show databases;
3.切换数据库
use XXXX(数据库名称);
4.创建表
create table xxxxx(表名,自定义)(参数名称1 参数类型1,........)
create table student(id int(11) NOT NULL AUTO_INCREMENT,
name varchar(20) COMMENT '姓名',
age int(3),
create_user varchar(20), 操作者姓名
create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP(默认值),
创建时间 的时间戳
update_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP(默认值,
修改时间的时间戳
PRIMARY KEY(id) );
2.查看数据库
show databases;
3.切换数据库
use XXXX(数据库名称);
4.创建表
create table xxxxx(表名,自定义)(参数名称1 参数类型1,........)
create table student(id int(11) NOT NULL AUTO_INCREMENT,
name varchar(20) COMMENT '姓名',
age int(3),
create_user varchar(20), 操作者姓名
create_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP(默认值),
创建时间 的时间戳
update_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP(默认值,
修改时间的时间戳
PRIMARY KEY(id) );
注意:
1.表名称 一定要写英文
2.建表风格
3.第一字段 使用自增主键 【本身没有任何业务意义】
4.字段 要加上注释
5.插入数据
insert into xxxxx (想要插入数据的表的名字) (参数类型1,参数类型2) values(输入相对应的数据)
插入多个数据时用逗号分隔
insert into zjs0427.student(name,age) VALUES ('甲',18);//插入一条数据
insert into zjs0427.student(name,age) VALUES ('乙',18),("bing",38),("ding",40);// 插入多条数据
6.查看表中数据
select *(*代表中所有的字段) from zjs0427.student;
光查询name和age的两个数据字段
select name,age from zjs0427.student;
7.update 更改数据 【 注意:是否要加 where 】
update xxxxxx(更改数据的表的名字)set xxx(更改后的数值)where xxxxx(过滤的目标或者更改的数值的目标)
示例
update zjs0427.student set age=28 where name='ding';
注意 不加过滤 会直接把age列所有数值全改目标数值。
update bigdata.student_info set age=20 ;
8.delete 语句 删除某条数据 【 注意:是否要加 where 】
delete from xxxx(删除数据的表) where xxxx(过滤,限制删除条件);
示例
delete from zjs0427.student where name='甲';
9.其他语法
(1.)where 【过滤条件】
where column expresion
1.> < = and or in not in
过滤增加
insert into zjs0427.student values ('甲',18);
过滤查询
select * from zjs0427.student where age <20;(查询表中的age<20的数据)
select * from zjs0427.student where name='甲' and(and可以理解为并且) age=18;(查询表中name=甲并且age=18的数据)
select * from zjs0427.student where name='甲' or(or可以理解为或者) age=18;(查询表中name=甲或者age=18的数据)
(2.)order by 排序语法
select * from xxxxx(想要排序的表) order by xxxx(需要排序的参数) asc;默认(asc)升序
降序是 desc
示例
select * from zjs0427.student order by age asc;
select * from xxxxx order by age desc,name asc;
如果年龄降序 年龄相等 按照名字升序 默认26个英文字母顺序
(3.)like 语法 (regexp)
模糊查询 :
1.% 模糊
2._ 占位符
select * from zjs0427.student where name like "%i%"; (查询这个表中name带i的数据)
select * from zjs0427.student where name like "d_%";(查询这个表中以d开头的数据)
(4.)合并表
union 去除重复数据
union all 不去除重复数据
create table a0427(id int,name varchar(20));
create table b0427(id int,name varchar(20));
创建两个新表并输入数据
insert into a0427 values(1,'qq');
insert into b0427 values(1,'qq');
insert into b0427 values(2,'wx');
select * from a0427 union select * from b0427;
select * from a0427 union all select * from b0427;
(5).null 语法
insert into bigdata.student_info(name,age,create_user) VALUES ('lb',30,"zsdnr");
1.过滤空值 :
null
''
'null'
is null :
select * from xxxxx(表的名字) where create_user is null;
is not null :
2.处理空值 : etl 数据清洗
null =》
1.数值类型 0
2.字符串 unknown --
select *处理空值的函数(create_user) as create_user_etl from xxxxx(表的名字);
2.处理空值 : etl 数据清洗
null =》
1.数值类型 0
2.字符串 unknown 、 --
示例
select *,处理空值的函数(create_user) as create_user_etl from xxxxx(表的名字);
处理空值的函数:
ifnull
coalesce
select *,ifnull(create_user,'--') as create_user_etl from student;
、
select name,age,create_user,coalesce(create_user,'--') as create_user_etl from student;
(6.)聚合函数(分组的语法)
1.分组 : group by xxx,... [以谁进行分组]
2.聚合函数 sum,avg, max, min, count
1.不加分组 =》 全表 =》聚合函数 =》 指标
平均年龄
select avg(age) as avg_age from student;
2.分组+ 聚合函数 相同数据的和求平均数
select name, avg(age) as avg_age from student group by name;