目录
外键约束 foreign key ... references ...
order by ... desc 从大到小排序。asc升序
full join 全连接-mysql不支持-使用union代替
函数avg(平均值), count(统计个数),max(最大值),min(最小值)
把字段里的值转换为 大写ucase( ) 小写lcase( )
格式:
create database 数据库名;
案例:
create database text;查询以有的数据库
查询以有的数据库:
show databases;指定一个数据库
格式:
use 数据库名;
案例:
use test;创建表
格式:
create table 表名(
类型 ...;
...
);
案例:
create table test(
id int,
name varchar(20)
);查看表的结构
格式:
describe 表名;
案例:
describe text;
Field:字段
Type: 字段的类型
Null: 是否允许为空
Key: 约束
Default:默认值
Extra: 其他
增删改查
格式:
增:insert into 表名 values (字段1,字段2....);
删:delete from 表名 where 条件;
改:update 表名 set 字段名 where 条件 ;
查:select * from 表名;
案例:
-- 增删改查
# 增
insert into text values('七七','女','2000-1-1');
# 删
delete from text where name='七七';
# 改
update text set name='伞兵nb' where sex='男';
# 查
select * from text;
主键约束
案例:创建表时添加主键
该id 添加了主键约束,就是id的数据是不可重复的、唯一的、并且不能为空。
关键字:primary key
create table test(
id int primary key,
name varchar(20)
);
联合主键
案例:创建表时添联合主键
create table user2(
id int,
name varchar(20),
pwd varchar(20),
primary key(id,name)
);
联合主键的字段,添加数据时都不能为空
而且主键联合的数据内容不能被重复
例如:
insert into user2 values(1,'张三','666');-- 成功
insert into user2 values(1,'张四','666');-- 成功
insert into user2 values(1,'张四','666');-- 失败自增约束
案例:创建表时添加主键自增
关键字:auto_increment
create table user3(
id int primary key auto_increment,
name varchar(20)
);
自增: 这里只添加的名字 id是自增
insert into user3 (name) values('张三');-- 成功id自增
insert into user3 (name) values('张三');-- 成功id自增alter add 添加字段
案例:给表添加字段
格式:alter table 表名 add 字段名称 字段类型;
关键字:alter add
create table user3(
id int,
name varchar(20)
);
alter table user3 add passtest int(4); --添加成功
alter add 添加主键
在我们创建表时忘记设置主键
create table user3(
id int,
name varchar(20)
);
案例:给关键字加上索引
格式:alter table 表名 add primary key(字段名称);
例子:
alter table user3 add primary key(id);-- 设置id主键成功
drop 删除主键+删除字段
create table user4(
id int primary key,
name varchar(20)
);
alter table user4 drop primary key;--删除了id的主键
alter table user4 drop id; --删除了idalter modify修改字段类型
create table user4(
id int primary key,
name varchar(20)
);
格式:alter table 表名 modify 字段名 类型;
alter table user4 modify name int(20);alter change修改字段名
create table user4(
id int primary key,
name varchar(20)
);
格式:alter table 表名 change 字段名 新的字段名 类型; 这里类型也可以修改为别的
alter table user4 change name nb varchar(25);唯一约束 unique
格式:unique(字段名)
表示这个字段插入的内容不可以有重复数据
create table user5(
id int primary key auto_increment,
name varchar(20),
unique(name)
);
insert into user5 values(1,'牛B'); -- 成功
insert into user5 values(2,'牛B'); -- 失败非空约束not null,默认约束default null
not null:插入数据时不能为空,非空约束
defaulr null:不插入这个字段数据 默认为空
也可以写 default 1,
不插入这个字段默认 为1
create table user3(
id int,
name varchar(20)not null,
pwd varchar(20)default null
);
外键约束 foreign key ... references ...
一个表中的 foreign key 指向另一个表中的 primary key
需要创建两张表 子表key引入父类key
父表的外键id
create table a(
id int primary key,
name varchar(20)
);
create tabke b(
id int primary key,
name varchar(20),
a_id int,
foreign key(a_id)references a(id)
);
insert into a values(1,'小李');
insert into b values(1,'张三',1);
delete from a where id = 1; 删除失败,因为子类a_id 有数据
delete from b where a_id = 1; 删除成功;
现在在删除父类的id 才成功
delete from a where id = 1; 删除成功
父类里面键没有的数据 子表里添加的话会失败
因为两个id关联的distinct 排重查询
案例:先创建一个表
create table test0(
id int primary key auto_increment,
name varchar(25)not null
);
给name 插入数据
insert into test0 (name) values('a');
insert into test0 (name) values('a');
insert into test0 (name) values('a');
insert into test0 (name) values('b');
有三个重复的
select * from test0;这个会查到所有,4条数据
排重关键字 distinct
select distinct name from test0;
会显示两条,一个a 一个bbetween ... and... 取两个值之间范围
案例:先创建一个表
create table test0(
id int primary key auto_increment,
a int
);
插入3条数据
insert into test3 (a) values(100);
insert into test3 (a) values(60);
insert into test3 (a) values(50);
查询100 到 50 之间的数字 (包括100,50)
select * from test3 where a between 50 and 100;
等同于:
select * from test3 where a >=50 and a<=100;in (或者)
案例:先创建一个表
create table test0(
id int primary key auto_increment,
a int
);
插入2条数据
insert into test3 (a) values(100);
insert into test3 (a) values(50);
查询a= 50 和, a=999;的数据;
如果没有a=999的,程序也不会报错
select * from test3 where a in(50,999);显示一条
select * from test3 where a in(50,100);两条and 和 or 和where 结和
and 和 or 可在 where 子语句中把两个或多个条件结合起来。
如果第一个条件和第二个条件都成立,则 and 运算符显示一条记录。
如果第一个条件和第二个条件中只要有一个成立,则 or 运算符显示一条记录。
案例:先创建一个表
create table test0(
id int primary key auto_increment,
a int
);
给a添加100;
insert into test0 (a) values (100);
使用and
查询a=100,和id=100的用户
这里查询不到,因为a=100有,id=100没有。
select * from test0 where a=100 and id=100;
使用or
查询a=100,或者id=100的用户
select * from test0 where a=100 or id=100;
查到一条数据order by ... desc 从大到小排序。asc升序
ordey by 列 desc倒叙
案例:先创建一个表
create table test0(
id int primary key auto_increment,
a int
);
insert into test0 (a) values (1);
insert into test0 (a) values (2);
insert into test0 (a) values (3);
ordey by 列 关键字desc 倒叙 a的数据
select * from test0 order by a desc;
查询结果:
id a
1 3
2 2
3 1
默认是asc 就是从小到大,可以看出id就是asccount() 统计总数
count(列,或者*) 如果字段里的数据是null -不计数据
案例:先创建一个表
create table test0(
id int primary key auto_increment,
a int
);
insert into test0 (a) values (1);
insert into test0 (a) values (2);
insert into test0 (a) values (3);
insert into test0 (id) values (4); 这个id=4,a默认是空
获取字段a 有几条数据
select count(a) from test0;
结果集 是3
count(a)
3 limit M,N
limit M,N
M:从第几条数据开始查,代表索引
N:要查几条数据
案例:先创建一个表
create table test0(
id int primary key auto_increment,
a int
);
insert into test0 (a) values (1);
insert into test0 (a) values (2);
insert into test0 (a) values (3);
案例:
从第二条数据开始查,查询2两条数据
select * from test0 where a limit 2,2;
结果:
id a
2 2
3 3like % _ 模糊查询
like 指定一个字段 连接通配符 模糊查询
通配符:% _ ^[charlist] ^[^charlist]
% 代表0个 或者多个
_ 代表一个
^[charlist] 字符列中的任何一个字符
^[^charlist] 不查询这个里面的字符
案例:先创建一个表
create table test01(
id int primary key auto_increment,
name varchar(20)
);
insert into test01 (name) values('李世民');
insert into test01 (name) values('李王者');
insert into test01 (name) values('王者');
例子1、希望查询姓李的用户
select * from test01 where name like '李%';
结果集:
id name
1 李世民
2 李王者
例子2、希望查询姓名结尾是民的用户
select * from test01 where name like '%民';
结果集:
id name
1 李世民
例子3、希望查询名字包含王者 的人
select * from test01 where name like '%王者%';
结果集:
id name
2 李王者
3 王者
例子4、希望查询名字 不包含 王者的人
select * from test01 where name not like '%王者%';
结果集:
id name
1 李世民
例子5、希望查询名字开头是李 第二个字随便 第三个是者的人
select * from test01 where name like '李_者';
例子6、希望查询名字 不是姓王和姓李的人
select * from test01 where name regexp '^[^王李]';
例子7、希望查询名字 姓名李和姓王的人
select * from test01 where name regexp '^[王李]';
as 别名
可以为列名称和表名称指定别名(Alias)。
案例:先创建一个表
create table test01(
id int primary key auto_increment,
name varchar(20)
);
insert into test01 values(1,'牛逼');
列的名字起别名
select id as nameId,name from test01;
结果集
nameId name
1 牛逼
表名起别名
创建第二个表
create table test02(
id int primary key auto_increment,
name varchar(2);
);
insert into test02 values(1,'nb');
select a.id,a.name,b.id,b.name from test01 as a,test02 as b where a.id = b.id;
等同于:
select test01.id,test01.name,test02.id,test02.name from test01,test02 where test01.id=test02.id;inner join 关联查询(内连接)
inner join 和 join 是相同的
用于多个表关联查询
案例:
创建两个表
create table num01(
id int primary key auto_increment,
name varchar(10) not null
);
create table num02(
id int primary key auto_increment,
home varchar(20) not null,
num01_id int
);
insert into num01 value (1,'张三');
insert into num01 value (2,'李四');
insert into num02 values (1,'中国',2);
insert into num02 values (2,'美国',1);
内连接 关联查询 inner join
select * from num01 inner join num02 on num01.id=num02.num01_id;
只查询表表一的name ,和表二对应表一的家,可以使用别名 也可以不使用
select a.name,b.home from num01 a inner join num02 b on a.id=b.num01_id;
left join 关联查询(左连接)
left join 左连接,左表的数据全显示,右边连接的表匹配左边,没有就显示null
案例:
创建两个表
create table num01(
id int primary key auto_increment,
name varchar(10) not null
);
create table num02(
id int primary key auto_increment,
home varchar(20) not null,
num01_id int
);
insert into num01 value (1,'张三');
insert into num01 value (2,'李四');
insert into num01 value (3,'王五');
insert into num02 values (1,'中国',2);
insert into num02 values (2,'美国',1);
左连接查询
select a.name,b.home from num01 a left join num02 b on a.id = b.num01_id;
结果集
name home
张三 中国
李四 美国
王五 空
right join 关联查询(右连接)
right join 右连接, 右边的表数据全显示,左表匹配右表,没有就是null
案例:
创建两个表
create table num01(
id int primary key auto_increment,
name varchar(10) not null
);
create table num02(
id int primary key auto_increment,
home varchar(20) not null,
num01_id int
);
insert into num01 value (1,'张三');
insert into num02 values (1,'中国',1);
insert into num02 values (2,'美国',2);
右连接查询
select a.name,b.home from num01 a right join num02 b on a.id=b.num01_id;
结果集:
name home
张三 中国
空 美国
full join 全连接-mysql不支持-使用union代替
right join 右连接, 右边的表数据全显示,左表匹配右表,没有就是null
案例:
创建两个表
create table num01(
id int primary key auto_increment,
name varchar(10) not null
);
create table num02(
id int primary key auto_increment,
home varchar(20) not null,
num01_id int
);
insert into num01 value (1,'张三');
insert into num01 value (3,'李四');
insert into num02 values (1,'中国',1);
insert into num02 values (2,'美国',2);
全连接
select * from num01 a left join num02 b on a.id=num01_id union all
select * from num01 a right join num02 b on a.id=b.num01_id;
结果集:
id name id home num01_id
1 张三 1 中国 1
空 空 2 美国 2
3 李四 空 空 空union 合并多个select查询
union 连接多个select查询,union后面连接的select的结果集会排重
是内容一模一样 才会排重
union all 是显示所有数据 --内容一样的数据也会展示
案例:
创建两个表
create table a(
id int primary key auto_increment,
name varchar(20) not null
);
create table b(
id int primary key auto_increment,
nome varchar(20) not null
);
insert into a values (1,'李先生');
insert into a values (2,'李先生');
insert into a values (3,'张先生');
insert into b values (1,'李先生');
insert into b values (2,'吴先生');
insert into b values (3,'张先生');
union
select * from a union select * from b;
结果集:
id name
1 李先生
2 李先生
3 张先生
2 吴先生
union all
select * from a union all select * from b;
结果集:
id name
1 李先生
2 李先生
3 张先生
1 李先生
2 吴先生
3 张先生Date 时间数据类型
格式:
date - 格式 YYYY-MM-DD
datetime YYYY-MM-DD HH:MM:SS
timestamp YYYY-MM-DD HH:MM:SS
year YYYY 或者YY
查询当前日期:YYYY-MM-DD HH:MM:SS
select now(); 结果:2021-8-26 18:59:18
查询当前年月日:YYYY-MM-DD
select curdate(); 结果:2021-08-26
查询当前时间:HH:MM:SS
select curtime(); 结果:18:59:18
练习:查询90岁以上 而且姓李的用户
create table studengTest(
id int primary key auto_increment,
name varchar(50) not null,
birthday date
);
insert into studengTest values (1,'李世民','1900-01-01');
insert into studengTest values (2,'唐太宗','1800-09-09');
查询90岁以上 而且姓李的用户
select * from studengTest where year(now())-year(birthday) > 90 and name like '李%';
查询他们的年龄 ----当前年月日-出生年月日
select name as '用户名',birthday as '出生日期',year(curdate())-year(birthday) as '年龄' from studengTest;
查询当前年月日
select date(now());
结果集
2021-08-26函数avg(平均值), count(统计个数),max(最大值),min(最小值)
avg()函数:返回的结果集 数值列的平均数,null值不会计算
count()函数:统计行数,null不计算
max()函数:查询price列中最大的值,null不计算
min()函数:查询price列中最小的值,null不计算
sum()函数:查询price总额
create table abc01(
id int,
name varchar(20),
price int
);
insert into abc01 values(1,'李先生',1000);
insert into abc01 values(2,'吴先生',650);
insert into abc01 values(3,'王先生',400);
练习,查询Price 平均值
select avg(price) from abc01;-- 结果683.3333
查询比平均值高的用户
select * from abc01 where Price > (select avg(Price) from abc01);--结果 1 李先生 1000
统计一共有几行数据
select count(*) from abc01;结果3
统计name字段有多少行数据
select count(name)from abc01;结果3
查询price最大的值
select max(price) from abc01;结果1000
查询price最小的值
select min(price) from abc01;结果400
查询prioce总额
select sum(price) from abc01;接轨2050
聚合函数计算结果 group by 分组
group by:用于对结果集分组
create table num(
id int primary key auto_increment,
name varchar(25) not null,
price int
);
insert into num values(1,'李先生',200);
insert into num values(2,'吴先生',100);
insert into num values(3,'李先生',400);
insert into num values(4,'李先生',20);
insert into num values(5,'狗先生',40);
insert into num values(5,'吴先生',1);
查询:希望查询每个用户的总金额 sum()
select name,sum(price) from num group by name;
结果集:
name price
李先生 620
吴先生 101
狗先生 40
查询: 去掉重复的名字
select name from num group by name;
结果集:
name
李先生
吴先生
狗先生
查询:同姓人的个数 count(*)
select name,count(*) as '人数' from num group by name;
结果集:
name 人数
吴先生 2
李先生 3
狗先生 1
查询:同姓人工资平均数 avg()
select name,avg(price) as '平均工资' from num group by name ;
结果集:
name 平均工资
吴先生 50.5000
李先生 206.6667
狗先生 40.0000
查询:同姓名的人 工资最高的那个人
select name,max(price) as '最大' from num group by name ;
查询:同姓名的人 工资最低的那个人
select name,min(price) as '最小' from num group by name ;group by 的条件判断 having
group by 无法和where一起使用,having可以
create table num(
id int primary key auto_increment,
name varchar(25) not null,
price int
);
insert into num values(1,'李先生',200);
insert into num values(2,'吴先生',100);
insert into num values(3,'李先生',400);
insert into num values(4,'李先生',20);
insert into num values(5,'狗先生',40);
insert into num values(5,'吴先生',1);
select * from num; 结果集
id name price
1 李先生 200
2 吴先生 100
3 李先生 400
4 李先生 20
5 狗先生 40
6 吴先生 1
条件 我们希望查询工资小于200的人
select name,sum(price) from num group by name having sum(price)<200;
结果集:
name sum(price)
吴先生 101
狗先生 40
条件 我们希望查询客户 吴先生 或者 狗先生 工资超过10的
select name,sum(price) from num where name='吴先生' or name = '狗先生' group by name having sum(price)>10;
结果集:
吴先生 101
狗先生 40把字段里的值转换为 大写ucase( ) 小写lcase( )
ucase()把字段里面的值 转换为大写
create table sum01(
id int,
name varchar(20)
);
insert into sum01 values(1,'abcd');
insert into sum01 values(2,'ABc');
例子1;name 里面的数据全部转换大写
select id,ucase(name) from sum01;
结果集:
id name
1 ABCD
2 ABC
例子2;name 里面的数据全部转换小写
select id,lcase(name) from sum01;
结果集:
id name
1 abcd
2 abc把字段的数据提取字符mid()
mid 提取字符
create table sum01(
id int,
name varchar(20)
);
insert into sum01 values(1,'abcd');
insert into sum01 values(2,'ABc');
-- 从第一个开始 提取两个
select mid(name,1,2) from sum01;
结果集:
ab
AB版权声明:本文为weixin_45967375原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。