mysql学习日记一

在上次的两堂课中开始学习了MySQL的基础知识,从=基本的创建,修改,删除用户开始学起

#创建用户
create user yang  identified  by'123456';
#修改用户
alter user yang identified  by'147852';
#删除用户 
drop user yang ;

接下来就是创建表及使用的一系列的学习

创建,修改,删除表格:

#外键约束   保证数据一致性
create table mytable
(
     number int primary key,#主键约束,表述每一个表只有一个主键,当前列信息不允许重复或为空
     name   varchar(45) unique, #唯一约束,这列值不允许重复或为空
     age    datetime default now(),#默认约束
     sex    enum('男','女')
);

alter table mytable add column address varchar(45);
alter table mytable modify address int;
alter table mytable drop column address;
drop table mytable;

其次就是在表格中进行查询,运用了很多的方法(very)

在表中查询,增加,修改,删除:(这还是很简单的)

#查询
#select 列名  from  表名
select number from mytable;
select number,name from mytable;
select * from mytable;
#增加 
#insert into 表名 values(值,值,值)
insert into mytable values(1,'test','202201','man');
insert into mytable values(2,'test','202202','man');
insert into mytable values(3,'test','202203','man');
insert into mytable values(4,'test','202204','man');
insert into mytable(number,sex) values(2,'woman');

#修改
update mytable set sex='男';
#删除 
delete from mytable;

特定条件的查询(不同方式):

select *from mytable where number=1;
select *from mytable where number!=1;
select *from mytable where number<>1;
select *from mytable where number>=1 and number <=3;
select *from mytable where number between 1 and 3;
select *from mytable where number= 1 or number=3;
select *from mytable where number in(1,4,7);
select *from mytable where number not in(1,4,7);

update mytable set name='ttt' where number=2;#更新,差不多是修改吧

模糊匹配,limit,排序函数的使用:

#模糊匹配 like %任意0个或多个字符  ——代表任意一个字符
select * from mytable where number like 't%';
select * from mytable where number like '%t';
select * from mytable where number like '%1%';
select * from mytable where number like '_e%';

#limit限制显示个数以及位置 
select*from mytable limit 3;
select*from mytable limit 1,2;  #偏移,显示
#排序函数 order by +lie +asc 升序/desc 降序
select*from mytable order by age desc,number asc;

三种范式:#第一范式 属性不可以拆分 

                 #第二范式 不允许对组合主键的部分依赖

                 #第三范式  不允许传递依赖 

连接函数和聚合函数的应用:(分组就是增加了条件,说白了就是事多了)

#聚合函数    将多个结果聚合成一个值
select max(score) from sc;
select min(score) from sc;
select sum(score) from sc;
select avg(score) from sc;
select count(score) from sc;
#求出student男女生的个数
#union all 连接函数  列的个数  类型保持一致 
select count(ssex)from student where ssex='男'
union  all
select count(ssex)from student where ssex='女';

select *from student;

select ssex,count(ssex)from student group by ssex;
#分组group by  +  条件 having
#分了组的列,或者加了聚合函数
select ssex,count(ssex)from student group by ssex having count(ssex)=4;

连接函数就是把两种列的个数,类型保持一致的查询连接在一起

select也可以当条件

多表查询:(简化了过多的步骤)

#多表查询

#内联 
select *from student;
select*from sc;

select *from student inner join sc on student.snum=sc.snum;
#左联 
select *from student left join sc on student.snum=sc.snum;
#右联 
select *from student right join sc on student.snum=sc.snum;
#笛卡尔积 
select*from student,sc where student.snum=sc.snum;

今天的总结就是这麽多,去吃荔枝了

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值