MySQL

sql语句

'''库语句'''
show databases;  # 查看所有库
show create database 库名;   # 查看具体库的信息
create database 库名; #增加库
alter database 库名 charset='uff8';   修改库
drop database 库名; # 删除库
'''表语句'''
show tables;  # 查看库内的所有表格
desc 表名;      # 查看表的结构
show create table 表名;  # 查看表结构、索引,字符编码
create table 表名(id int,name varchar(10));  # 增加表
alter table 旧表名 rename 新表名;   # 修改表名
drop table 表名; # 删除表
'''记录语句'''
select * from 表名  # 查看表记录
insert into 表名 values(记录1),(记录2),(记录3),(记录4) # 全字段多条记录增加
insert into 表名(记录信息); # 全字段单条记录增加
insert into 表名(id,name) values(1,'tom'); # 部分字段添加需指定字段名
updata 表名 set name='tom' where id=2 # 改记录
'''写更新语句的时候,千万检查要有where条件,如果没有where条件,影响的是全表'''
delete from 表名 where id=1; # 指定条件删除
delete from 表名; # 删除表内所有记录

其他SQL语句

# 修改表名
	alter table 表名  rename 新表名;

# 增加字段
	alter table 表名
add 字段名 数据类型 [完整性约束条件…];
	alter table 表名
add 字段名 数据类型 [完整性约束条件…] first;  'first 表示将新字段添加到表的第一列'
    alter table 表名
add 字段名 数据类型 [完整性约束条件…] after 字段名;
'after 指定新字段的位置name指在哪个字段后 alter table 表名 add age int after name;'

# 删除字段
	alter table 表名 drop 字段名;

# 修改字段
	'modify只能改字段数据类型完整约束,不能改字段名,change可以'
		modify
			alter table 表名 modify 字段名 新数据类型[完整性约束条件…];
		change
			alter table 表名 change 旧字段名 新字段名 数据类型 [完整性约束条件…];

MySQL基本数据类型

'''整型'''
tinyint
	# 1个字节 ---> 11111111  ----> 0-255  -> -128~127
smallint
	#	2个字节 ---->16位  ------> 0-32 768
int
	#	4个字节
bigint
	#	8个字节(存储量非常大)

'''浮点型'''
float 
  #	create table 表名 (id float(255, 30));  255位,小数占30位
double 
	#	create table 表名 (id double(255, 30));  255位,小数占30位
decimal
	#	create table 表名 (id decimal(65, 30));  65位,小数占30位
  绝大部分,都选decimal

'''字符串'''  
char(4)
	#	定长,超出4位,报错,不够4位,空格填充
varchar(4)
	#	可变长,不够4位,有几位存几位,超出4位,有几位存几位

'''日期类型'''
date
		2023-04-04
datetime
		2023-04-04 11:11:11
time
		11:11:11
year
		1995

'''枚举:多选一 enum()'''
	create table 表名 (id int, gender enum('male','female','other'));只能选择enum里面的选项

'''集合:多选多 set()'''
	create table t15 (id int,  hobby 
  set('read','music','tangtou','xijio','anmo') );可以选择set里面的多个选项

约束条件

# unsigned
无符号属性,表示该列只能存储非负数  create table 表名(id int unsigned);

# zerofill
填充0,在列的值不足指定长度时进行填充

# not null
指定列的值不能为空
		create table 表名(
        id int, 
        name varchar(32) not null );

# default
	指定列的默认值,当插入一条记录时如果该列没有值则使用默认值
		 create table 表名(
        id int, 
        name varchar(32) default 'ly');

# unique
	指定该列的值必须是唯一的,保证了该列的值不会出现重复
		'单列唯一
			单列唯一是指保证表中某个列的取值唯一
				create table 表名 (
        	id int,
            name varchar(32) unique);
		'联合唯一
			联合唯一是指保证表中多个列的组合取值唯一
				create table 表名(
        	id int,
            host varchar(32) ,
            port varchar(32) ,
            unique(host,port));

# primary key
	指定该列为主键,且该列的值必须非空且唯一,'主键可以加快查询速度!!!因为主键本质上也是一种索引!!!
	MySQL中的InnoDB存储引擎规定了每张表必须有主键,即使我们没有设置主键,它也会自动添加一个隐藏的字段作为主键来维护表的结构。
	'如何给字段添加主键
		create table 表名 (
    	id int primary key, 
        name varchar(32));

# auto_increment
	可以自增整数类型的字段,会在插入新记录时,将该字段的值加1。保证每条记录的唯一性。
		 '''以后我们在创建id字段的时候,固定语法结构:'''
    id int primary key auto_increment


# 如果语句中同时存在not noll和unique自动升级为主键
# auto——increment 搭配主键一起使用;固定搭配。可以主动自增序号

查询关键字

where 筛选

  模糊查询:没有明确的筛选条件
    关键字:like
    关键符号:
        %:匹配任意个数任意字符
        _:匹配单个个数任意字符

		
查询id大于等于3小于等于6的数据
	select id,name from 表名 where id >= 3 and id <= 6;
	select *  from 表名 where id between 3 and 6;  
		
'查询薪资是20000或者18000或者17000的数据'
	select * from 表名 where salary = 20000 or salary = 18000 or salary = 17000;
	select * from 表名 where salary in (20000,18000,17000);  # 简写
		
'查询员工姓名中包含o字母的员工姓名和薪资'
			先是查哪张表 from 表名
再是根据什么条件去查 where name like ‘%o%’
再是对查询出来的数据筛选展示部分 select name,salary
	select name,salary from 表名 where name like '%o%';
		
'查询员工姓名是由四个字符组成的员工姓名与其薪资'
	select name,salary from 表名 where char_length(name) = 4;
		
'查询id小于3或者大于6的数据'
	select *  from 表名 where id not between 3 and 6;
		
'查询薪资不在20000,18000,17000范围的数据'
	select * from 表名 where salary not in (20000,18000,17000);
		
'查询岗位描述为空的员工名与岗位名  针对null不能用等号,只能用is,在sql中,NULL和''不一样'
	select * from 表名 where post_comment is null or '';  (查询单个null或者空‘’,is null或者is ‘ ’)

group by分组

按照某个指定的条件将单个单个的个体分成一个个整体
        针对5.6需要自己设置sql_mode模式
 set global sql_mode = 'only_full_group_by,STRICT_TRANS_TABLES,PAD_CHAR_TO_FULL_LENGTH';

 

show variables like '%mode%'; 查看数据库的模式

 '聚合函数   聚合函数主要就是配合分组一起使用
    max最大值 min最小值 sum求和  count计数  avg平均值
 

'按部门分组'
	select * from 表名 group by 字段名;  # 分组后取出的是每个组的第一条数据
以组为单位统计组内数据>>>聚合查询(聚集到一起合成为一个结果)
	每个部门的最高工资
		select post,max(salary) from 表名 group by 字段名;
	每个部门的平均工资
		select post,avg(salary) from 表名 group by 字段名;
	每个部门的工资总和
		select post,sum(salary) from 表名 group by 字段名;
	每个部门的人数
		select post,count(id) from 表名 group by 字段名;


# group_concat  分组之后使用 ,如果需要获取分组以外的数据字段
	每个部门的员工姓名
		select post,group_concat(name) from 表名 group by 字段名;
	同时获取两种数据,数据之前特殊符号隔开
		select post,group_concat(name,'|',sex) from 表名 group by 字段名;
	返回唯一不同值,避免出现重复
		select post,group_concat(distinct name) from 表名 group by 字段名;
		separator 是在使用 group_concat 函数时,用来分隔合并结果的字符串的符号。如果不指定 separator,则默认使用逗号作为分隔符
			select post,group_concat(distinct name separator '%') from 表名 group by 字段名;


# concat  不分组使用
		select concat(name,sex) from 表名; 没有分割
		select concat(name,'|',sex) from 表名;  指定分割


# concat_ws()
		'一个分隔符连接多个对象'
		select post,group_concat('|',name,sex)from 表名 group by 字段名;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值