mysql SQL语句
mysql——管理数据库的软件,里面有多个数据库。
一些面试题转载:https://blog.csdn.net/qq_43475097/article/details/84346417
SQL查询——占70%
1.select [distinct] *|字段1|字段2|.....字段n from 表名;
distinct——删除指定字段的重复的数据;
select distinct gender from student;
(再有很多的数据的数据库中谨慎使用上面查询)
2.select可以使用表达式,并且还可以使用as:字段 as 别名 或者 字段 别名(起了别名之后再用之前的名字就会报错)
select salary+1000 from A;
select math+english+chinese from score;
select math+english+chinese as 总成绩 from score;
select name as 姓名 from students;
3.where子句——过滤(条件)查询
where 子句中可以使用:
比较运算符:
>,<,<=,>=,!=
范围查询:
between 值1 and 值2——表示在一个连续的范围内
select * from students where age between 18 and 24;
select * from students where age not between 18 and 24;——记住这个就好
(select * from students where not age between 18 and 24;)
in(值1,值2,值3,值4......值n)表示在一个非连续的范围内,匹配的值是其中的一个
select name,age from students where age in(12,18,22)
select name,age from students where age not in(12,18,22)
模糊查询(用得比较少):
like:
like "周%" 匹配周后面的人一个字符
select name from students where name like "小%";
like ”周____“ 如果是_则是有几个_则就匹配相应多少个个字符;
(查询有三个字符的名字)
select name from students where name like "___";
(查询至少有两个字的名字)
select name from students where name like "__like";
rlike正则表达式:
select name from students where name rlike "^周.*琼$";
逻辑运算符:
and,or,not
select * from students where age>18 and age<28;
select * from students where age>18 or age<28;
优先级:
select * from students where not (age<18 and gender="女");
判断是否空:
is null(为空)
select * from students where height is null;
is not null(判断非空)
select * from students where height is not null;
4.order by——指定表中的字段或者select语句后指定的别名来进行排序。
(在没有指定排序条件的情况下都是按照主键排序,但是在指定的排序条件之后可以指定主键的升降序排序)
(身高从高到低排序,如果身高相同的情况下按照年龄从小到大排)
select * from students where age between 18 and 28 and gender="女" order by height desc,age asc,id desc;
ASC——升序,默认为升序;
DESC——降序;
order by 位于select语句的末尾;
ifnull主要是用在select字段数据里,相当于代码里的一个二元函数,即看字段是否为空,不为空则返回该字段,为空则返回函数里的第二个参数的值。
select name, (ifnull(math,0)+ifnull(english,0) +ifnull(chinese,0)) as 总成绩 from scores order by 总成绩;
5.group by——分组查询
having——对分组之后的结果进行筛选;
分组的意义:和聚合函数一起使用,否则毫无意义;
通过by后面的一个或者多个字段进行分组,分组之后对其组内的信息做处理,否则无意义;
按分组条件分组后每一组只显示第一条记录;
(通过gender分组之后,查看分组之后每一组中name有哪些)
select gender,grou_concat(name) from students group by gender;
select gender,grou_concat(name,age,id) from students group by gender;
select gender,grou_concat(name,"_",age,"_",id) from students group by gender;
6.聚合函数,先把需要的内容查出来在包上聚合函数即可
count(列名)——统计行的个数;
sum(列名)——统计满足条件的行的总和;
avg(列名)——求平均分;
max(列名)——最大值;
min(列名)——最小值;
四舍五入:
round(123.467384,2)——保留两位小数
select round (sum(score) / count(*),3) from students;
7.limit——分页显示记录
select * from table limit m,n
其中m是指记录开始的index,从0开始,表示第一条记录
n是指从第m+1条开始,取n条。
(查询前五个:)
select * from students limit 5;
(查询id6-10的书序:)
select * from students limit 5,5;
(每页显示5个,第一个页面)
select * from students limit 0,5;
(每页显示5个,第二个页面)
select * from students limit 5,5;
(每页显示5个,第三个页面)
select * from students limit 10,5;
(每页显示m个,第n个页面)
select * from students limit (第n页-1)*m,m;
但是请不要这么写:
select * from students limit (3-1)*5,5;
最重要的来了!!!!!!
多表查询
当查询结果的列来源于多张表时,需要将多张表连接成一个大的数据集,再选择合适的列返回;
准备表:
create table UserInfo(
UserID int primary key auto_increment,
UserAccounts int,
UserName varchar(20),
UserPwd varchar(10));
insert into table Userinfo ( UserID,UserAccounts ,UserName , UserPwd ) values
(1,50,"a","a123"),
(2,52,"b","b123"),
(3,53,"c","c123"),
(34,550,"d","d123"),
(374,5540,"f","f123");
create table News(
NewsID int primary key auto_increment,
UserID int,
NewsTitle varchar( 50 ),
NewsRelease varchar( 200 ),
NewsReleaseTime datetime,
FOREIGN KEY (UserID) REFERENCES UserInfo(UserID)); --外键约束
insert into News(NewsID,UserID,NewsTitle) values
(100,1,"xxx发生火灾"),
(101,2,"xxx发生水灾"),
(102,3,"xxx发生天灾"),
(104,374,"xxx发生旱灾"),
多表查询之连接查询:
1.笛卡尔积
select * from UserInfo,News;
2.内连接
查询两张表都有的关联数据,相当于利用条件从笛卡尔积结果中筛选了正确的结果
select * from UserInfo,News where UserInfo.UserID=News.UserID;
或者 select * from UserInfo inner join News on UserInfo.UserID=News.UserID;
3.外连接:
(1)左外连接——在内连接的基础上增加左边有右边没有的结果,右边没有的用null补充
select * from UserInfo left join News on UserInfo.UserID=News.UserID and UserAccounts>55 ;
(2)右外连接——在内连接的基础上增加右边有左边没有的结果,左边没有的用null补充
select * from News right join UserInfo on UserInfo.UserID=News.UserID and UserAccounts>55 ;
(3)全外外连接——在内连接的基础上增加左边有右边和右边有左边没有的结果
mysql不支持全外连接,但是可以间接实现
select * from UserInfo left join News on UserInfo.UserID=News.UserID
union
select * from UserInfo rightjoin News on UserInfo.UserID=News.UserID;
4.子查询
- 子查询是将一个查询语句嵌套在另一个查询语句中。
- 内层查询语句的查询结果,可以为外层查询语句提供查询条件。
- 子查询中可以包含:IN、NOT IN、ANY、ALL、EXISTS 和 NOT EXISTS等关键字
- 还可以包含比较运算符:= 、 !=、> 、<等
5.自关联
【部分参考别人】
设计省provinces信息的表结构:
id
ptitle
设计省下面的市ctitys信息的表结构:
id
ctitle
proid
ctitys表中的proid表示城市所属的省,对应着provinces表的id值;
通过发现存储的都是地区信息,而且每种信息的数据量有限,没必要增加一个新表,或者将来还要存储区、乡镇信息,都增加新表的开销太大;
重新设计表结构:
id
atitle
pid
省没有所属的省份,所以可以填写为null;
城市中的所属的省份pid,填写省所对应的编号id;
自关联:这就是自关联,表中的某一列,关联了这个表中的另外一列,但是它们的业务逻辑含义是不一样的,城市信息的pid引用的是省信息的id;
在这个表中,结构不变,可以添加区县、乡镇街道、村社区等信息
具体请参考:https://blog.csdn.net/hubingzhong/article/details/81277220
但是在实际的工作中用得最多的还是内连接和左外连接!!!!!!!!!!!!!——尤其是软件测试面试的时候。
1.关系型数据库
关系型数据库是建立在关系模型基础上的数据库;
借助于集合代数等数学概念和方法来处理数据库中的数据。
关系型数据库主要有:
Oracle:以前的大型项目中使用,比如银行,电信等项目;
MySQL: web时代使用最广泛的关系型数据库;
Ms SqlServer:在微软的项目中使用;
sqlite:轻量级数据库,只要应用在移动平台。
在本机安装了mysql数据库之后,进入cmd
登陆:mysql (-h 服务器 -P 端口(如果是默认不填的话登陆的就是在本机127.0.0.1的3306的mysql))-u 用户名 -p 回车,这样输入的密码就是密文
退出:quit,exit,\q
启动mysql服务:net start mysql
停止mysql服务:net stop mysql
查询端口是否冲突:netstat –na | findstr 8080 查看被监听的端口 , findstr用于查找后面的端口是否存在。
修改密码:格式:mysqladmin -u用户名 -p旧密码 password 新密码;例如给root修改密码: mysqladmin -u root -p ab12 password djg345
数据类型:
使用数据类型的原则:够用就行,尽量使用取值范围小的,而不用大的,这样可以节省存储空间。
常用的数据类型:
整数:int bit;
小数:decimal——表示浮点数,如decimal(3,2)表示共存5位数,小数占两位;
字符串:varchar,char
varchar表示科比那长度的字符串,如varchar(4),填充'ab'时就会存储'ab';
char表示固定长度的字符串,如char(3),填充'ab'时会补一个空格'ab '
日期时间:date,time,datetime
枚举类型:enum
字符串text表示存储大文本,当字符大于4000时推荐使用
对于对于图片、音频、视频等文件,不存储在数据库中,而是上传到某个服务器上,然后在表中存储这个文件的保存路径。
使用navicat 新建数据库,在弹出来的窗口选择:

2.SQL的一些规范:
SQL语句不区分大小写(但是建议大写),;为结束符
单行注释:--
多行注释:/*......*\
3.数据库操作:
show databases;——查看所有的数据库
create database (if not exists)数据库名;——(如果不存在)创建数据库
drop database (if exists)数据库名:——(如果存在)删除数据库
show warnings;——查看警告
show create database 数据库名——查看数据库的创建方式;
create database 数据库名 charset=utf8;
select now()——查看当前数据库时间
select version——查看当前数据库版本
set utf8;——改变编码
use 数据库名;——使用数据库
select database();——查看当前进入的数据库
4.数据表操作
(1)完整性约束:
primary key——主键,非空且唯一
unique—— 约束确保在非主键列中不输入重复的值
not null——不为空
unsigned-——无符号(比如 int unsigned——无符号范围,0—4294967295)
auto_increment——用于为一个表中记录自动生成ID功能
foreign key——外键
(2)创建表
create table 表名(字段名 type [完整性约束]);
create table A(id int primary key auto_increment,
name varchar(20),
job varchar(20) not null);
(3)修改表结构
增加列:
alter table 表名 add 列名 type [完整性约束] [first | after 字段名;
增加多个:
alter table users2
add addr varchar(20),
add age int first,
add birth varchar(20) after name;
修改某一列的类型(type):
alter table 表名 modify 列名 类型 [完整性约束 [first | after 字段名;
alter table users2 modify age int after id;
修改列名:
alter table 表名 change 列名 新列名 type [完整性约束] [first | after 字段名;
alter table users2 change age Age int default 28 first;
删除某一列:
alter table 表名 drop 列名;
修改表名:
rename table 表名 to 新表名;
5.表记录操作:
(1)插入记录:
insert [into] 表名(字段1 ,字段2,字段3.....)values (值1,值2,至3........)
选择部分字段的插入:
insert into A(id,name,age,birth)values(1,"小猪猪",18,"1999-05-21")
选择其中字段的插入,则需要将相应的字段名带上;
插入多条:
insert into A(id,name,age,birth)values
(1,"小猪猪",18,"1999-05-21"),
(18,"小猪猪2",19,"1998-05-21"),
(87,"小猪猪9",45,"1989-05-21");
全部字段的插入:
insert into A values
(34,"小猪猪98",45,""四川省凉山州","成都信息工程大学",1979-05-21"),
(74,"小猪猪98",35,""成都市","四川大学",1979-05-21");
键-值对形式插入:
insert into 表名 set 字段名=值;
insert into A id=200;
(2)修改表记录
update 表名 set 字段名1=值1,字段名2=值2,字段名3=值3......字段名n=值n [where 字句]
update A set salary=salary+5000 where id=23;
update可用于表中新值更新原表行中的各列;
where则是更新指定的行,如若不指定,则是跟新所有的行。
(3)删除表记录
delete from 表名 [where 字句]
不跟where语句,删除整张表中的数据
delete只能用来删除一行记录
delete语句只能删除表中的内容,不能删除表本身,想要删除表,用drop

浙公网安备 33010602011771号