MySQL DAY2

MySQL DAY2

主键外键补充

一个表只能有一个主键,主键可以由多列组成。

create table t5(
       nid int(11) not null auto_crement,
       pid int(11) not null,
       num int(11),
       primary key(uid,pid)
       )engine=innodb default charset=utf8;
create table t6(
       id int auto_increment primary key,
	   name char(10),
	   id1 int,
	   id2 int,
	   constraint fk_t5_t6 foreign key(id1,id2) references t5(nid,pid)
	   )engine=innodb default charset=utf8;

对于自增补充

desc t1查t1表中各属性
show create table t1 \G查自增的序列,即auto_increment增至多少

MySQL:自增步长
基于会话级别:
shoe session variables like 'auto_inc%'c查看全局变量
set session auto_increment_increment=2设置会话步长(关闭会话框再打开时恢复默认值)
set sesiion auto_increment_offset=10设置初始值,一般默认为1,不用改
基于全局变量(一般不要用)
show global variables like 'auto_inc%'查看全局变量
set global auto_increment_increment=2设置步长
set global auto_increment_offset=10设置初始值

唯一索引

create table t1(
			id int ....,
			num int,
			xx int,
			unique 唯一索引名称 (列名,列名),
			constraint ....
		)

PS:唯一:约束不能重复(可以为空)
                 (主键不能重复)
         加速查找

外键的变种

一对多

用户表:

id name department_id
1 alex 1
2 root 1
3 egon 2
4 laoyao 3
id deparment
1 服务
2 公关
3 保安
create table bm(
     id int auto_increment primary key,
     name char(32),
     )engine=innodb default charset=utf8;
     
create table yh(
     id int auto_increment primary key,
     name char(32),
     department_id int,
     constraint fk_yh_bm foreign key(department_id) references bm(id)
     )engine=innodb default charset=utf8;

一对一

用户表:

id name
1 alex
2 root
3 egon
4 laoyao

博客表:(每个人只能有一个博客,所以增加了唯一索引)

id blog_name yh_id
1 /fsddf/ 4
2 /dsfdsf/ 1
3 /sdfsdi/ 2
4 /laoydff/ 3
create table yh(
     id int auto_increment primary key,
     name char(32)
     )engine=innodb default charset=utf8;

create table blog(
     id int auto_increment primary key,
     blog_name char(32),
     yh_id int,
     unique uq_yh(yh_id),
     constraint fk_yh_blog foreign key(yh_id) references yh(id)
     )engine=innodb default charset=utf8;

多对多

示例1:

用户表
相亲表
         一个用户可以和多个用户约会,也可以和同一个用户多次相亲

示例2:

用户表
主机表
用户主机表
             一个用户可以管理多台主机,一个主机可以由多个用户管理,为了使查找某个用户管理哪几台主机或者某个主机由哪几个用户管理方便,再创建第三个表【用户主机表】。

create table userinfo(
	id int auto_increment primary key,
	name char(10),
	gender char(10)
	)engine=innodb default charset=utf8;

create table host(
	id int auto_increment primary key,
	hostname char(64)
	)engine=innodb default charset=utf8;

create table user2host(
	id int auto_increment primary key,
	userid int not null,
	hostid int not null,
	unique uq_user_host (userid,hostid),
	constraint fk_u2h_user foreign key (userid) references userinfo(id),
	constraint fk_u2h_host foreign key (hostid) references host(id)
	)engine=innodb default charset=utf8;

SQL语句数据行操作补充

insert后面不加table
insert后面into可加可不加

insert tb1(name) valus ('alex'),('ayz');可以多个数据一次性加
insert tb1(name,age) select name,age from tb2;可以从某表里取出数据插入另一个表

delete from tb1
delete from tb1 where id !=1
delete from tb1 where id <>1
delete from tb1 where id >=1
delete from tb1 where id <=1 or name ='alex'

update tb1 set name='alex' where id>12 and name='xx'
update tb12 set name='alex',age=19 where id>12 and name='xx'

select * from tb12;
select id,name from tb12;
select id,name from tb12 where id > 10 or name ='xxx';
select id,name as cname from tb12 where id > 10 or name ='xxx';查的同时改列名
select name,age,11 from tb12;多增一列,列名为11,数据全为11

其他

select * from tb12 where id != 1 select * from tb12 where id in (1,5,12);查找出id=1、5、12 的
select * from tb12 where id not in (1,5,12);查找出id不等于1、5、12 的
select * from tb12 where id in (select id from tb11)查找出id=tb11表里的
select * from tb12 where id between 5 and 12;查找出id=5~12 的(闭区间)

通配符

select * from tb12 where name like "a%"查找以a开头的所有name
select * from tb12 where name like "a_"查找出以a开头且后面只跟一个字母的name
select * from tb12 where name like "%a%"查找包含a的name
%单引号双引号都可以

分页

select * from tb12 limit 10;只显示10条数据
select * from tb12 limit 0,10;只显示10条数据,从0开始显示,即从头(为0)
select * from tb12 limit 12,10;只显示12条数据,从第11条数据开始
select * from tb12 limit 12 offset 10;只显示12条数据,从第11条数据开始

排序

select * from tb12 order by id desc;大到小
select * from tb12 order by id asc;小到大
select * from tb12 order by age desc,id desc;先按age倒序排列,当age相同时,按id倒序排
select * from tb12 order by id desc limit 10;取后10条数据

分组

select count(id),max(id),part_id from tb group by part_id;
按照part_id分组
countpart_id出现的次数
maxpart_id的最大值
minpart_id的最小值
sumpart_id的求和
avgpart_id的平均值

查找用户表:

用户表

按性别分组:

按性别分组

** 如果对于聚合函数结果进行二次筛选时?必须使用having **
select count(gender),gender from yhbiao group by gender having count(gender) ='男';
select count(id),part_id from t1 where id > 0 group by part_id having count(id) > 1;

连表

select * from t1,t2
select * from t1,t2 where t1.part_id = t2.id
select * from t1 left join t2 on t1.part_id = t2.id
select * from t2 left join t1on t1.part_id = t2.id
t1左边全部显示
select * from t2 right join t1 on t1.part_id = t2.id
t2右边全部显示
select * from t1 innder join t2 on t1.part_id = t2.id
将出现null时一行隐藏

查找用户表:

用户表

查找博客表

博客表

用户_博客表 连表(左连接):

用户_博客表连表

用户_博客表 连表(右连接)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/TOPic666/article/details/107501654