mysql的左右连接 分页_MySQL基础之Ⓑ表连接、约束、外键、分组、分页、排序、唯一索引...

本文详细介绍了MySQL数据库中的数据类型,包括数字、字符、时间、二进制以及枚举和集合类型。同时,阐述了对数据库和表的操作,如创建、查询、修改和删除。此外,还讲解了主键、外键和唯一约束,以及自增设置。最后,提到了条件查询和表连接的基本用法,如内连接、外连接和联合查询。
摘要由CSDN通过智能技术生成

1.重要的重复:键的数据类型

1)数字相关

tinyint  255

int      65535

bigint   42亿

decimal  十进制小数,实际是字符串保存,有精度的小数。

float    浮点型  保留有限位小数

double   双精度  保留双倍有限位小数

2)字符相关

char  固定长度,查询速度快,节约时间  最长255个字节

varchar 可变长度  节约空间  最长255个字符

text  文本型  最长:65535个字符

3)时间

date,time,datetime,timestamp

4)二进制:

blob

5)枚举、set

enum(‘xl’,‘xxl’,‘xxxl’)

set('a','b','c','d') # 任意组合

2.sql操作

(1)对数据库的操作

1)查看数据库

show databases;

2)打开数据库:

use (数据库名);

3)创建数据库

create database test default charset utf8;

4)删除数据库

drop databases test;

5)查看数据库下所有的表:

show tables;

(2)对表的操作

1)创建表

create table student(

id int auto_increment primary key,

name varchar(12) not null,

info varchar(22)) engine=innodb charset=utf8;

2)查询表

select  * from student;

3)查看表:

desc student;  # 表结构

show create table class \G;  # 建表的信息

4) 清空表:

保留自增id号:delete from student;

不保留自增号:truncate table student;

5) 彻底删除表:

drop table student;

6) 修改表

alter table student auto_increment = 1;

(3)对行的操作:

增删改查:

insert into student(name,info) values('tom','');

#一次插入多个值:

insert into user(name,age) values('tom',22),('jim',23),('tomas',18),('jerry',19);

# 将临时表插入实体表

insert into t2(name) select name from user;

# 修改内容

update student set name='',info='' where name = '';

update user set age = 29 where age = 18;

# 查询的条件

select * from user where age between 18 and 22;

# 综合查询

select * from user where age > 17 and name like 't%';

select name as s_name,age, 20 from user;

select name as s_name,age, 'avg_20' from user;

select * from user order by uid limit 3;

select * from user order by age desc limit 3;

3、约束和条件查询

(1)主键约束

create table score(

id int auto_increment not null,

student_id int not null,

class_id int not null,

score_num tinyint,

pimary key(id),

(2)外键约束

constraint fk_stu_cls foreign key (class_id) references class(cid)

(3)唯一约束

unique uq_std_cls_id (student_id, class_id)

)engine=innodb default charset=utf8

(4)自增设置步长

1)修改会话,局部变量,只影响当前绘画(登录的用户),常用。默认为1

show session variables like 'auto_incre%'

set session auto_increment = 2

2)修改全局,影响所有的会话。

show globle variables like 'auto_incre%'

set globle auto_increment = 2

(5)特殊的外键:

1)一对一

外键加上唯一索引,就是一对一的关系。

2)一对多

自带支持

3)多对多

双向都有一对多关系,就是多对多关系。

这种情况下,为了操作方便,会建立一张中间表,记录两表的关系。

中间表存储两个表的id,建立外键约束,如果需要,可以加唯一索引,将两个id加入。

(6)条件语句

1)分页

取前10行结果

select * from student limit 10;

从第一条记录开始,取前10条

select * from student limit 0,10;

取第一条

select * from student limit 0 1;

取第二条

select * from student limit 1 1;

2)通配符

查询学生表中,以t开头的名字的学生

select * from student where name like 't%';

查询以nd结尾的名字的学生

select * from student where name like '%du'

查询由t开头的,两个字符组成的名字

select * from student where name like 't_';

3)排序

默认升序,从小到大

select * from student order by id;

可设置为倒序,即从大到小

select * from stduent order by id desc;

4)分组

默认的查询都是一个分组,

查询的结果必须是一个聚合函数:sum,avg,count,max,min等,或聚合的列,即跟group by后的列,是一对一的关系。

select count(id) from score where score_num > 60 group by id having count(id) > 1

mysql5.7版本,新增sql_mode=only_full_group_by,可以关闭,但不建议!

4、表连接

(1)左右连接 join

1)内连接

隐藏有null值的行

2)外连接

左外连接 left join  全部显示左边的表的列

右外连接 right join 全部显示右边的表的列

(2)上下连接 union

1)去掉重复内容

union

2)不去重

union all

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值