【作为测试这些MySQL知识必备】。书写万字手把手教你MySQL,从建库开始步步教学,也可直接复制粘贴使用

1、整数类型(INT)占位4字节

2、浮点类型有两种:单精度浮点数类型(FLOAT)占位4字节和双精度浮点数类型(DOUBLE)占位8字节

3、字符串类型(CHAR) 占3字节和 (VARCHAR)4字节

4、日期与时间类型(YEAR)占1字节、(DATE)占4字节、(TIME)占3字节。

基本操作

====

建库,建表,添加、修改、删除表单等,以及增删改查!随意建个库,MySQL对于字母大小写没有严格的要求,必须规定你大写或小写。你随意,开心了大写,不开心了小写。😕

创建数据库


create database 数据库名称;

create database qingan CHARACTER SET utf8 COLLATE utf8_general_ci;

查询数据库基本信息


show create database 数据库名称;

show create database qingan;

使用数据库


use 数据库名称;

use qingan;

创建数据表


这里将上面的例子全部用下来,活学活用,学以致用。💗,由建库到建表

CREATE DATABASE qingan;

use qingan;

create table wubieshi(

id varchar(18),

sex char(10),

c_name varchar(10)

);

查询表单


show tables;

查询表单基本信息


show create table 表单名称;

show create table wubieshi;

查询表的字段信息


desc 表单名称;

desc wubieshi;

修改表名


alter table 原表单名称 rename to 想要修改的名称;

alter table wubieshi rename to qing;

修改字段名及数据类型


这里因为上述改了表名所以后续都用qing这个表名。

alter table 表单名称 change 原字段名 修改字段名 数据类型;

alter table qing change id id_number varchar(40);

修改数据类型


alter table 表名 modify 字段名 数据类型;

alter table qing modify id varchar(10);

增加字段


alter table 表名 add 增加字段名 数据类型;

alter table qing add c_name varchar(30);

删除字段


alter table 表名 drop 字段名;

alter table t_beauty drop c_name;

删除数据表


drop table 表名;

drop table qing;

增删改查

====

此处我们从简,别搞的那么麻烦,所以你建库建表的时候就必须要想清除了,别后面删删改改的,倒时候自己都搞错了。

增:

INSERT INTO 表名(字段名1,字段名2,…) VALUES (值 1,值 2,…);

insert into qing(id,sex,home,call_num)
VALUES(‘小芳’,‘女’,‘北京’,132465);

同时增加多条数据:

insert into qing(id,sex,home,call_num) VALUES(‘小芳’,‘女’,‘北京’,132465),

(‘小小’,‘女’,‘上海’,777555),

(‘小沐’,‘男’,‘广东’,1525465)

改:

此处是将搜索id为晴雪的改成小芳,切勿搞混了。

UPDATE 表名 SET 字段名1=值1 [WHERE 条件表达式];

update qing set id = ‘小芳’ where id=‘晴雪’;

指定一个字段数据,将所有的id字段名都更改为小北

update student set id=‘小北’;

删:

DELETE FROM 表名 [WHERE 条件表达式];

delete from qing where id = ‘小小’;

删除所有数据:

delete from 表1;

delete from wubieshi;

查:

select * from 表名;

select * from wubieshi;

查询指定字段:

select 字段名1,字段名2 from 表名;

select id,name form qing;

过滤重复信息:

过滤掉重复的name名字

select distinct 字段名 from 表名;

select distinct name from qing;

聚合函数

====

count


查询表中有多少人

select count(*) from 表名;

select count(*) from qing;

max


计算指定列最大值,这里只是举例,没有谁会去计算id的,没啥意义。

select max(字段名) from 表名;

sele max(id) from qing;

min


计算指列最小值

select min(字段名) from 表名;

select min(id) from qing;

sum


求和

select sum(字段名) from 表名;

select sum(id) from qing;

avg


求平均值

select avg(字段名) from 表名;

select avg(id) from qing;

关系运算符

=====

关系运算符说明
=等于
<>不等于
<小于
>大于
<=小于等于
>=大于等于
!=不等于

关系运算符不要求全部记住,但是这里的必须要会,毕竟后面还是要用的。看看运用

select * from student where 字段名>=条件;

select * from student where age>=17;

关键字查询

=====

in

用于判断某个字段的值是否在指定集合中

select * from 表名 where 字段名 in (‘字段1’);

select * from qing where id in (‘穆雪’);

not in


用于判断某个字段的值不在在指定集合中,不在输出全部

select * from 表名 where 字段名 not in (‘字段1’);

select * from qing where id not in (‘穆雪’);

between and


select * from 表名 where 字段名 between 条件1 and 条件2;

select * from qing where age between 15 and 18;

not between and


select * from 表名 where 字段名 not between 条件1 and 条件2;

select * from qing where age not between 15 and 18;

反用null查询


查询表中不是空值的值(字段)

select * from 表名 where 字段名 is not null;

select * from qing where id is not null;

and查询


select * from 表名 where 条件判断 and 条件判断;

select * from qing where age>18 and id=‘沐雪’;

or查询


select * from 表名 where 条件查询 or 条件查询;

select * from qing where age>15 or id=‘沐雪’;

字符串匹配查询

=======

普通字符串查询


查询id为‘沐雪’的人

select * from 表名 where 字段名 like 字符串;

select * from qing where id like ‘沐雪’;

%通配的字符串查询

查询id为‘沐雪~’的人,如:沐雪,沐雪雪,沐雪砅等

select * from 表名 where 字段名 like ‘字段1%’;

select * from qing where id like ‘沐雪%’;

查询以某字符串结尾的记录


查询以‘~雪’结尾的记录,如:沐雪,夏雪等

select * from 表名 where 字段名 like ‘%字段1’;

select * from qing where id like ‘%雪’;

通配字符串查询


下划线通配符只匹配单个字符,如果要匹配多个字符,需要连续使用多个下划线通配符。单个下划线只匹配一个字符串,两个下划线匹配多个字符串。

select * from 表名 where 字段名 like ‘字段1__’;

select * from qing where id like ‘x__’;

select * from qing where id like ‘x_’;

limit限制查询

=========

select * from qing limit i,n;

# qing:表名

# i:为查询结果的索引值(默认从0开始),当i=0时可省略i

# n:为查询结果返回的数量

# i与n之间使用英文逗号","隔开

查询5条数据,从3开始往后查询5条数据

select * from qing limit 2,5;

order by查询

==========

ORDER BY默认从小到大,asc也是从小到大排序,可以忽略不写,desc从大到小排序

select * from 表名 order by 字段名 asc;

select * from qing order by age asc;

select * from 表名 order by 字段名 desc;

select * from qing order by age desc;

limit结合order by查询


select * from qing order by age asc limit 5;

group by


多个列中取相同值,如下,在表t_beauty中取相同的字段名为id的值

select count(*), 字段名 from 表名 group by 字段名;

select count(*), id from qing group by id;

结合where使用

查询表中id>1001的数据

select count(*), 字段名 from 表名 where 字段名>值 group by 字段名;

select count(*), id from qing where id>1001 group by t_beauty;

组合having使用

查询班级里薪水大于2000的数据,这里值得注意的是having后面必须接聚合函数

select sum(字段名1),字段名2 from 表名 group by 字段名2 having sum(字段名1)>值;

select sum(salary),class from qing group by calss having sum(salary)>2000;

数据表约束

=====

主键约束


create table qing(

id int primary key,

names varchar(20)

);

非空约束


create table qing(

id int,

names varchar(20) not null

);

默认约束


即给定默认值

create table qing(

id int,

names varchar(20),

gender varchar(10) default ‘cha’

);

唯一约束


create table qing(

id int,

names varchar(20) unique

);

外键约束


CONSTRAINT fk_qing FOREIGN KEY(Id) REFERENCES qingan(id)

CONSTRAINT 从表 FOREIGN KEY(字段名) REFERENCES 主表(字段名)

create table qing(

id int,

names varchar(20) unique,

CONSTRAINT fk_qing FOREIGN KEY(Id) REFERENCES qingan(id)

);

创建数据表创号后语法如下:

ALTER TABLE 从表名 ADD CONSTRAINT 外键名 FOREIGN KEY (从表外键字段) REFERENCES 主表 (主键字段);

删除外键约束

alter table 从表名 drop foreign key 外键名;

取别名

===

select * from 原表名 as 表名;

select * from qing as qingan;

select 原字段名 as 字段名 from 表名;

select id as c_id from qingan;

表的关联查询

======

适用于表与表之间有相同的数据,如class,classroom1中nname,cname字段中有相同值

select * from 表名1 where 字段名1=(select 字段名2 from 表名2 where 字段名3=‘值’);

select * from class where nname=(select cname from classroom1 where cid=2);

关联删除


DELETE t1 FROM t1,t2 WHERE t1.id=t2.id

delete from 表 where 字段名=值;

delete from qing where id=1;

多表连接查询

======

SELECT * FROM 表1 CROSS JOIN 表2;

select * from qing cross join qingan;

内连接查询


内连接(Inner Join)又称简单连接或自然连接,是一种非常常见的连接查询。内连接使用比较运算符对两个表中的数据进行比较并列出与连接条件匹配的数据行,组合成新的记录。

这里是查询id相同的字段:

SELECT 表名1.字段,表名2.字段, … FROM 表1 [INNER] JOIN 表2 ON 表2.关系字段=表1.关系字段

SELECT qing.id,qingan.cid FROM qing INNER JOIN qingan ON qingan.cid = qing.id;

如果是三个表,我需要查询其中的id相同的name:

SELECT qing.id,qing.bname,qingan.gname FROM qing INNER JOIN qingan ON qingan.hid = qing.hid;

外连接查询


外连接又分为左外连接和右外连接,其用法类似,以其中一张表为主表,另外一张为从表。

**1、LEFT [OUTER] JOIN 左(外)连接:返回包括左表中的所有记录和右表中符合连接条件的记录。

2、RIGHT [OUTER] JOIN 右(外)连接:返回包括右表中的所有记录和左表中符合连接条件的记录。**

注意事项:关联查询的时候,字段名可以不一样,当时数据类型需要一致,右外连接只需要将下面的例子left改为right即可使用,注意主从表即可

注意:外连接查询也可以是删除!!!

select 表1.字段1,表1.字段2,表2.字段3 from 表1 left outer join 表2 on 表1.关系字段1=表2.关系字段2;

select class.did,class.dname,student.sname from class left outer join student on class.did=student.classid;

子查询

===

子查询是指一个查询语句嵌套在另一个查询语句内部的查询;

比较运算符的子查询


查询qingan所在班级的信息

select * from 表1 where 字段名1>(select 字段名2 from 表2 where 字段名3=查询数据);

select * from d_class where c_id>(select d_id from d_student where d_name=‘qingan’);

exists


exists关键字后面的参数可以是任意一个子查询, 它不产生任何数据只返回TRUE或FALSE。当返回值为TRUE时外层查询才会 执行

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值