mysql 基本指令 1
Posted TY520
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql 基本指令 1相关的知识,希望对你有一定的参考价值。
desc 表名 --查看表属性
show create table 表名 g; --查看代码
alter table 表名 auto_increment=20; --改自增的值
mysql:自增步长
基于会话级别:
show session variables like ‘auto_inc%‘; --站看全局变量
set session auto_increment_increment=2; --设置会话步长
基于全局级别:
show global variables like ‘auto_inc%‘; --查看全局变量
set global auto_increment_increment=2; --设置会话步长
SQLServer:自增步长:
基础表级别:
create table ‘t1‘(
‘nid‘ int(11) not null auto_increment primary key,
‘pid‘ int(11) not null,
‘num‘ int(11) default null,
primary key(‘nid‘)
)ENGINE=InnoDB auto_increment=4, 步长=2 DEFAULT CHARSET=utf8
唯一索引:
create table t1(
id int ...,
num int,
xx int,
unique uq1 (num,xx)
)
PS:
唯一:
约束不能重复(可以为空)
PS:主键不能重复(不能为空)
加速查找
外键的变种:
a.用户表和部门表
用户:
1 alex 1
2 root 1
3 egon 2
4 laoyao 3
部门:
1 服务
2 保安
3 公关
====》 一对多
示列1
b. 用户表和博客表
用户表:
1 alex
2 root
3 egon
4 laoyao
博客表:
FK() + 唯一
1 /alex3714/ 4
2 /yuancheqi/ 1
3 /wupeiqi/ 1
====》一对一
create table userinfo1(
id int auto_increment primary key,
name char(10),
gender char(10),
email varchar(64)
)engine=innodb default charset=utf8;
create table admin(
id int not null auto_increment primary key,
username varchar(64) not null,
password varchar(64) not null,
user_id int not null,
unique uq_u1 (user_id),
constraint fk_admin_ul foreign key (user_id) references userinfo1(id)
)engine=innodb default charset=utf8;
示列2
用户表
主机表
用户主机关系表
====》 多对多
create table userinfo2(
id int auto_increment primary key,
name char(10),
gender char(10),
email varchar(64)
)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 userinfo2(id),
constraint fk_u2h_host FOREIGN key (hostid) references host(id)
)engine=innodb default charset=utf8;
分页:
select * from stu limit 1,10;
select * from stu limi;
select * from stu order by sni xxx desc;
排序:
select * from stu order by sni desc limit 2; 从大到小
select * from stu order by sni asc;从小到大
a.用户表和部门表
用户:
1 alex 1
2 root 1
3 egon 2
4 laoyao 3
部门:
1 服务
2 保安
3 公关
====》 一对多
示列1
b. 用户表和博客表
用户表:
1 alex
2 root
3 egon
4 laoyao
博客表:
FK() + 唯一
1 /alex3714/ 4
2 /yuancheqi/ 1
3 /wupeiqi/ 1
====》一对一
create table userinfo1(
id int auto_increment primary key,
name char(10),
gender char(10),
email varchar(64)
)engine=innodb default charset=utf8;
create table admin(
id int not null auto_increment primary key,
username varchar(64) not null,
password varchar(64) not null,
user_id int not null,
unique uq_u1 (user_id),
constraint fk_admin_ul foreign key (user_id) references userinfo1(id)
)engine=innodb default charset=utf8;
示列2
用户表
主机表
用户主机关系表
====》 多对多
create table userinfo2(
id int auto_increment primary key,
name char(10),
gender char(10),
email varchar(64)
)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 userinfo2(id),
constraint fk_u2h_host FOREIGN key (hostid) references host(id)
)engine=innodb default charset=utf8;
分页:
select * from stu limit 1,10;
select * from stu limi;
select * from stu order by sni xxx desc;
排序:
select * from stu order by sni desc limit 2; 从大到小
select * from stu order by sni asc;从小到大
分组:
select count(id) from userinfo;
select count(id),part_id from userinfo1 group by part_id;
max
min
sum
avg
***** 如果对于聚合函数结果进行二次筛选时? 必须使用having ****
select count(id), part_id from userinfo1 group by part_id having count(id)>1;
select count(id) from userinfo;
select count(id),part_id from userinfo1 group by part_id;
max
min
sum
avg
***** 如果对于聚合函数结果进行二次筛选时? 必须使用having ****
select count(id), part_id from userinfo1 group by part_id having count(id)>1;
连表操作:
select * from userinfo1,userinfo2;
select * from userinfo1,userinfo2 where userinfo1.part_id = userinfo2.id;
select * from userinfo1 left join userinfo2 on userinfo1.part_id = userinfo2.id
# userinfo2左边全显示
select * from userinfo right join userinfo2 on userinfo1.part_id = userinfo2.id
# userinfo2右边全显示
select * from userinfo1 innder join userinfo2 on userinfo1.part_id = userinfo2.id;
# 将出现null的行隐藏
select
work.id
from
work
left join userinfo1 on work.userinfo1_id = userinfo1.id
left join userinfo2 on work.userinfo2_id = userinfo2.id;
select * from userinfo1,userinfo2;
select * from userinfo1,userinfo2 where userinfo1.part_id = userinfo2.id;
select * from userinfo1 left join userinfo2 on userinfo1.part_id = userinfo2.id
# userinfo2左边全显示
select * from userinfo right join userinfo2 on userinfo1.part_id = userinfo2.id
# userinfo2右边全显示
select * from userinfo1 innder join userinfo2 on userinfo1.part_id = userinfo2.id;
# 将出现null的行隐藏
select
work.id
from
work
left join userinfo1 on work.userinfo1_id = userinfo1.id
left join userinfo2 on work.userinfo2_id = userinfo2.id;
以上是关于mysql 基本指令 1的主要内容,如果未能解决你的问题,请参考以下文章